首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ESLint与eslint-plugin-import和typescript-eslint冲突。

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

Stack Overflow用户
提问于 2020-04-04 05:34:26
回答 1查看 10.5K关注 0票数 4

我想包含来自eslint plugin节点的规则eslint plugin节点,但是它与我当前的.eslintrc冲突,因为我使用的是打字稿eslint-导入解析器-类型记录

这是我目前的配置:

代码语言:javascript
复制
{
    "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选项中,编译过程将失败:

代码语言:javascript
复制
  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。另外,这个规则应该被忽略,因为我使用的是类型记录。另一方面,我只是从express导出类型,因为模块不使用它。

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

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

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

更新了2:,似乎eslint-plugin-node正在做一个增强来完成它。在此发行

EN

回答 1

Stack Overflow用户

发布于 2020-04-04 06:58:04

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

代码语言:javascript
复制
"settings": {
    "node": {
        "tryExtensions": [".js", ".json", ".node", ".ts", ".d.ts"]
    },

要解决no-unsupported-features/es-syntax,根据这个关于向TypeScript添加信息的问题,如果您使用的是传输程序,则必须在rules下忽略它

代码语言:javascript
复制
"node/no-unsupported-features/es-syntax": ["error", { "ignores": ["modules"] }],

另一方面,只使用类型,而不是eslint-plugin-node还不支持代码。他们正在开发一个增强功能来解决这个问题。但是,要解决no-missing-import,必须将node_modules/@types添加到resolvePath

代码语言:javascript
复制
"node": {
    "resolvePaths": ["node_modules/@types"],
    "tryExtensions": [".js", ".json", ".node", ".ts", ".d.ts"]
},

即便如此,它也会生成一个no-extraneous-import,因为它不检测模块,因为它只是一个类型。同时,您可以在此规则下使用allowModules来解决此问题:

代码语言:javascript
复制
"node/no-extraneous-import": ["error", {
    "allowModules": ["express"]
}]
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61024198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档