3

I was writing some Javascript when I found out about optional chaining (?.). I decided that I would need it in some code I was writing. When I finished typing out the code, I noticed that JSHint was giving me an error that stated Expected an identifier and instead saw '.'. (E030) jshint(E030). The following code runs without any error (MDN compatibility table), but JSHint still gives a warning for it.

var x = {
  y: {
    z: 123
  }
};

console.log(x.y?.z)

I have found another StackOverflow question relating to this, but the question specifically asks about ESLint, while this question is about JSHint. I also searched the issues tab of the JSHint GitHub repository, but I couldn't find anything. Is there any way to suppress this kind of error? I am using Visual Studio Code Insiders.

The editor information taken from Code - Insiders > About Visual Studio Code - Insiders:

Version: 1.48.0-insider
Commit: d13d2fc56da7a2f8bcad4256212db0661fcbba45
Date: 2020-08-05T05:26:44.946Z (20 hrs ago)
Electron: 7.3.2
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0
OS: Darwin x64 19.5.0
shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34

3 Answers3

5

Add: "esversion": 11 to your jshint file. Optional chaining was added in version 11.

Logan Cundiff
  • 479
  • 8
  • 13
3

There is an issue open for this in https://github.com/jshint/jshint/issues/3448.

It suggests "You can use the ignore:start/ignore:end or ignore:line directives to cause JSHint to pass over any syntax it doesn't recognize."

Anita Graham
  • 456
  • 1
  • 5
  • 13
1

It doesn't seems to work when upgraded by yarn

node -v
v16.2.0

.jshint in project folder

cat .jshintrc 
{
    "-W138": true,
    "-W083": true,
    "esversion": 11
}

from package.json:

"grunt-contrib-jshint": "^3.0.0",
"jshint": "^2.13.1",

from gruntfile.js

jshint: {
    options: {
        "-W138": true,
        "-W083": true,
        "jshintrc": true,
        esversion: 11
    },
    files: ['src/js/**/*.js']
},

result:

grunt jshint:files
if (response?.errCode != 200) {
             ^ Expected an identifier and instead saw '.'.

some investignation:

node_modules/grunt-contrib-jshint/node_modules/jshint/package.json
"repository": {
    "type": "git",
    "url": "https://github.com/jshint/jshint.git"
},

in repository is proper (2.13.1) version of jshint but in my project it isn't updated (still have 2.12.0)

solution #1:

throw new Exception(response?.errMessage); /* jshint ignore:line */

solution #2:

cd node_modules
cp -Rp jshint grunt-contrib-jshint/node_modules/
Marek Lisiecki
  • 498
  • 6
  • 10