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

Sna*_*rak 2 node.js typescript eslint

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

这是我目前的配置:

{
    "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
}
Run Code Online (Sandbox Code Playgroud)

代码编译正确,但是,如果我添加到 extends 选项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
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

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

Sna*_*rak 5

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

"settings": {
    "node": {
        "tryExtensions": [".js", ".json", ".node", ".ts", ".d.ts"]
    },
Run Code Online (Sandbox Code Playgroud)

为了解决no-unsupported-features/es-syntax这个问题,根据this issue about added information to works with TypeScript,如果您使用转译器,则必须在以下情况下忽略它rules

"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }],
Run Code Online (Sandbox Code Playgroud)

另一方面,仅使用类型而不是代码尚不支持eslint-plugin-node。他们正在研究一种增强来解决这个问题。然而,,解决了no-missing-import,你要添加到resolvePathnode_modules/@types

"node": {
    "resolvePaths": ["node_modules/@types"],
    "tryExtensions": [".js", ".json", ".node", ".ts", ".d.ts"]
},
Run Code Online (Sandbox Code Playgroud)

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

"node/no-extraneous-import": ["error", {
    "allowModules": ["express"]
}]
Run Code Online (Sandbox Code Playgroud)