2

我现在的 eslint 解析器是@typescript-eslint/parser. 我想使用@babel/plugin-proposal-optional-chaining需要babel-eslint解析器的插件。

我看到了,eslint-multiple-parsers但它说它已被弃用: 使用基于 glob 模式的 ESLint 配置(overrides)。请参阅https://eslint.org/docs/user-guide/configuring/#configuration-based-on-glob-patterns

如何以这种方式设置多个解析?

4

2 回答 2

8

来自基于 Glob 模式的配置

glob 特定配置的工作方式几乎与任何其他 ESLint 配置相同。覆盖块可以包含在常规配置中有效的任何配置选项,root 和 ignorePatterns 除外。

在您的 eslint 配置文件中,您可以添加一个overrides作为对象数组的部分。每个对象都需要有files定义 glob 模式的键。任何匹配的文件都将使用覆盖的配置。例子:

{
    // estree parser
    "env": {
        "es6": true
    },
    "extends": [
        "eslint:recommended",
        "plugin:security/recommended"
    ],
    "parserOptions": {
        "ecmaVersion": 2018,
        "sourceType": "module",
        "ecmaFeatures": {
            "jsx": true
        }
    },
    "plugins": [
        "security"
    ],
    "rules": {
        "indent": [ "error", 4 ]
    },
    // rest of your "normal" configuration here

    "overrides": [{
        // for files matching this pattern
        "files": ["*.ts"],
        // following config will override "normal" config
        "parser": "babel-eslint",
        "parserOptions": {
            // override parser options
        },
        "plugins": [
            "@babel/plugin-proposal-optional-chaining"
        ],
        "rules": [
            // override rules
        ],
    },
    }]
}

但是,如果您已经使用了@typescript-eslint/parser,那么您可能已经匹配 *.ts 文件,并且覆盖只会使每个 *.ts 文件babel-eslint改为使用,这并不能解决您的问题。

我假设您希望两个解析器(typescript-eslint 和 babel)都针对同一个文件运行,但我不知道简单的解决方案。

于 2020-03-13T14:15:39.410 回答
0

您的目标是针对文件 X 运行解析器 A,针对文件 Y 运行解析器 B?

@enterthenamehere-bohemian's anwser是解决方案。

您的目标是针对同一个文件运行两个解析器吗?

这取决于……</p>

您的 eslint 是否在您的编辑器中运行?

截至今天,我在单个 eslint 配置文件中看不到任何解决方案。如果要在编辑器中运行它,则需要一个配置文件。

您的 eslint 是在命令行还是持续集成环境中运行?

您需要三个配置文件。

文件.eslintrc.json

{
    "parser": "@typescript-eslint/parser",
    "extends": ["./eslint-common-rules.json"],
    "rules": {
        …typescript parser rules here
    }
    …
}

文件.eslintrc.babel.json

{
    "parser": "babel-eslint",
    "extends": ["./eslint-common-rules.json"],
    "rules": {
        …babel parser rules here
    }
    …
}

文件eslint-rules.json

{
    "rules": { … 
        …common eslint rules here
    }
    …
}

有了它,你可以在你的命令行中运行这两者:

eslint # <- runs .eslintrc.json
eslint --config .eslintrc.babel.json
于 2022-02-10T08:20:24.540 回答