[最近在我们的项目中,我们迁移到使用@typescript-eslint/parser
从tslint缓慢迁移。一切基本上都很顺利,但是我有一个小问题/问题我无法解决。
我是否需要在tsconfig exclude数组以及.eslintrc
导出对象上的ignorePatterns中都指定忽略文件/模式?有什么区别?
我们在src/lang
目录中有一个messages.js文件,我正在尝试忽略该文件,但目前在pre-commit钩子上抛出了一个皮棉错误,这让我想知道这个问题以及这两种设置如何协同工作。
Parsing error: "parserOptions.project" has been set for '@typescript-eslint/parser'
The file does not match your project config: src/lang/messages.js. The file must be included in at least one of the projects provided
我认为我对这些交织的理解已经关闭,因为当eslint运行时,我认为parserOptions将从tsconfig中获取项目规则,而js文件被排除在其中。
当前,我在我们的[[eslintrc中谈论的部分看起来像:
module.exports = {
parser: '@typescript-eslint/parser',
parserOptions: {
project: path.resolve(__dirname, './tsconfig.json'),
tsconfigRootDir: __dirname,
useJSXTextNode: true,
sourceType: 'module',
ecmaFeatures: {
modules: true,
jsx: true,
},
},
ignorePatterns: ['node_modules/**', '.storybook/**', 'src/stories/**', '*.scss', '*.js'] // ignoring here works
:tsconfig
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "src/**/*.js"], // exclude here doesn't work.
package.json:
"scripts": { "lint": "tsc --project ./tsconfig.json --noEmit && eslint --ext=jsx,ts,tsx src" },
@typescript-eslint/parser
(以下简称为ts-parser
)是ESLint的插件。[ESLint将ts-parser
传递给文件列表(ESLint从命令行获得)。 ts-parser
无法控制此列表,因此它将尽最大努力根据给出的列表进行操作。
错误消息试图通知用户ts-parser
ESlint正在清除从打字稿中排除的文件。从ts-parser
的角度来看,可能有两个原因:
ts-parser
想要通知用户他们的配置错误,以便他们可以纠正它。为了正确地从TSLint中排除文件,一种选择是使用.eslintignore文件。您还可以更改eslint命令以忽略排除的文件:
eslint ... --ignore-pattern "src/**/*.js"
((但是请注意,忽略模式是相对于
当前目录
,而不是相对于tsconfig等的位置。)或者,如果do
希望ESLint整理文件(但仍在tsconfig中排除它们),则可以考虑通过创建ts-parser
的tsconfig.eslint.json
文件为tsconfig.eslint.json
提供更具包容性的tsconfig。 另请参见以下GitHub问题: