Skip to main content

大仓配置


如果你使用的是 monorepo,这些文档将帮助你了解如何设置类型化 linting。 如果你不想使用类型化 linting,那么你可以到此为止 - 你无需执行任何特殊操作。

英:If you're using a monorepo, these docs will help you figure out how to setup typed linting. If you don't want to use typed linting, then you can stop here - you don't need to do anything special.

根据你使用的设置,配置看起来会有所不同:

英:Configurations will look different based on which setup you use:

  1. tsconfig.json
  2. 每个包一个 tsconfig.json(根目录中可选一个)

tsconfig.json

如果你只有一个 tsconfig.json 文件,并且其 include 路径包含你想要 lint 的所有文件,则可以直接将其与 typescript-eslint 一起使用,而无需进一步配置。

英:If you only have one tsconfig.json file and its include paths include all the files you'd like to lint, you can directly use it with typescript-eslint without further configuration.

如果它的 include 路径不能包含所有要检查的文件,我们建议创建一个名为 tsconfig.eslint.json 的新配置,如下所示:

英:If its include paths cannot include all files to be linted, we suggest creating a new config called tsconfig.eslint.json, that looks something like this:

tsconfig.eslint.json
{
// extend your base config to share compilerOptions, etc
"extends": "./tsconfig.json",
"compilerOptions": {
// ensure that nobody can accidentally use this config for a build
"noEmit": true
},
"include": [
// whatever paths you intend to lint
"src",
"test",
"tools"
]
}

请务必更新你的 .eslintrc.js 以指向这个新的配置文件。

英:Be sure to update your .eslintrc.js to point at this new config file.

每个包一个 tsconfig.json(根目录中可选一个)

使用类型信息进行代码检查 中引入的 parserOptions.project 选项接受相对路径数组。 路径可以提供为 节点全局。 对于每个被检查的文件,第一个匹配的项目路径将用作其支持 TSConfig。

英:The parserOptions.project option introduced in Linting with Type Information accepts an array of relative paths. Paths may be provided as Node globs. For each file being linted, the first matching project path will be used as its backing TSConfig.

.eslintrc.js
/* eslint-env node */
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
],
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: true,
project: ['./tsconfig.eslint.json', './packages/*/tsconfig.json'],
},
plugins: ['@typescript-eslint'],
root: true,
};

parserOptions.project 中的广泛通配符

parserOptions.project 中使用宽 glob ** 可能会降低 linting 性能。 与使用 ** 递归检查所有文件夹的 glob 不同,更喜欢一次使用单个 * 的路径。

英:Using wide globs ** in your parserOptions.project may degrade linting performance. Instead of globs that use ** to recursively check all folders, prefer paths that use a single * at a time.

.eslintrc.js
/* eslint-env node */
module.exports = {
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended-type-checked',
],
parser: '@typescript-eslint/parser',
parserOptions: {
tsconfigRootDir: __dirname,
project: ['./tsconfig.eslint.json', './**/tsconfig.json'],
project: ['./tsconfig.eslint.json', './packages/*/tsconfig.json'],
},
plugins: ['@typescript-eslint'],
root: true,
};

详细信息请参见 解析器选项 "project" 中的 Glob 模式会减慢 linting

英:See Glob pattern in parser's option "project" slows down linting for more details.

关于大型(> 10)多包大仓的重要说明

我们收到的报告表明,对于足够大和/或相互依赖的项目,使用此方法可能会遇到 OOM。 我们的建议是先设置并测试,因为触发此 OOM 的情况很少。

英:We've had reports that for sufficiently large and/or interdependent projects, you may run into OOMs using this approach. Our advice is to set it up and test first, as there are very few cases that trigger this OOM.

有关更多信息和讨论,请参阅 #1192

英:See #1192 for more information and discussion.

如果你确实遇到了 OOM,请对上述问题发表评论,并让我们了解你的存储库 - 我们拥有的信息越多越好。 作为临时解决方法,请考虑以下方法之一:

英:If you do run into an OOM, please comment on the above issue and let us know about your repo - the more information we have, the better. As an interim workaround, consider one of the following:

  • 切换到单根 tsconfig.eslint.json(参见 tsconfig.json
  • 使用 shell 脚本一次仅检查一个包,并使用上面的现有配置。

故障排除

如果你在使用此功能时遇到问题,请查看我们的 故障排除常见问题解答

英:If you're having problems getting this working, please have a look at our Troubleshooting FAQ.