Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 325280 - [validation] Should report Javascript syntax parsing error on final commas within array or object initializers
Summary: [validation] Should report Javascript syntax parsing error on final commas wi...
Status: CLOSED WORKSFORME
Alias: None
Product: JSDT
Classification: WebTools
Component: General (show other bugs)
Version: unspecified   Edit
Hardware: PC Windows 7
: P3 enhancement with 3 votes (vote)
Target Milestone: 3.8 RC4   Edit
Assignee: Project Inbox CLA
QA Contact: Chris Jaun CLA
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2010-09-14 14:28 EDT by Philippe Verdy CLA
Modified: 2016-09-19 18:11 EDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Philippe Verdy CLA 2010-09-14 14:28:55 EDT
Array initializers and object initializers in Javascript are perfectly valid if the last item is terminated by a comma.

Currently Eclipse incorrectly signals a syntax error on these commas:

x = [1, 2, ]; // same as x = [1, 2];
y = {a: 1, b: 2, }; // same as y = {a: 1, b: 2};

Javascript is more relaxed than Java here because it simplifies the generation of lists of items in indexed arrays or associative arrays/objects...

-- Configuration Details --
Product: Eclipse Platform 3.6.0.v201006080911 (org.eclipse.platform.ide)
Installed Features:
 org.eclipse.platform 3.6.0.v20100602-9gF78GpqFt6trOGhL60z0oEx3fz-JKNwxPY
Comment 1 Nitin Dahyabhai CLA 2011-06-30 17:29:34 EDT

*** This bug has been marked as a duplicate of bug 329716 ***
Comment 2 Nitin Dahyabhai CLA 2011-06-30 17:31:20 EDT
Reopening after more consideration.  There's no error reported *now*.
Comment 3 Philippe Verdy CLA 2011-06-30 17:52:03 EDT
This was clearly NOT a duplicate for bug 329716, which only addresses the formatting of comma-expressions, where the "comma operator", that exists in JavaScript/ECMAScript (like C and C++) in parenthesized subexpressions or in expression statements (all of them not tolerated in Java) still generates an incorrect parsing error wherever it is found in Javascript/ECMAScript sources.

Here we are handling another context, where the comma is not used in comma-expressions, but in array/object initializers, where it is effectively recognized as such, except that this current bug is related to the presence of a "superfluous" comma at end of the list of expressions for the initializer: this is not accepted in Java, but perfectly valid in Javascript/ECMAScript (like also in most C and C++ compilers).

This should NOT generate an error, but may only an optional warning for "code beautifiers" (such as the one integrated in the EDT environment when editing Javascript/ECMASCript files), only when working in paranoid mode (because a few bogous Javascript implementations may sometime shoke on the presence of extra commas at end of initializers list, i.e. in constructors of arrays between [], or objects between {}.

A simple code reformatting should probably not even remove those extra final commas.
Comment 4 Sam Hanes CLA 2012-01-25 17:29:00 EST
As Philippe points out, trailing commas in initializers are valid according to the ECMAScript standard. I prefer to use them because then all entries in the initializer have the same format so reordering them doesn't cause missing comma errors. Unfortunately, some poor implementations (most notably IE7) don't support them so they have to be avoided in portable code.

I'd think this should be added to the list of validation problems whose severity is configurable in Preferences -> JavaScript -> Validator -> Errors/Warnings.
Comment 5 Lin ZuXiong CLA 2012-02-05 21:19:36 EST
I think the authors of JSDT donot know Javascript Language.

I suggest let us close the javascript validation. 
But eclipse jee 3.7.1 canot close JS validation.I close the Preference of
javascript validation and make the level be Ignore ,but eclipse Problems still 
reports the problems (errors and warnings) of Javascript type .

Guys , you all can import a js file ,like JQuery-1.x.min.js , so that you can
find too many errors.
Comment 6 Lin ZuXiong CLA 2012-02-06 00:31:30 EST
One way to ignore the annoying errors is that :
1.remove javascript -> validator -> errors/warning.
2.remove project -> .project file -> javascript command
3.It is important,too! delete js file and import again.
Comment 7 Philippe Verdy CLA 2012-10-02 04:49:33 EDT
The suggestion to remove the validation is very poor. True ECMAScript syntax errors  should still be detected as "errors" and not blindly disabled.
trailing commas in initializers lists should only be a warning (only for compatibility with old Javascript implementations that are not conforming to the ECMAScript standard).

This warning should be off by default, whilst true syntax errors should be on be default.

Once again I request a change of classification by NOT parsing initializer lists as if they were comma expressions (they are not). This requires a very modest change in the syntax analyzer, so that they are parsed differently and the code colorizer works properly on code which is tehnically correct and standard (even if it would bug on old Javascripts for which most developers no longer work for as a primary goal).

If needed the developers can enable this specific warning in Eclipse only for compatibility with old Javascript parsers, if their project need this compatibility, or specifically treat it as an error by an explicit preference setting for their specific project :

this should require a separate setting.
Comment 8 Philippe Verdy CLA 2014-03-18 10:25:31 EDT
3 years after reporting it, and still no progress... still marked for a "future milestone" when it is a request for very simple change to fix the JavaScript/ECMAscript parser that incorrectly confuses initializers with comma-expressions.

Trailing commas in initializers is definitely not an error but in fact a recommended style, they are part of the language standard.
OJ ou may drop them in a compresses javascript version, but these compresses version are built automatically by tools and not intended to be editable. The editable versions will include correct indentation, trailing commas and non obfuscated local variables.

Eclipse still should not warn on these commas, and not on standard packages of jQuery
Comment 9 Victor Rubezhny CLA 2016-09-19 18:11:56 EDT
Closed as outdated