最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JSHint suppress error for optional chaining - Stack Overflow

programmeradmin10浏览0评论

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 patibility table), but JSHint still gives a warning for it.

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

console.log(x.y?.z)

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 patibility 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
Share Improve this question edited Aug 6, 2020 at 2:22 shreyasm-dev asked Aug 6, 2020 at 2:03 shreyasm-devshreyasm-dev 2,8445 gold badges22 silver badges39 bronze badges 2
  • That doesn't look like a warning, more like a plain syntax error where jshint cannot parse this recently added syntax. – Bergi Commented Aug 6, 2020 at 2:04
  • @Bergi Okay, I will edit the question and change "warning" to "error" – shreyasm-dev Commented Aug 6, 2020 at 2:05
Add a ment  | 

3 Answers 3

Reset to default 6

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

There is an issue open for this in https://github./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."

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./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/
发布评论

评论列表(0)

  1. 暂无评论