41

I am looking for the relevant eslint rules for

  • @babel/plugin-proposal-optional-chaining
  • @babel/plugin-proposal-nullish-coalescing-operator

My editor highlights in red when I do the following

const baz = obj?.foo?.bar?.baz; // 42
const safe = obj?.qux?.baz; // undefined
const foo = obj.baz ?? 'default'; // default
// eslint-disable-next-line no-console
console.log('baz', baz);
// eslint-disable-next-line no-console
console.log('safe', safe);
// eslint-disable-next-line no-console
console.log('foo', foo);

The code works properly, but eslint highlights my code in red.

Reference:

6 Answers 6

62

Nullish coalescing operator is natively supported starting from eslint>=7.5.0.

The easiest is set ES2020 in your package.json:

{
  "eslintConfig":
  {
    "parserOptions":
    {
      "ecmaVersion": 2020
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

You probably mean ecmaVersion, not exmaVersion?
Looks like canonical is ecmaVersion: 11 or 12, while 2020 and 2021 are de facto accepted as aliases: eslint.org/docs/user-guide/configuring/language-options
For me I found "parserOptions" in .eslintrc.json and resolved this problem
7

add the following config to your eslint:

"parser": "babel-eslint"

1 Comment

More Context: Create .eslintrc.json Add {"parser": "babel-eslint"} Save
1

Have you tried setting the parser on your eslint config to "babel-eslint"? https://www.npmjs.com/package/babel-eslint It's the recommended parser when using experimental features not supported in eslint yet.

1 Comment

This throws up tons of typescript errors when i change from @typescript-eslint/parser
0

You have to use this plugin: https://github.com/babel/eslint-plugin-babel

You can then disable the original eslint-rule and enable the babel version of it, which will then show no false errors. Your eslint-config could then look like this:

{
  parser: "babel-eslint",
  rules: {
    "no-unused-expressions": 0,
    "babel/no-unused-expressions": 1
  },
  plugins: ["babel"]
}

Comments

0

Adding

parserOptions: {
  ecmaVersion: "latest",
},

to .eslintrc.js did the trick for me

3 Comments

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
Hi @Katlego, I had to lower the rating of this answer because writing ecmaVersion: "latest" doesn't solve the issue but creates a bigger one. The trailing comma after the word "latest" crashes the ESLint configuration as it's JSON, not JS. The parsing of the .eslintrc.json file is broken, and as a result, ESLint just stopped working. That's why the red squiggly line disappeared.
I would just like to add that this answer worked for me. And as @Katelgo specifically mentioned, they are using .eslintrc*.js*, which is Javascript, so trailing commas and lack of quotes are fine.
0

As of 2021 after babel-eslint became decprecated, you can fix this issue by upgrading to @babel/eslint-parser.

The configuration needed to be added to .eslintrc.json is:

"parser": "@babel/eslint-parser",
"parserOptions": {
    "requireConfigFile": false,
    "presets": [
         "@babel/preset-env"
     ]
}

"requireConfigFile": false, - you need this line if you don't have a dedicated eslint config file

Also, make sure to upgrade to eslint versin 7 and above, and remove babel-eslint as it's been replaced by @babel/eslint-parser.

Setting the babel parser's options, like "plugins": [ "jsx" ] or "plugins": [ "typescript" ] are required only if you do use react or typescript.

"babelOptions": {
      "parserOpts": {
          "plugins": [ "jsx", "typescript" ]
      }
  }

So if you also need React, your final config changes could look like this:

"parser": "@babel/eslint-parser",
"parserOptions": {
    "requireConfigFile": false,
    "presets": [
        "@babel/preset-env",
        "@babel/preset-react"
    ],
    "babelOptions": {
        "parserOpts": {
            "plugins": [ "jsx" ]
        }
    }
},
"plugins": [
    "react",
    "react-hooks"
]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.