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“文件(空白/默认配置):
{}我用以下内容填充".eslintrc.js“文件,如Airbnb文件中所指定的
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“文件时,这个错误也会出现在.eslintrc.js中。需要解决该错误,以便ESLint可以将文件的其余部分链接起来。(为了澄清,我希望".eslintrc.js“文件的排列方式与我希望我的TypeScript源代码被排列的方式相同--例如,有2个空格缩进等等)。
附加信息:我在Windows 10 LTSC与Nodev14.8.0和npm版本6.14.7。
发布于 2020-10-09 15:39:55
当您使用类型记录驱动的eslint规则(当您的eslint配置包括一个"parserOptions.project"配置时),如果您试图链接一个不是类型记录项目中包含的文件之一的文件,eslint就会抛出一个错误。
这是由于性能方面的原因--以前是eslint允许这样做的,但是这样做会造成很大的性能损失,所以他们将其更改为引发错误。。
不属于TS项目一部分的linting文件的解决方案是创建一个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文件进行链接。
发布于 2021-10-19 10:44:20
雷萨姆的回答工作得非常好。但是,您也只能使用@ TypeScript eslint/解析器对TypeScript文件使用ESLint覆盖。这避免了只为ESLint创建一个新的ESLint。
module.exports = {
// Common configuration for .eslintrc.js and TypeScript files
rules: {
eqeqeq: "error",
"no-var": "error",
// ...
},
overrides: [{
files: "**/*.ts"
// TypeScript-only configuration
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
tsconfigRootDir: __dirname,
sourceType: "module",
},
plugins: ["@typescript-eslint"],
rules: {
"@typescript-eslint/no-floating-promises": "error",
// ...
}
]},
}https://stackoverflow.com/questions/64271575
复制相似问题