question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[json] option to format code with leading commas

See original GitHub issue

Can we get an option in VS Code to have it format all lists by using leading commas instead of trailing commas?

This would so much simplify editing code and it would also greatly simplify the IDE’s automatic code completion (e.g. the VS Code Settings editor).

With the suggested option set to true, comma separated lists would proposedly be formatted like this:

{ "workbench.iconTheme": "vs-seti"
, "editor.lineNumbers": "off"
, "editor.tabSize": 2
, "editor.insertSpaces": false
, "editor.autoClosingBrackets": false
}

Function calls using line wrapping for their arguments would look like this:

call( 1
    , "test"
    , new Console()
    );

a nested sample would look like this:

{ "version": "0.2.0"
, "configurations":
  [ { "type": "node"
    , "request": "launch"
    , "name": "Programm starten"
    , "program": "${file}"
    }
  , { "type": "node"
    , "request": "attach"
    , "name": "An den Port anfügen"
    , "address": "localhost"
    , "port": 5858
    }
  ]
}

This formatting style would result in much cleaner, tidier code.

Moreover, adding a new property to an object would be a snap:

Either copy/paste from an existing property (=> no more syntax errors due to redundant trailing commas, see screenshot below):

comma error

… or create a new property and you might immediately get IntelliSense right after typing the leading comma of the property to create.


The suggested formatting style is applicable to all languages

E.g. T-SQL:

CREATE TABLE
  ( id INT PRIMARY KEY IDENTITY
  , name NVARCHAR(100) NOT NULL UNIQUE CHECK(LEN(name) > 0)
  , created DATETIME NOT NULL DEFAULT(GETDATE())
  , updated DATETIME NOT NULL DEFAULT(GETDATE())
  )

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:24
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
SetTrendcommented, Jun 3, 2018

They always did. Same as they always supported leading commas. There is no rule on where to put punctuation characters.

Actually, once you started using leading commas you will immediately recognize the benefit of using it, and you will never want to revert to struggle with the clumsiness of trailing commas.

3reactions
caubcommented, Jul 3, 2018

Why do people still use this leading-commas formatting? objects, arrays, functions allow trailing commas (even json5 does) now, it’s significantly more readable

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you use a trailing comma in a JSON object?
With Relaxed JSON, you can have trailing commas, or just leave the commas out. They are optional.
Read more >
Trailing commas - JavaScript - MDN Web Docs
JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item.
Read more >
JsonSerializerOptions.AllowTrailingCommas Property
By default, AllowTrailingCommas is set to false , and a JsonException is thrown during deserialization if a trailing comma is encountered.
Read more >
Leading with commas — ugly or efficient? An investigation ...
Here I analyze 320 GB of SQL code from 80,000 GitHub repositories to support this choice. Background. Many programming languages (C, Python, ...
Read more >
Code Syntax Style: Trailing Commas - ReSharper - JetBrains
Configure preferences for trailing commas · Go to the Code Editing | C# | Syntax Style page of ReSharper options ( Alt+R, O...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found