Eslint无法解析并突出显示可选的链接(?)和无效合并(??)运算符 [英] Eslint fails to parse and red-highlights optional chaining (?.) and nullish coalescing (??) operators

查看:1712
本文介绍了Eslint无法解析并突出显示可选的链接(?)和无效合并(??)运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找


  • @ babel / plugin-proposal-optional-chaining
  • $ b的相关陪同规则$ b
  • @ babel / plugin-proposal-nullish-coalescing-operator

  • @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);

代码可以正常工作,但是eslint用红色突出显示我的代码。

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

参考:

  • https://babeljs.io/docs/en/babel-plugin-proposal-optional-chaining
  • https://babeljs.io/docs/en/babel-plugin-proposal-nullish-coalescing-operator

推荐答案

eslint> = 7.5.0 开始,自然支持空位合并运算符。

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

最简单的方法是在package.json中设置ES2020:

The easiest is set ES2020 in your package.json:

{
  "eslintConfig":
  {
    "parserOptions":
    {
      "ecmaVersion": 2020
    }
  }
}

这篇关于Eslint无法解析并突出显示可选的链接(?)和无效合并(??)运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆