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”文件的形式以与我的TypeScript源代码的形式相同的方式进行编码-例如,具有2个空格缩进,依此类推。)
其他信息:我在Windows 10 LTSC上,具有Node v14.8.0和npm版本6.14.7。
答案 0 :(得分:6)
当您使用打字稿驱动的eslint规则时(当eslint配置包含"parserOptions.project"
配置时),如果您尝试抹掉不是其中包含的文件之一的文件,则eslint会引发错误打字稿项目。
这是由于性能原因-过去曾经是eslint允许这样做,但是这样做会造成很大的性能损失,所以他们changed it to throw an error instead。
清除不属于TS项目的文件的解决方案是创建一个tsconfig,该tsconfig扩展您的“真实” 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
文件。