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.

Support comma first style of variable declaration

See original GitHub issue

Allow code to be formatted like this:

var a = 1
  , b = "somethign else"
  , isAwesome = true;

And apparently, we have a somewhat ready patch available. Would be great if this can get included in here!

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:27 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
tgirardicommented, Dec 16, 2013

I’ve been working on a patch to support this feature “on demand” (the user can enable or disable the option of using comma first with a setting I’ve created).

I’ve achieved the following formatting for variable declaration (using 2 spaces):

var firstVar = 1
  , secondVar = 2
  , thirdVar = 3
;

But I have some doubts.

How should we treat arrays, objects and arguments declarations? Lately I’ve been using the following format:

myArray = [
  item1
, item2
, item3
];

myObject = {
  prop1: 1
, prop2: 2
, prop3: 3
}

Which, as you can see, is not exactly the same as the variable declaration formatting: this last one includes a +1 indentation level for the second variable (notice the two spaces before the comma that are not present before the comma for “item2” nor for the one for “prop2”). Var declarations use this extra indentation in order to align in the same column the start of each variable name as stated by @lukemartin.

The reasons for using the formatting shown on the above code are:

1.- To avoid the linting errors that would be thrown by jshint (indentation errors). For example:

myArray = [
    item1
  , item2
];

Throws “Expected ] to have an indentation at 3, instead at 1”. If we follow the suggestion, we get a very ugly result.

2.- To maintain the advantage of aligning the start of each property name, array item, etc. For example:

myArray = [
  item1
  , item2
];

Also doesn’t produce a linting error, but doesn’t look as nice as the examples above.

With a more clear idea of how I should treat those cases I could finish the implementation of this feature and issue a Pull Request.

1reaction
evocateurcommented, Nov 8, 2013

What about 4 spaces per tab? Actual tabs?

On Fri, Nov 8, 2013 at 9:36 AM, Luke Martin notifications@github.com wrote:

+1 for comma-first support. At 2-space indentation, it’s the tidiest way of arranging variables var foo = true , bar = ‘hello’

, something;

Reply to this email directly or view it on GitHub: https://github.com/einars/js-beautify/issues/245#issuecomment-28081629

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support comma first style of variable declaration · Issue #59 ... - GitHub
Allow code to be formatted like this: var a = 1 , b = "somethign else" , isAwesome = true; JSBeautifier supports it...
Read more >
comma-style - ESLint - Pluggable JavaScript Linter
"first" requires a comma before and on the same line as an array element, object property, or variable declaration. This rule also accepts...
Read more >
Comma operator (,) - JavaScript - MDN Web Docs
The comma ( , ) operator evaluates each of its operands (from left to right) and returns the value of the last operand....
Read more >
JavaScript Code Styling Best Practices — Commas and ...
In JavaScript code, there's a standard style for arranging commas. For instance, for a comma-separated list of variable or constant declarations ...
Read more >
scope - what is the purpose of comma in declaring variables ...
what is the purpose of comma (,) after div? var div, container = document.getElementById("container");. is it for declaring two variables at ...
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