Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eslint fails to parse and red-highlights optional chaining (?.) and nullish coalescing (??) operators

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:

  • https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
  • https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator
like image 543
Adeel Imran Avatar asked Aug 06 '19 14:08

Adeel Imran


3 Answers

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
    }
  }
}
like image 58
Dima Tisnek Avatar answered Sep 29 '22 02:09

Dima Tisnek


add the following config to your eslint:

"parser": "babel-eslint"
like image 36
Gideao Avatar answered Sep 29 '22 01:09

Gideao


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.

like image 25
Mario Beltrán Avatar answered Sep 29 '22 01:09

Mario Beltrán