在 .eslintrc 中为 @babel/eslint-parser 声明 babel 插件

cyq*_*mon 9 javascript plugins eslint babeljs

我一直在尝试了一段时间,现在得到@babel/plugin-proposal-class-properties与插件的工作很好@babel/eslint-parser,并eslint没有成功。

这是我的.eslintrc.js

...
  "parser": "@babel/eslint-parser",
  "parserOptions": {
    "ecmaVersion": 11,
    "requireConfigFile": false,
  },
  "plugins": [
    "@babel",
  ],
...
Run Code Online (Sandbox Code Playgroud)

这些是相关的安装包:

+-- @babel/core@7.11.1
+-- @babel/eslint-parser@7.11.3
+-- @babel/eslint-plugin@7.11.3
+-- @babel/plugin-proposal-class-properties@7.10.4
+-- eslint@7.7.0
Run Code Online (Sandbox Code Playgroud)

在此配置下,ESLint 错误并显示以下消息:

Parsing error: \eg\example.js: Support for the experimental syntax 'classPrivateProperties' isn't currently enabled (xx:yy): (Fatal)
Run Code Online (Sandbox Code Playgroud)

但是,如果我加入@babel/plugin-proposal-class-propertiesplugins.eslintrc.js这样的:

  "plugins": [
    "@babel",
    "@babel/plugin-proposal-class-properties",
  ],
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Error while running ESLint: Failed to load plugin '@babel/plugin-proposal-class-properties' declared in '.eslintrc.js': Cannot find module '@babel/eslint-plugin-plugin-proposal-class-properties'.
Run Code Online (Sandbox Code Playgroud)

这似乎不是为@babel/eslint-parserin声明插件的正确方法.eslintrc.js。尽管如此,我怀疑这是可能的,因为这里引用

@babel/eslint-parser 还支持通过 ESLint 配置应用 Babel 配置。

所以我的问题是:

是否真的可以在 中声明 babel 插件.eslintrc?如果是这样,具体如何?

cyq*_*mon 8

其实比我想象的要简单...

所以事实证明,既然@babel/plugin-proposal-class-properties是babel插件,就需要在pluginsbabel的配置属性中声明。根据的文档@babel/eslint-parser,这些可以通过babelOptions属性传入。

因此它真的很简单:

...
  "parserOptions": {
    ...
    "babelOptions": {
      "plugins": [
        "@babel/plugin-proposal-class-properties",
        ...
      ],
    },
  },
  "plugins": [
    "@babel",
  ],
...
Run Code Online (Sandbox Code Playgroud)