繁体   English   中英

ESLint 与 eslint-plugin-import 和 typescript-eslint 冲突

[英]ESLint conflicts with eslint-plugin-import and typescript-eslint

我想从eslint-plugin-node包含规则no-unpublished-import ,但是,它与我当前的.eslintrc冲突,因为我使用的是typescript-eslinteslint-import-resolver-typescript

这是我目前的配置:

{
    "parser": "@typescript-eslint/parser", // Specifies the ESLint parser
    "extends": [
        "airbnb-base",
        "plugin:@typescript-eslint/recommended", // Uses the recommended rules from the @typescript-eslint/eslint-plugin
        "prettier", // Enables eslint-plugin-prettier and displays prettier errors as ESLint errors. Make sure this is always the last configuration in the extends array
        "prettier/@typescript-eslint" // Uses eslint-config-prettier to disable ESLint rules from @typescript-eslint/eslint-plugin that would conflict with prettier
    ],
    "parserOptions": {
        "project": "./tsconfig.json",
        "ecmaVersion": 6, // Allows for the parsing of modern ECMAScript features
        "sourceType": "module" // Allows for the use of imports
    },
    "rules": {
    },
    "settings": {
        "import/resolver": {
            "node": {
                "extensions": [".js", ".ts"]
            },
            // use <root>/tsconfig.json
            "typescript": {
                "alwaysTryTypes": true // always try to resolve types under `<root>@types` directory even it doesn't contain any source code, like `@types/unist`
            }
        }
    },
    "root": true
}

代码编译正确,但是,如果我添加到扩展选项plugin:node/recommended编译过程将失败:

  1:1   error  Import and export declarations are not supported yet  node/no-unsupported-features/es-syntax
  1:43  error  "express" is not found                                node/no-missing-import
  2:1   error  Import and export declarations are not supported yet  node/no-unsupported-features/es-syntax

我的package.json包括node": ">=12.0.0 此外,应该忽略此规则,因为我使用的是 typescript。 另一方面,我只是从express中导出类型,因为模块不使用它。

根据这个问题,冲突应该由eslint-plugin-node解决。

如何完成两个插件的合并? 我是否必须一一禁用 go 规则?

更新:似乎在eslint-plugin-node存储库的这个问题中被问到了。 它适用于no-unsupported-featuresno-missing-import ,但是,对于express的导入定义和no-extraneous-import ,它仍然失败。

更新 2:似乎eslint-plugin-node正在努力改进以实现它。 问题在这里

首先,您必须添加选项tryExtension以包含 TS 文件:

"settings": {
    "node": {
        "tryExtensions": [".js", ".json", ".node", ".ts", ".d.ts"]
    },

要解决no-unsupported-features/es-syntax ,根据this issue about added information to works with TypeScript,如果您使用转译器,您将不得不在rules下忽略它:

"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }],

另一方面,仅使用类型,而不是eslint-plugin-node尚不支持的代码。 他们正在努力改进以解决它。 但是,要解决no-missing-import问题,您必须将node_modules/@types添加到resolvePath中:

"node": {
    "resolvePaths": ["node_modules/@types"],
    "tryExtensions": [".js", ".json", ".node", ".ts", ".d.ts"]
},

即使这样,它也会生成一个no-extraneous-import ,因为它没有检测到模块,因为它只是一个类型。 与此同时,他们正在研究此增强功能,您可以在该规则下使用allowModules来解决问题:

"node/no-extraneous-import": ["error", {
    "allowModules": ["express"]
}]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM