简体   繁体   English

有没有办法让 ESLint 在不添加 Babel 的情况下理解新的导入断言语法?

[英]Is there a way to make ESLint understand the new import assertion syntax without adding Babel?

Since the import assertion syntax (eg assert {type: 'json'} ) is mandatory in the latest Node.js versions, I'm looking for a way to be able to write this new syntax, but I don't want to add Babel just to be able to use the Babel ESLint parser which understands it.由于导入断言语法(例如assert {type: 'json'} )在最新的 Node.js 版本中是强制性的,我正在寻找一种能够编写这种新语法的方法,但我不想只添加 Babel能够使用理解它的 Babel ESLint 解析器。

Could this be added by a different ESLint plugin?这可以通过不同的 ESLint 插件添加吗?

As of this writing, the Node.js core code itself is configured to use @babel/eslint-parser specifically for this .在撰写本文时,Node.js 核心代码本身已配置为专门为此使用@babel/eslint-parser It is unlikely that there is an easy and reliable way to do it using a parser shipped with ESLint (at this time).使用 ESLint 附带的解析器(目前)不太可能有一种简单可靠的方法来完成它。 As of this writing, the specification is Stage 3 .在撰写本文时,规范处于第 3 阶段 ESLint has a policy of only adding Stage 4 syntax. ESLint 的策略是只添加 Stage 4 语法。 Additionally, Node.js docs and a runtime warning make it clear that this is an experimental API.此外,Node.js 文档和运行时警告清楚地表明这是一个实验性 API。 Installing @babel/eslint-parser is the reasonable solution if you're using an experimental API.如果您使用的是实验性 API,安装@babel/eslint-parser是合理的解决方案。

You have to use the babel eslint parser for that必须为此使用 babel eslint 解析器

npm i -D @babel/eslint-parser @babel/plugin-syntax-import-assertions

then in.eslintrc然后在.eslintrc

  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "requireConfigFile": false,
    "babelOptions": {
      "plugins": [
        "@babel/plugin-syntax-import-assertions"
      ]
    }
  },

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

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