Eslint允许基于glob模式的多个解析器

到目前为止,我的eslint解析器是@typescript-eslint/parser。我想使用@babel/plugin-proposal-optional-chaining插件,它需要babel-eslint解析器。

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

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

okgame 回答:Eslint允许基于glob模式的多个解析器

Configuration Based on Glob Patterns

特定于全局的配置的工作原理与任何其他ESLint配置几乎相同。覆盖块可以包含常规配置中有效的任何配置选项,但root和ignorePatterns除外。

在您的eslint配置文件中,您可以添加overrides节,该节是对象数组。每个对象都必须具有files键,以在其中定义全局模式。然后,任何匹配的文件都将使用替​​代配置。示例:

{
    // 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)都针对同一个文件运行,但是我不知道简单的解决方案。

本文链接:https://www.f2er.com/3153470.html

大家都在问