简体   繁体   English

Eslint/Tslint 配置帮助(不包括文件)

[英]Eslint/Tslint Config help (excluding files)

Recently in our project we migrated to use the @typescript-eslint/parser to slowly migrate from tslint.最近在我们的项目中迁移到使用@typescript-eslint/parser从tslint慢慢迁移。 Everything has mostly been smooth but I have a small problem/question I can't work out.一切都很顺利,但我有一个小问题/问题我无法解决。

Do I need to specify ignore files/patterns in both the tsconfig exclude array, as well as the ignorePatterns on the .eslintrc export object?我需要在这两个tsconfig指定忽略文件/模式排除阵列,以及在上ignorePatterns .eslintrc出口对象? What is the difference?有什么不同?

We have a messages.js file inside our src/lang directory that I'm trying to ignore but currently throws a lint error on our pre-commit hook, which got me wondering about this question and how these two setups work together.我们的src/lang目录中有一个 messages.js 文件,我试图忽略它,但目前在我们的预提交挂钩上抛出了一个 lint 错误,这让我想知道这个问题以及这两个设置如何协同工作。

Parsing error: "parserOptions.project" has been set for '@typescript-eslint/parser' The file does not match your project config: src/lang/messages.js. The file must be included in at least one of the projects provided

I think my understanding of these intertwine is off, as when eslint runs, I thought the parserOptions would pick up the project rules from the tsconfig, where the js files are excluded.我认为我对这些交织的理解是关闭的,因为当 eslint 运行时,我认为 parserOptions 会从 tsconfig 中获取项目规则,其中排除了 js 文件。

Currently, the sections I'm talking about in our eslintrc looks like:目前,我在eslintrc 中谈论的部分如下所示:

module.exports = {
    parser: '@typescript-eslint/parser',
    parserOptions: {
        project: path.resolve(__dirname, './tsconfig.json'),
        tsconfigRootDir: __dirname,
        useJSXTextNode: true,
        sourceType: 'module',
        ecmaFeatures: {
            modules: true,
            jsx: true,
        },
    },
    ignorePatterns: ['node_modules/**', '.storybook/**', 'src/stories/**', '*.scss', '*.js'] // ignoring here works

tsconfig :配置

"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "src/**/*.js"], // exclude here doesn't work.

package.json:包.json:

"scripts": {
    "lint": "tsc --project ./tsconfig.json --noEmit && eslint --ext=jsx,ts,tsx src"
},

ESLint passes the @typescript-eslint/parser plugin a list of files (which ESLint obtains from the command-line). ESLint 向@typescript-eslint/parser插件传递文件列表(ESLint 从命令行获取)。 The @typescript-eslint/parser plugin cannot control this list, so it does its best to act based upon the list it is given. @typescript-eslint/parser插件无法控制这个列表,所以它会尽量根据给定的列表进行操作。

The error message is attempting to inform the user that ESlint is linting a file that is excluded from being parsed by the @typescript-eslint/parser plugin.该错误消息试图通知用户 ESlint 正在对一个被@typescript-eslint/parser插件排除在外的文件进行@typescript-eslint/parser From the plugin's perspective, there are two reasons this could occur:从插件的角度来看,这可能有两个原因:

  1. The file was purposefully, explicitly excluded (by adding it to the tconfig exclude ).该文件是有目的地明确排除的(通过将其添加到 tconfig exclude )。
  2. The user forgot to add the file to the tsconfig include .用户忘记将文件添加到 tsconfig include

In the latter case (which is much more common), the plugin wants to inform the user that their config is wrong, so that they can correct it.在后一种情况下(更常见),插件想要通知用户他们的配置是错误的,以便他们可以更正。 Hence the error message you saw.因此,您看到了错误消息。

In order to correctly exclude files from TSLint, one option is to use a .eslintignore file.为了从 TSLint 中正确排除文件,一种选择是使用.eslintignore文件。 You can also change the eslint command to ignore the excluded files:您还可以更改 eslint 命令以忽略排除的文件:

eslint ... --ignore-pattern "src/**/*.js"

(But be aware that the ignore pattern is relative to the current directory , not relative to the location of tsconfig etc.) (但请注意,忽略模式是相对于当前目录的,而不是相对于 tsconfig 等的位置)

Alternatively, if you do want ESLint to lint the files (but still exclude them in tsconfig), you can consider providing a more inclusive tsconfig for @typescript-eslint/parser by creating a tsconfig.eslint.json file that extends from your normal tsconfig .或者,如果您确实希望 ESLint 对文件进行 lint(但仍将它们排除在 tsconfig 中),您可以考虑通过创建一个从普通 tsconfig 扩展tsconfig.eslint.json文件为@typescript-eslint/parser提供更具包容性的tsconfig .

Also see these GitHub issues:另请参阅这些 GitHub 问题:

I was able to use the tsconfig exclude by using the JS config file .eslintrc.js and doing the following:通过使用 JS 配置文件.eslintrc.js并执行以下操作,我能够使用 tsconfig exclude:

const tsConfig = require('./tsconfig.eslint.json');

module.exports = {
  ...
  ignorePatterns: tsConfig.exclude,
};

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

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