带有 parserOptions 和排除的 Catch-22
Posted
技术标签:
【中文标题】带有 parserOptions 和排除的 Catch-22【英文标题】:Catch-22 with parserOptions and exclude 【发布时间】:2020-06-27 02:58:12 【问题描述】:我想将@typescript-eslint/no-floating-promises
添加到我的 ESLint 规则中,但遇到了困难。
这需要parserOptions
。
这是我的 .eslintrc.js:
module.exports =
root: true,
parser: '@typescript-eslint/parser',
parserOptions:
ecmaVersion: 2017,
project: './tsconfig.json'
,
plugins: [
'@typescript-eslint',
],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/eslint-recommended',
'plugin:@typescript-eslint/recommended',
'prettier'
],
rules:
'@typescript-eslint/no-floating-promises': ['error'],
'@typescript-eslint/no-explicit-any': 'off'
;
还有我的 tsconfig.json:
"compilerOptions":
"target": "ES2017",
"lib": ["ES2017"],
"module": "commonjs",
"declaration": true,
"outDir": "lib",
"removeComments": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
,
"include": ["src/**/*"],
"exclude": ["src/**/*.spec.ts"]
关键是exclude
。我不希望我的测试(规格)被编译到outDir
。但这在我运行 ESLint 时会导致以下错误:
error 解析错误:“parserOptions.project”已设置为 @typescript-eslint/解析器。该文件与您的项目不匹配 配置:src/index.spec.ts。该文件必须包含在至少一个 提供的项目
(脚本为"lint": "eslint \"src/**/*.ts\""
)
我确实想要 lint 我的测试。
在我添加 parserOptions
之前一切正常 - 我需要 no-floating-promises
规则。
typescript-eslint FAQ 说:
要解决此问题,只需确保 tsconfig 中的 include 选项包含您要 lint 的每个文件。
问题是我正在创建一个库,并且我不想发布测试文件。
如何只构建我想要发布到一个目录的文件,同时仍然对所有文件进行 linting? (我想让构建尽可能简单。)
【问题讨论】:
为什么没有两个不同的 TSConfig 文件?一个用于构建发布项目,另一个用于开发项目? 【参考方案1】:我在我的项目中使用了两个tsconfig.json
s。
tsconfig.json
"compilerOptions":
"target": "ES2017",
"lib": ["ES2017"],
"module": "commonjs",
"declaration": true,
"outDir": "lib",
"removeComments": true,
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true
,
"include": ["src/**/*"]
tsconfig.build.json
"extends": "./tsconfig.json",
"exclude": ["src/**/*.spec.ts"]
要构建项目运行tsc -p tsconfig.build.json
。
【讨论】:
完美运行!谢谢!以上是关于带有 parserOptions 和排除的 Catch-22的主要内容,如果未能解决你的问题,请参考以下文章
为@typescript-eslint/parser 设置了“parserOptions.project”
为@typescript-eslint/parser 设置了解析错误“parserOptions.project”
我的“.eslintrc.js”文件出错 - “解析错误:已为 @typescript-eslint/parser 设置了“parserOptions.project”。”
什么是“解析错误:已为 @typescript-eslint/parser 设置了“parserOptions.project”。 IntelliJ IDE 系列中的错误?
解析错误:已为 @typescript-eslint/parser 设置了“parserOptions.project”。该文件与您的项目配置不匹配:.eslintrc.js