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.

Add a setting for adding trailing/dangling commas

See original GitHub issue

Description

Historically, certain versions of Internet Explorer did not allow trailing commas in arrays or object literals, or caused strange behaviour when they were present. These days, trailing commas are either removed by minification tools, or code is run in Internet Explorer 9 or later. As such, the advice for writing JavaScript code in many codebases has changed from removing trailing commas, to instead requiring them.

I would like to see an option, which would be off by default, for adding trailing commas to arrays and objects. (Possibly function argument lists and function calls with ES2017) Currently js-beautify will remove trailing commas, but this conflicts with the eslint configuration which is set up in my codebase, which means that js-beautify cannot be used on the code in my codebase without also applying eslint --fix, which isn’t the fastest thing in the world.

Input

var x = [
    1,
    2,
    3,
];

var y = {
   a: 1,
   b: 2,
   b: 3,
];

var missing_comma = [
    1,
    2
]

Expected Output

I want js-beautify to leave my trailing commas alone, or add them.

var x = [
    1,
    2,
    3,
];

var y = {
   a: 1,
   b: 2,
   b: 3,
];

var missing_comma = [
    1,
    2,
]

Actual Output

js-beautify instead removes all trailing/dangling commas.

var x = [
    1,
    2,
    3
];

var y = {
   a: 1,
   b: 2,
   b: 3
];

var missing_comma = [
    1,
    2
]

Steps to Reproduce

Run js-beautify on any file containing the syntax in the example.

Environment

OS: Linux

Settings

I want a new setting for this. The current settings are irrelevant.

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:26
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
w0rpcommented, Apr 4, 2017

Is it possible to add this option? I went searching for it, and realised that I already added an issue a few months ago.

1reaction
bitwisemancommented, May 26, 2019

This seems like a moderate difficulty task.

PR’s are welcome but it will need some design discussion - this would be a change in philosophy for this project. There will also need to be extensive tests.

Read more comments on GitHub >

github_iconTop Results From Across the Web

comma-dangle - ESLint - Pluggable JavaScript Linter
Trailing commas simplify adding and removing items to objects and arrays, since only the lines you are modifying must be touched. Another argument...
Read more >
Trailing comma after last line in object - Stack Overflow
To modify the setting in VSCode: Go to FILE -> PREFERENCES -> SETTINGS. (VS Code Menus); Settings window should open.
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 >
Code Syntax Style: Trailing Commas - ReSharper - JetBrains
Code Syntax Style: Trailing Commas · It is easier to add or remove the last item, because it is always another single line....
Read more >
How to Allow Trailing Commas (Comma-Dangle) With ...
You may require trailing commas in all places of your code. Or you can require comma-dangle in multiline statements. For example, the "only- ......
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