38

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:

Paul
  • 26,170
  • 12
  • 85
  • 119
Adeel Imran
  • 13,166
  • 8
  • 62
  • 77

6 Answers6

59

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
    }
  }
}
Dima Tisnek
  • 11,241
  • 4
  • 68
  • 120
8

add the following config to your eslint:

"parser": "babel-eslint"
Gideao
  • 374
  • 3
  • 5
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.

Mario Beltrán
  • 521
  • 3
  • 6
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"]
}
Myzel394
  • 1,155
  • 3
  • 16
  • 40
makkabi
  • 619
  • 6
  • 10
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"
]
AlexMelw
  • 2,406
  • 26
  • 35
-1

Adding

parserOptions: {
  ecmaVersion: "latest",
},

to .eslintrc.js did the trick for me

Katlego
  • 39
  • 5
  • 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](/help/how-to-answer). – Community Jan 18 '23 at 17:34
  • 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. – AlexMelw Feb 17 '23 at 07:32