【问题标题】:ESLint conflicts with eslint-plugin-import and typescript-eslintESLint 与 eslint-plugin-import 和 typescript-eslint 冲突
【发布时间】:2020-07-16 08:23:38
【问题描述】:

我想包含来自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": "&gt;=12.0.0。此外,应该忽略此规则,因为我使用的是打字稿。另一方面,我只是从express 导出类型,因为模块不使用它。

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

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

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

更新 2: 似乎eslint-plugin-node 正在努力改进以实现它。 Issue here

【问题讨论】:

    标签: node.js typescript eslint


    【解决方案1】:

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

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

    要解决no-unsupported-features/es-syntax,根据这个issue关于添加信息以使用TypeScript,如果您使用转译器,您将不得不在rules下忽略它:

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

    另一方面,只使用类型而不是 eslint-plugin-node 尚不支持的代码。他们正在研究enhancement 来解决它。但是,要解决no-missing-import,您必须在resolvePath 中添加node_modules/@types

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

    即便如此,它还是会生成一个no-extraneous-import,因为它没有检测到模块,因为它只是一个类型。与此同时,他们正在努力进行这项改进,您可以在该规则下使用allowModules 解决方法:

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

    【讨论】:

    • 出于某种原因,这表明我对这个答案投了反对票,但我以前从未见过。除非您编辑答案,否则我无法删除我的反对票。您在“但是,解决...”中有一个双逗号,删除它,我可以解决这个问题。
    猜你喜欢
    • 2023-03-14
    • 2019-07-30
    • 2016-05-27
    • 2019-11-16
    • 2019-06-28
    • 2020-12-25
    • 1970-01-01
    • 2021-12-08
    • 2020-11-25
    相关资源
    最近更新 更多