简体   繁体   English

为什么@typescript-eslint/parser 包含在我的 tsconfig.json 中配置的文件之外的文件?

[英]Why is @typescript-eslint/parser including files outside of those configured in my tsconfig.json?

I am getting the error: -我收到错误:-

Parsing error: "parserOptions.project" has been set for u/typescript-eslint/parser.

The file does not match your project config: .eslintrc.js.

The file must be included in at least one of the projects provided.

IDE - WebStorm 20.1.1 IDE - WebStorm 20.1.1

Project structure: -项目结构:-

node_modules

src

--- testfile.js

.eslintrc.js

.prettierrc.js

baseeslint.js

package.json

tsconfig.json

.eslintrc.js: - .eslintrc.js:-

module.exports = {
extends: ['./baseeslint'],
parserOptions: {
project: './tsconfig.json'
},
rules: {}
}

baseeslint.js: - baseeslint.js:-

module.exports = {

parser: '@typescript-eslint/parser'

}

tsconfig.json: - tsconfig.json:-

{

"include": [

"/src/**/*.ts",

"/src/**/*.tsx"

],

"exclude": ["node_modules"],

"compilerOptions": {

"outDir": "./lib",

"types": ["styled-components", "react", "@types/react-router-dom", "reactstrap"]

}

}

I have absolutely no idea why this is happening?我完全不知道为什么会这样? I would assume it would ignore files in the root, especially with include specified in my tsconfg.json?我假设它会忽略根目录中的文件,尤其是在我的 tsconfg.json 中指定的包含?

Any help would be greatly appreciated.任何帮助将不胜感激。

EDIT: -编辑: -

My solution, should it help others...我的解决方案,它应该帮助别人......

I added the ignorePatterns configuration to my.eslintrc.js file: -我将 ignorePatterns 配置添加到 my.eslintrc.js 文件中:-

module.exports = {
  extends: ["mycustom/eslint-config"],
  parserOptions: {
    project: './tsconfig.json'
  },
  ignorePatterns: ["/*.*"],
  rules: {}
}

This can also be done via.eslintignore (see Anton Bessonov's response below).这也可以通过.eslintignore 完成(请参阅下面的 Anton Bessonov 的回复)。

Another thing relating to this which maybe of use to others here is the VScode config I used (I switched from WebStorm to VSCode after posting the original question).与此相关的另一件事可能对其他人有用的是我使用的 VScode 配置(我在发布原始问题后从 WebStorm 切换到 VSCode)。 I am a big fan of running eslint fix on save but I do not want it attempting to do this with all files.我非常喜欢在保存时运行 eslint fix,但我不希望它尝试对所有文件执行此操作。 My project files (and only my project files) are all.ts/.tsx.我的项目文件(并且只有我的项目文件)是 all.ts/.tsx。 The options below in settings.json allow fix on save without it including every file in my repo: - settings.json 中的以下选项允许修复保存而不包括我的存储库中的每个文件:-

{
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "eslint.probe": [
        "typescript",
        "typescriptreact"
    ],
}

If eslint.probe is not specified, it defaults to: -如果未指定 eslint.probe,则默认为:-

["javascript", "javascriptreact", "typescript", "typescriptreact", "html", "vue"]

More about the VSCode ESLint extension settings can be found here .更多关于 VSCode ESLint 扩展设置可以在这里找到。

Why this happens?为什么会发生这种情况?

I'm not entirely sure, but it seems like @typescript-eslint/parser tries to compare files covered in the eslint configuration (for example by extension) with files included in tsconfig.json .我不完全确定,但似乎@typescript-eslint/parser试图将 eslint 配置中涵盖的文件(例如通过扩展名)与tsconfig.json中包含的文件进行比较。 Since the .js files are covered by @typescript-eslint/parser and depending on your eslint configuration, the file is probably included in the eslint run.由于.js文件被@typescript-eslint/parser覆盖,并且根据您的 eslint 配置,该文件可能包含在 eslint 运行中。 But because the file isn't included in tsconfig, you get this error.但是由于该文件未包含在 tsconfig 中,因此您会收到此错误。

How to fix that?如何解决?

Depending on your case, you can select one of them:根据您的情况,您可以 select 其中之一:

  1. You can ignore it in .eslintignore .您可以在.eslintignore中忽略它。 This is what @U4EA did.这就是@U4EA 所做的。
  2. If can add it to the includes in tsconfig.json :如果可以将其添加到tsconfig.json中的包含:
    "include": [
        ".eslintrc.js",
        // another includes
    ]
  1. If you don't have a "right" tsconfig.json, because you use a monorepo, then you can create a new file tsconfig.eslint.json :如果您没有“正确”的 tsconfig.json,因为您使用的是 monorepo,那么您可以创建一个新文件tsconfig.eslint.json
{
    "include": [
        ".eslintrc.js"
    ]
}

and use this file for eslint only:并将此文件仅用于 eslint:

    parserOptions: {
        project: [
            './tsconfig.eslint.json',
            './packages/*/tsconfig.json',
        ],
    },

You can also ignore.eslintrc.js in.eslintrc.js itself, saving you an additional file:你也可以在.eslintrc.js 中忽略.eslintrc.js,为你节省一个额外的文件:

"ignorePatterns": [
    ".eslintrc.js"
],

can be helpful for VS Code users, uninstalling or disabling ESLint extension may solve this issue, it worked for me:)可能对 VS Code 用户有帮助,卸载或禁用 ESLint 扩展可能会解决这个问题,它对我有用:)

Solved by adding .eslintignore通过添加.eslintignore解决

/*.*

Edit:编辑:

My final resolution was to add the ignorePatterns configuration to my.eslintrc.js file: -我的最终解决方案是将ignorePatterns配置添加到 my.eslintrc.js 文件中:-

module.exports = {
  extends: ["mycustom/eslint-config"],
  parserOptions: {
    project: './tsconfig.json'
  },
  ignorePatterns: ["/*.*"],
  rules: {}
}

Reason for this is to just get rid of the.eslintignore file (less workspace clutter).这样做的原因是为了摆脱 .eslintignore 文件(减少工作空间的混乱)。

Extension扩大

I had a similar issue and I disabled the eslint extension on vs code and that was it for me.我有一个类似的问题,我在 vs 代码上禁用了 eslint 扩展,这就是我的问题。 I'm assuming there was some kind of conflict between the global eslint and the one for my project.我假设全球 eslint 和我的项目之间存在某种冲突。

Just to build off the accepted answer , something I found helpful for me was that, by default, hidden files aren't captured by globs .只是为了建立公认的答案,我发现对我有帮助的是,默认情况下,隐藏文件不会被 glob 捕获

To fix this, you could do some combination of this (depending on what your shell environment supports ):要解决此问题,您可以对此进行一些组合(取决于您的shell 环境支持的内容):

"include": [
    "./**/*", // All non-hidden files at any directory level
    "./**/.*", // Hidden dotfiles like .eslintrc.js at any level
    "./**/.*.[tj]s?(x)" // All hidden TS/JS dotfiles
]

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

相关问题 带有 TypeScript 解析器/插件的 ESLint:如何包含从 tsconfig.json 中排除的 include.spec.ts? - ESLint with TypeScript parser/plugin: how to include .spec.ts, that is excluded from tsconfig.json? typescript-eslint 未使用 tsconfig - tsconfig not used by typescript-eslint tsconfig.json之外是否有打字稿编译器选项 - Are there typescript compiler options outside the tsconfig.json 为什么 TypeScript 编译器会忽略 tsconfig.json? - Why is the TypeScript compiler ignoring tsconfig.json? 为什么 Vite 会创建两个 TypeScript 配置文件:tsconfig.json 和 tsconfig.node.json? - Why does Vite create two TypeScript config files: tsconfig.json and tsconfig.node.json? Typescript 编译器在 tsconfig.json 上包含文件时找不到绝对路径 - Typescript compiler not finding absolute paths when including files on tsconfig.json 为什么 Typescript 在 Visual Studio Code 中忽略我的 tsconfig.json? - Why does Typescript ignore my tsconfig.json inside Visual Studio Code? 为什么打字稿编译器会在服务器启动时更改我的 tsconfig.json 文件? - Why is typescript compiler changing my tsconfig.json file on server start up? 澄清tsconfig.json“文件”列表和打字稿语义 - Clarify tsconfig.json “files” list and typescript semantics 如何使PhpStorm在.vue文件中反映TypeScript tsconfig.json? - How to make PhpStorm reflect TypeScript tsconfig.json in .vue files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM