23

I'm trying to use node-jslint https://github.com/reid/node-jslint in order to keep my code clean

I've got a const in my nodejs script, but jslint says it isn't valid ES6 code

 Unexpected ES6 feature.
const pdPersonsFilterId = process.argv[2]; // Line 10, Pos 0

Here is the command I use in the console

jslint --edition=latest index.js

According to the https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/const it possible to use global constants.

Why does jslint does not consider this code to be valid?

Nikage
  • 578
  • 2
  • 7
  • 18
  • es6 is not 'the latest working version' of javascript. – mattdevio Mar 19 '16 at 16:27
  • @magreenberg: It's not? –  Mar 19 '16 at 16:28
  • No, browsers are not up to speed with the new es6 features, they are in development still. You can use them in specific cases, but are not web ready. – mattdevio Mar 19 '16 at 16:29
  • @magreenberg: ES2015 (ES6) is indeed the latest version of JavaScript. Separately, the question above is about JavaScript code running under NodeJS. Not a browser in sight. – T.J. Crowder Mar 19 '16 at 16:29
  • 1
    @magreenberg: So they define it as some arbitrary level of browser adoption? Weird. I'd think the developer could make that determination on their own. But then it is jsLint, so I keep my expectations low. –  Mar 19 '16 at 16:30
  • http://i.imgur.com/T5Q6p7j.jpg stiill does not work – Nikage Mar 19 '16 at 16:48
  • 1
    @Nikage I'd suggest [ESLint](http://eslint.org/) for linting ES6, at least until JSLint has full support for ES6 features. – twernt Mar 21 '16 at 14:51

2 Answers2

28

EDIT in 2020: As ctrl-alt-delar mentions in a comment, and as predicted in the answer, JSLint has dropped the es6 requirement -- it looks like on 9 Oct 2017.

That is, es6 is no longer a valid JSLint option. The OP's code today would lint as written here:

/*jslint browser */
/*global process */
const pdPersonsFilterId = process.argv[2];

If you think you're in the OP's situation, however, ensure that whatever process you use to lint your files isn't using an older version of JSLint. Some tools ship with outdated versions, or perhaps your build script maintains an older version so as not to break legacy code. If you're in this situation, the fix below should work.

But if you know you have a version of JSLint that is newer than 9 Oct 2017 and you have what appears to be an es6 error, please open a new StackOverflow question!


For the original question/older versions of JSLint...

JSLint is happy enough with ES6; you just have to let it know you're using ES6. Add the es6 directive to your JSLint config or atop your file, and profit.

/*jslint es6 */
const pdPersonsFilterId = process.argv[2];

Now the warning you saw goes away.

From JSLint's help:

It may take time for the sixth edition of ECMAScript [ES6] to reach ubiquity. Using the new features in enviroments that do not fully implement the new standard will result in failure. This is why JSLint gives warnings when ES6 features are used. Some of ES6's features are good, so JSLint will recognize the good parts of ES6 with the es6 option. As implementations of the new standard become more stable and better understood, the set of features recognized by JSLint may increase. After the transition to ES6 is complete, the es6 option will be dropped. [emph mine]

Seems fair enough. So what you saw was just warning you that what you've got might not work where ES6 isn't supported, since that's a lot of places right now. Once ES6 is more widespread -- or if you explicitly let Crockford know you intend to use ES6 -- the warning will go/goes away. (TJ's point might be that, at least with Node, the time to remove the warning is now. ;^D)

ruffin
  • 16,507
  • 9
  • 88
  • 138
  • 3
    @T.J.Crowder I think you have the [node wrapper](https://github.com/reid/node-jslint) to blame there (there's [an issue for this](https://github.com/reid/node-jslint/issues/147)), not Crockford. Painfully, it looks like the node wrapper still uses [JSLint from Oct 2015 for ES6](https://github.com/reid/node-jslint/blob/master/lib/jslint-es6.js), which doesn't have the `es6` option. Crockford doesn't (afaik) release versions of JSLint beyond [the "canonical" online version](http://jslint.com) and the [raw code on GitHub](https://github.com/douglascrockford/JSLint/blob/master/jslint.js). – ruffin Mar 29 '16 at 13:24
  • I think you're right. :-) I can't imagine why I didn't twig to that. – T.J. Crowder Mar 29 '16 at 13:50
  • Wrapper now has an updated version, fwiw - but you may need --edition=es6 to work with es6 code – Sam Mikes May 16 '16 at 15:04
  • 4
    How do I add this to a config file? (I believe the file name should be `.jslintrc`, but `{"es6":true}` does nothing) – theonlygusti Aug 21 '16 at 08:59
  • ```$ jslint --version node-jslint version: 0.11.0 JSLint edition 2013-08-26``` With that version ES6 does not work, what the OP said holds true – mrArias Dec 15 '17 at 08:02
  • @mrArias What error are you receiving? What's your code? **Your paste suggesets you have `JSLint edition 2013-08-26` which doesn't support the es6 option**, iirc. If you upgraded to a version concurrent with the question, I bet what I have above works. If you want a workaround for a 2013 edition of JSLint, you've probably got a new question. Make sense? If you can, let me know what happens when you update jslint. *Also keep in mind that all es6 options are **not** supported in JSLint, [by design](http://jslint.com/help.html#es6)*, which is why seeing your code would help. – ruffin Dec 15 '17 at 15:34
  • Indeed it's not the same version, my bad; I got my (really old) version off a brew-installed npm; the error I got was the same as the OP: ` Unexpected ES6 feature.` Thanks! – mrArias Dec 18 '17 at 10:59
  • I now get an error on the comment `/*jslint es6 */` `#1 Unexpected 'es6'. /*jslint es6 */ // Line 2, Pos 10` – ctrl-alt-delor Jun 08 '19 at 11:55
  • @ctrl-alt-delor Fair enough and I've updated the answer to be very specific on this point. As I mentioned, "**_Once ES6 is more widespread... the warning will go/goes away._**" The flip-side of this, however is that a version of JSLint that doesn't support the `es6` option _won't have the issue the OP needed resolved_. If you have a _new_ question where you think JSLint isn't recognizing an es6 feature, [take a look at its howto](https://jslint.com/help.html) and then feel free to create a new SO question. Hope this helps. – ruffin Sep 08 '20 at 17:03
17

Try out ESLint.

It has better stats on NPM, the documentation is brilliant and it's widely used.

czerasz
  • 13,682
  • 9
  • 53
  • 63