我的";.eslintrc.js";文件出错-";解析错误:";parserOptions.project";已为@tyescript-eslint/parser设置。"; [英] Error with my ".eslintrc.js" file - "Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser."
本文介绍了我的";.eslintrc.js";文件出错-";解析错误:";parserOptions.project";已为@tyescript-eslint/parser设置。";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
ESLint似乎无法分析";.eslintrc.js";文件。
复制步骤: 我设置了一个新的";hello world";TypeScript项目,如下所示:# Make a new directory for our new project
mkdir test
# Go into the new directory
cd test
# Create a "package.json" file
npm init --yes
# Install TypeScript
npm install typescript --save-dev
# Install ESLint (the linter)
npm install eslint --save-dev
# Install the Airbnb ESLint config (the most popular linting config in the world)
npm install eslint-config-airbnb-typescript --save-dev
# The import plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install eslint-plugin-import@^2.22.0 --save-dev
# The TypeScript plugin for ESLint is needed for the Airbnb config to work properly with TypeScript
npm install @typescript-eslint/eslint-plugin@^4.2.0 --save-dev
# Create the config file for TypeScript
touch tsconfig.json
# Create the config file for ESLint
touch .eslintrc.js
# Create the entry point for our TypeScript application
touch main.ts
我使用以下内容(空白/默认配置)填充";tsconfig.json";文件:
{}
我用the Airbnb documentation中指定的以下内容填充";.eslintrc.js";文件:
module.exports = {
extends: ['airbnb-typescript/base'],
parserOptions: {
project: './tsconfig.json',
},
};
我在";main.ts";中填入以下内容:
const foo = 'bar';
然后,当我运行npx eslint main.ts
时,它正确地生成以下错误:
1:7 error 'foo' is assigned a value but never used @typescript-eslint/no-unused-vars
因此,ESLint似乎工作正常。但是,当我运行npx eslint .eslintrc.js
时,我收到以下错误:
0:0 error Parsing error: "parserOptions.project" has been set for @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
每当我打开";.eslintrc.js";文件时,VSCode中也会出现此错误。需要解决该错误,以便eSLint可以链接文件的睡觉。(为澄清起见,我希望";.eslintrc.js";文件的打印方式与我希望打印脚本源代码的打印方式相同-例如,使用2个空格缩进,依此类推。)
其他信息:我使用的是Windows 10 LTSC和Node v14.8.0和NPM版本6.14.7。
推荐答案
当您使用TypeScript支持的eslint规则时(当eslint配置包括"parserOptions.project"
配置时),如果您尝试链接的文件不是TypeScript项目中包含的文件之一,则eslint将引发错误。
这是由于性能原因-以前ESLINT允许这样做,但这样做会对性能造成很大影响,因此它们changed it to throw an error instead。
要链接不属于TS项目的文件,解决方案是创建一个tsconfig,它扩展您的&real";tsconfig,但包括您要链接的所有文件。这通常称为tsconfig.eslint.json
,如下所示:
// Special typescript project file, used by eslint only.
{
"extends": "./tsconfig.json",
"include": [
// repeated from base config's "include" setting
"src",
"tests",
// these are the eslint-only inclusions
".eslintrc.js",
]
}
在您的.eslintrc.js
中,您将把project: './tsconfig.json'
更改为project: './tsconfig.eslint.json'
。
这应该可以解决错误,并允许链接.eslintrc.js
文件。
这篇关于我的";.eslintrc.js";文件出错-";解析错误:";parserOptions.project";已为@tyescript-eslint/parser设置。";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文