ESlint和对象在函数或const内部传播运算符,为什么会抱怨呢?

时间:2020-06-16 13:18:17

标签: javascript eslint

ESLint

解析错误:意外的令牌...

老实说,我不明白为什么ESlint会为第一个console.log语句而不是第二个语句添加错误:

const generateCss = () => {
  console.log({...{foo: 'bar'}});
};

function bar() {
  console.log({...{foo: 'bar'}});
}

有什么解释吗?或者是我的错?

ESlint(v7.2.0)配置:

{
  "extends": "eslint:recommended",
    "env": {
      "es6": true,
      "node": true,
      "mocha": true
    },
    "parserOptions": {
      "ecmaVersion": 6,
      "sourceType": "module"
    },
    "rules": {
      "array-bracket-spacing": "warn",
      "arrow-parens": ["warn", "as-needed"],
      "arrow-spacing": "warn",
      "brace-style": "warn",
      "comma-spacing": "warn",
      "computed-property-spacing": "warn",
      "consistent-return": "warn",
      "curly": ["warn", "all"],
      "eol-last": "warn",
      "eqeqeq": "warn",
      "func-call-spacing": "warn",
      "indent": ["warn", 2, {
        "SwitchCase": 1
      }],
      "key-spacing": ["warn"],
      "new-cap": "warn",
      "new-parens": "warn",
      "no-multiple-empty-lines": ["warn", {
        "max": 1
      }],
      "no-nested-ternary": "warn",
      "no-return-assign": ["warn", "always"],
      "no-trailing-spaces": "warn",
      "no-unneeded-ternary": "warn",
      "no-var": "warn",
      "object-curly-spacing": ["warn", "always"],
      "padded-blocks": ["warn", "never"],
      "prefer-const": "warn",
      "quote-props": ["warn", "as-needed"],
      "quotes": ["warn", "single"],
      "semi-spacing": "warn",
      "space-before-blocks": ["warn", "always"],
      "space-before-function-paren": ["warn", {
        "anonymous": "always",
        "asyncArrow": "always",
        "named": "never"
      }],
      "space-in-parens": "warn",
      "space-infix-ops": ["warn", {
        "int32Hint": true
      }],
      "space-unary-ops": "warn",
      "spaced-comment": ["warn", "always"],
      "yoda": ["warn", "always", {
        "onlyEquality": true
      }]
    }
}

1 个答案:

答案 0 :(得分:2)

正如在对答案的评论中提到的那样,对象传播在ES2018中引入。

因此,如果您使用的是更现代的ES功能,请记住为此在配置中更新"ecmaVersion"。在这种情况下,您需要"ecmaVersion": 9

完整列表如下:

ES3 -> 3
ES5 -> 5
ES2015 -> 6
ES2016 -> 7
ES2017 -> 8
ES2018 -> 9
ES2019 -> 10
ES2020 -> 11