Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup Eclipse to be warned about trailing comma in JavaScript

As many of us know, IE7 is not quite friendly with JavaScript code containing trailing commas, which can be a large problem for projects using modern JS framerworks and containing a lot of JS code. In a pretty good article on the subject, the author mentions:

On the tools front, my preference for combating these devils is the Eclipse JavaScript Development Tools. The JavaScript source editor in JSDT flags trailing commas as errors: http://www.enterprisedojo.com/wp-content/uploads/2010/12/jsdtRules.png

However, using Eclipse Indigo with WTP/JSDT, I'm not seeing trailing commas as errors, and I can't find a proper setting to fix this.

How do I setup Eclipse to flag trailing commas in JavaScript as errors?

like image 683
Das Avatar asked Sep 11 '11 22:09

Das


People also ask

Does JavaScript allow trailing commas?

JavaScript has allowed trailing commas in array literals since the beginning. Trailing commas are now also allowed in object literals, function parameters, named imports, named exports, and more.

What is trailing comma JavaScript?

A trailing comma, also known as a dangling or terminal comma, is a comma symbol that is typed after the last item of a list of elements. Since the introduction of the JavaScript language, trailing commas have been legal in array literals. Later, object literals joined arrays.

Is comma required in JavaScript?

The comma operator in JavaScript is really very simple and, to be 100% honest, you never need to use it.

What is comma dangle?

Require or disallow trailing commas. 🛠 Some problems reported by this rule are automatically fixable by the --fix command line option.


2 Answers

It looks like the fix for another bug involving erroneous syntax errors on the comma operator also removed the syntax error on trailing commas in initializers. That's technically correct; the standard says they're allowed and IE7 is just nonconformant. There's a feature request open asking that they be reinstated.

like image 146
Sam Hanes Avatar answered Sep 17 '22 13:09

Sam Hanes


Slightly off topic, but you should also look into using JSLint to check the syntax of the JavaScript code. It will warn you about the trailing comma, but also about many other potential problems. There is a good plugin for Eclipse, http://marketplace.eclipse.org/content/phonegap-android-jslintjshint. The instructions for setting it up: http://www.mobiledevelopersolutions.com/home/announce-1/mds12released-nowwithjslintjshint

like image 38
Karolis Avatar answered Sep 18 '22 13:09

Karolis