Eslint允许基于glob模式的多个解析器 [英] Eslint allow multiple parsers based on glob patterns

查看:115
本文介绍了Eslint允许基于glob模式的多个解析器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My eslint parser as for now is @typescript-eslint/parser. I want to use @babel/plugin-proposal-optional-chaining plugin, which requires babel-eslint parser.

我看到了 eslint-multiple-parsers ,但它说它已被弃用:使用基于全局模式的ESLint配置(替代).参见 https://eslint.org/docs/用户指南/配置/#基于全局模式的配置 .

I saw the eslint-multiple-parsers but it says that it was deprecated : Use ESLint configuration based on glob patterns (overrides). See https://eslint.org/docs/user-guide/configuring/#configuration-based-on-glob-patterns.

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

How can I set multiple parses that way?

推荐答案

来自基于全局模式的配置

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

A glob specific configuration works almost the same as any other ESLint config. Override blocks can contain any configuration options that are valid in a regular config, with the exception of root and ignorePatterns.

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

In your eslint config file you can add an overrides section which is an array of objects. Each object is required to have files key where you define the glob pattern. Any file which match will then use the overriden config. Example:

{
    // 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 ,但这并不能解决您的问题.

However if you already use @typescript-eslint/parser then you probably already match *.ts files, and overriding would only make every *.ts file use babel-eslint instead, which doesn't solve your issue.

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

I assume you want both parsers (typescript-eslint and babel) to run against same file, but I don't know easy solution for that.

这篇关于Eslint允许基于glob模式的多个解析器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆