【问题标题】:Eslint allow multiple parsers based on glob patternsEslint 允许基于 glob 模式的多个解析器
【发布时间】:2020-03-02 18:08:26
【问题描述】:

我的 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

我怎样才能这样设置多个解析?

【问题讨论】:

  • @angular-eslint/template-parser@html-eslint/parser 存在同样的问题。我能想到的唯一解决方法是使用两个不同的 eslint 配置文件运行两个命令。但遗憾的是,我看不到将两种解析器都放入我的代码编辑器的解决方案。

标签: babeljs eslint typescript-eslint


【解决方案1】:

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

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

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

这取决于……

你的 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

【讨论】:

    【解决方案2】:

    来自Configuration Based on Glob Patterns

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

    【讨论】:

      猜你喜欢
      • 2015-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-04
      • 1970-01-01
      • 2016-02-15
      相关资源
      最近更新 更多