gremo
gremo

Reputation: 48439

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

ESLint

Parsing error: Unexpected token ...

Honestly, I can't understand why ESlint is adding the error for the first console.log statement and not for the second:

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

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

Is there any explanation or it's my fault?

ESlint (v7.2.0) configuration:

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

Upvotes: 0

Views: 1234

Answers (1)

Keith
Keith

Reputation: 24221

As mentioned in a comment to the answer, Object spread was introduced in ES2018.

So, if you're using more modern ES features, remember to update the "ecmaVersion" inside your config for this. In this case, you need "ecmaVersion": 9.

For a reference here is the full list:

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

Upvotes: 3

Related Questions