简体   繁体   English

eslint - vscode 的可选链接错误

[英]eslint - Optional chaining error with vscode

I am seeing aa red underline when I'm using an optional chain, but the code runs fine as I am on node 14当我使用可选链时,我看到一条红色下划线,但代码运行良好,因为我在节点 14 上

Here's my setup:这是我的设置:

node 14.1.0
eslint "^6.8.0"

.eslintrc.js .eslintrc.js

module.exports = {
    "env": {
        "node": true
    },
    "extends": [
        "eslint:recommended",
    ],
    "parserOptions": {
        "sourceType": "module",
        "ecmaVersion": 2020
    },
    "rules": {
    },
}

在此处输入图像描述

You no longer need @babel/eslint-parser as eslint@^7.5 now supports optional chanining.你不再需要@babel/eslint-parser ,因为eslint@^7.5现在支持可选的 chanining。

Run the following to update eslint within your project:运行以下命令来更新项目中的 eslint:

npm npm

npm install --save-dev eslint@^7.5

yarn

yarn add -D eslint@^7.5

And then, ensure your config is as follows:然后,确保您的配置如下:

.eslintrc .eslintrc

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

.eslint.js .eslint.js

module.exports = {
    "parserOptions": {
        "ecmaVersion": 2020
    }
}

See https://eslint.org/blog/2020/07/eslint-v7.5.0-released#optional-chaining-support for more information.有关更多信息,请参阅https://eslint.org/blog/2020/07/eslint-v7.5.0-released#optional-chaining-support

You should use @babel/eslint-parser (formerly called babel-eslint ) with your eslint config.你应该在你的 eslint 配置中使用@babel/eslint-parser (以前称为babel-eslint )。 This allows you to lint ALL valid Babel code with eslint.这允许您使用 eslint 对所有有效的 Babel 代码进行 lint。 eslint supports all ES2020 features as of version v7.2.0 eslint 支持从 v7.2.0 版本开始的所有 ES2020 功能

$ npm install @babel/eslint-parser --save-dev
# or
$ yarn add @babel/eslint-parser -D

Then in your .eslintrc do:然后在你的.eslintrc中做:

{
  parser: "@babel/eslint-parser",
}

First, you should have a ESLint parser that supports the optional chaining:首先,您应该有一个支持可选链接的 ESLint 解析器:

npm install -D @babel/eslint-parser

If you're facing issues with peer dependencies then run by appending --legacy-peer-deps to the command.如果您遇到对等依赖项的问题,请通过将--legacy-peer-deps附加到命令来运行。

Then, you should have the ESLint version that supports the optional chaining.然后,您应该拥有支持可选链接的 ESLint 版本。 This is that release version(7.5.0): 是发布版本(7.5.0):

npm install eslint@^7.5

Tell your ESLint server to use the above-installed parser:告诉你的 ESLint 服务器使用上面安装的解析器:

{
  "parserOptions": {
    "ecmaVersion": 2020
  }
  ...
}

Not all JavaScript features enabled by Babel are included in ESLint.并非所有由 Babel 启用的 JavaScript 功能都包含在 ESLint 中。

You can use babel-eslint though:你可以使用babel-eslint

$ npm install @babel/eslint-parser --save-dev
# or
$ yarn add @babel/eslint-parser -D

Then in your .eslintrc do:然后在你的.eslintrc中做:

{
  parser: "@babel/eslint-parser",
}

All versions of Node.js above 12 support the optional-chaining operator (a ECMAScript 2020 feature).所有高于 12 的 Node.js 版本都支持可选链运算符(ECMAScript 2020 功能)。 Try to use this:尝试使用这个:

"parserOptions": {
    "ecmaVersion": 2020
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM