繁体   English   中英

ESlint 和 object 在 function 或 const 中传播运算符,为什么抱怨?

[英]ESlint and the object spread operator inside a function or a const, why is complaining?

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
      }]
    }
}

正如对答案的评论中提到的,Object spread 是在 ES2018 中引入的。

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

参考这里是完整列表:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM