Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Web Tools Project (WTP) » JavaScript formatter and comma separated vars(JavaScript formatter support for comma-separated, multiple, local-variable declarations)
JavaScript formatter and comma separated vars [message #715805] Mon, 15 August 2011 15:45 Go to next message
Martin  is currently offline Martin
Messages: 3
Registered: August 2011
Junior Member
I was wondering whether there's a way (I can't find one) of getting the JavaScript formatter to line-break after commas in multiple-variable declarations?

Currently:

var myVar = 'test', hisVar = 'real', herVar = 'live';


Ideally:

var myVar = 'test',
    hisVar = 'real',
    herVar = 'live';


This would then conform with the standard JSLint practice of declaring all at the front, and fit in nicely with the JavaScript parser's predilection for grabbing all the variables in the function and then simply declaring them first anyway!

If there's no way of doing it, how would I go about putting it in as a feature request?
Re: JavaScript formatter and comma separated vars [message #716317 is a reply to message #715805] Wed, 17 August 2011 03:09 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2272
Registered: July 2009
Senior Member
On 8/15/2011 11:45 AM, Martin wrote:
> I was wondering whether there's a way (I can't find one) of getting the
> JavaScript formatter to line-break after commas in multiple-variable
> declarations?
>
> Currently:
>
> var myVar = 'test', hisVar = 'real', herVar = 'live';
>
> Ideally:
>
> var myVar = 'test',
> hisVar = 'real',
> herVar = 'live';
>
> This would then conform with the standard JSLint practice of declaring
> all at the front, and fit in nicely with the JavaScript parser's
> predilection for grabbing all the variables in the function and then
> simply declaring them first anyway!
>
> If there's no way of doing it, how would I go about putting it in as a
> feature request?

You can enter a feature request here:
https://bugs.eclipse.org/bugs/enter_bug.cgi?classification=WebTools&product=JSDT&component=General&bug_severity=enhancement

Although that particular combination, one var keyword with multiple
lines, doesn't seem like it would be that popular.

--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: JavaScript formatter and comma separated vars [message #716481 is a reply to message #716317] Wed, 17 August 2011 14:26 Go to previous messageGo to next message
Martin  is currently offline Martin
Messages: 3
Registered: August 2011
Junior Member
Thank you for that Nitin. The reason for it is to do with JavaScript's scoping. I'll just quote the infamous Douglas Crockford, instead of explaining myself:

Quote:
In many languages, a block introduces a scope. Variables introduced in a block are not visible outside of the block.

In JavaScript, blocks do not introduce a scope. There is only function-scope. A variable introduced anywhere in a function is visible everywhere in the function. JavaScript's blocks confuse experienced programmers and lead to errors because the familiar syntax makes a false promise.

[...]

In languages with block scope, it is usually recommended that variables be declared at the site of first use. But because JavaScript does not have block scope, it is wiser to declare all of a function's variables at the top of the function. It is recommended that a single var statement be used per function. This can be declined with the vars option.


Hence why people prefer to have a single var, with multiple lines to make it more readable. Plenty of discussion on StackOverflow on a similar theme, if anyone's interested, e.g. use bit.ly/oza1yZ (can't post links yet)

Anyway, I'll go off and make that feature request when I get some time Smile
Re: JavaScript formatter and comma separated vars [message #716592 is a reply to message #716481] Wed, 17 August 2011 19:29 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin Dahyabhai
Messages: 2272
Registered: July 2009
Senior Member
On 8/17/2011 10:26 AM, Martin wrote:
> Hence why people prefer to have a single var, with multiple lines to
> make it more readable. Plenty of discussion on StackOverflow on a
> similar theme, if anyone's interested, e.g. use bit.ly/oza1yZ (can't
> post links yet)
>
> Anyway, I'll go off and make that feature request when I get some time :)

Oh, I completely understand why he urges variables to be declared at the
top of functions, it's the splitting of lines in conjunction with that
that I find dubious. Formatting in that way makes it more difficult for
me to spot check that the lines are separated by commas rather than
semicolons (my eyesight is _very_ poor), the accidental presence of one
semicolon on any of those lines being potentially disastrous.

--
Nitin Dahyabhai
Eclipse WTP Source Editing and JSDT
IBM Rational


---
Nitin Dahyabhai
Eclipse WTP, IBM
Re: JavaScript formatter and comma separated vars [message #716715 is a reply to message #716592] Thu, 18 August 2011 08:10 Go to previous messageGo to next message
Martin  is currently offline Martin
Messages: 3
Registered: August 2011
Junior Member
Nitin Dahyabhai wrote
Formatting in that way makes it more difficult for
me to spot check that the lines are separated by commas rather than
semicolons (my eyesight is _very_ poor), the accidental presence of one
semicolon on any of those lines being potentially disastrous.


Not an unreasonable point, but I certainly wouldn't want the line breaking to be mandatory or even default ... just possible Smile
Re: JavaScript formatter and comma separated vars [message #786803 is a reply to message #716715] Mon, 30 January 2012 20:49 Go to previous messageGo to next message
Ben Cochran is currently offline Ben Cochran
Messages: 2
Registered: January 2012
Junior Member
I would love to have this, too. Keeping all the declarations on one line is a pain for variable visibility, and retyping var just brings Carpal Tunnel Day closer.

[Updated on: Mon, 30 January 2012 20:49]

Report message to a moderator

Re: JavaScript formatter and comma separated vars [message #1033062 is a reply to message #715805] Wed, 03 April 2013 20:19 Go to previous messageGo to next message
Wei Wang is currently offline Wei Wang
Messages: 2
Registered: July 2012
Junior Member
The JavaScript formatter is unusable for me unless this gets fixed Sad
Re: JavaScript formatter and comma separated vars [message #1078327 is a reply to message #715805] Fri, 02 August 2013 21:48 Go to previous messageGo to next message
Dave Morgan is currently offline Dave Morgan
Messages: 1
Registered: August 2013
Junior Member
Me too Sad

It wouldn't be so bad if line-wrap worked; my comma-separated variable declarations don't wrap with the rest of my code.
Re: JavaScript formatter and comma separated vars [message #1421727 is a reply to message #1078327] Thu, 11 September 2014 20:21 Go to previous message
Bryan Elliott is currently offline Bryan Elliott
Messages: 1
Registered: September 2014
Junior Member
This.

It's best practices to have one var statement per function, and having them forcibly unrolled into a single line as not only the default, but required way of doing things - it's wrong, it's annoying, and it makes the JS formatter completely unusable for practical purposes.
Previous Topic:Trouble importing WSDL definitions from another WSDL
Goto Forum:
  


Current Time: Fri Sep 19 07:49:20 GMT 2014

Powered by FUDForum. Page generated in 0.02268 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software