Skip to main content



      Home
Home » Language IDEs » ServerTools (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 11:45 Go to next message
Eclipse UserFriend
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] Tue, 16 August 2011 23:09 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: JavaScript formatter and comma separated vars [message #716481 is a reply to message #716317] Wed, 17 August 2011 10:26 Go to previous messageGo to next message
Eclipse UserFriend
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 15:29 Go to previous messageGo to next message
Eclipse UserFriend
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
Re: JavaScript formatter and comma separated vars [message #716715 is a reply to message #716592] Thu, 18 August 2011 04:10 Go to previous messageGo to next message
Eclipse UserFriend
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 15:49 Go to previous messageGo to next message
Eclipse UserFriend
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 15:49] by Moderator

Re: JavaScript formatter and comma separated vars [message #1033062 is a reply to message #715805] Wed, 03 April 2013 16:19 Go to previous messageGo to next message
Eclipse UserFriend
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 17:48 Go to previous messageGo to next message
Eclipse UserFriend
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 16:21 Go to previous messageGo to next message
Eclipse UserFriend
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.
Re: JavaScript formatter and comma separated vars [message #1441130 is a reply to message #715805] Thu, 09 October 2014 05:44 Go to previous message
Eclipse UserFriend
Yes please make this happen!
Previous Topic:Javascript editor hangs for long period
Next Topic:How to deploy persistence.xml in a web project?
Goto Forum:
  


Current Time: Mon Jul 14 21:09:00 EDT 2025

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

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

Back to the top