JsHint 显示 es6 对象解构分配错误

JsHint showing error with es6 object descructuring assignment

我有以下测试代码

const sth = {
  one: 'one',
  two: 'two',
  three: {
    foo: 'foo',
    bar: 'bar',
  }
};

const state = {
  ...sth
};

当我 运行 jshint 时,我得到以下输出

jshint --config .jshintrc test.js
test.js: line 12, col 3, Expected '}' to match '{' from line 11 and instead saw '...'.
test.js: line 12, col 6, Missing semicolon.
test.js: line 12, col 9, Missing semicolon.
test.js: line 12, col 6, Unrecoverable syntax error. (92% scanned).

4 errors

但这是完全有效的 es6 代码。

我的 .jshintrc 如下所示:

{
  "curly": false,
  "expr": true,
  "maxlen": 200,
  "esversion": 6
}

是否有我缺少的魔法设置才能通过?

您正在使用 Object Rest/Spread Properties,它不在 ES6 中,甚至还不是标准。目前处于第 3 阶段。

JSHint does not support Object Rest/Spread Properties, yet. You could replace JSHint with ESLint which has experimental support using experimentalObjectRestSpread.

或者,当然,您可以按照 sabareesh 的建议忽略这些行。但是,就我个人而言,我建议不要这样做。它会污染代码库,还会禁用后续的 linting。我建议改用 ESLint,因为它通常对新功能有更好的支持,而且似乎更受欢迎。

 var state =  {
    // jshint ignore:start
    ...sth,
   // jshint ignore:end
  }

试试这些。

您应该在应用的根目录中添加一个名为 .jshintrc 的文件,其中包含以下内容:

{
    "esversion": 9
}

基本上es9之前解构是不行的