如何使用 tslint 对整个文件夹进行 lint

How to lint entire folder using tslint

是否可以使用 tslint 对整个文件夹进行 lint?

使用 eslint 可以 eslint ./src 验证整个文件夹。

当我尝试对 tslint 执行相同操作时 - 我收到错误 Error: EISDIR: illegal operation on a directory。在他们网站上的示例中 - 他们展示了如何验证单个文件,这通常不是这种情况。

是否可以仅从命令行验证我的项目而无需额外的东西,例如 gulp-tslint

您可以使用 glob 对多个文件进行 lint。

通常,如果您按原样传递一个 glob,您的 shell 将扩展它并将生成的文件传递给 TSLint。因此,例如,在启用了 globstar 选项的 bash 4+ 中,您可以执行以下操作来检查所有 .ts.tsx 文件:

tslint src/**/*.ts{,x}

尽管使用跨平台和 shells 可以一致工作的命令,但您可能会过得更好。为此,您可以在引号中传递 glob。当在引号中时,glob 将按原样传递给 TSLint,TSLint 将使用 node-glob 处理它。然后,您可以 运行 以下命令获得与上述相同的结果:

tslint 'src/**/*.ts?(x)'

现在有一个 --project 选项。示例:

tslint --project .

来自docs

-p, --project:

The path or directory containing a tsconfig.json file that will be used to determine which files will be linted. This flag also enables rules that require the type checker.

If your project has a tsconfig.json you can aproach --project advange.

在我的例子中,我有一个 nativescript 项目,它在项目根目录下包含一个 tsconfig.json,所以我有这个:

注意 json 'node_modules' 和 'platforms' 目录被排除在外。 这意味着项目中的所有文件都将被映射,除了 'node_modules' 和 'platforms' 目录。

我已经在我的 package.json 添加了这个脚本,'tslint' 用于记录错误,'tslint-fix' 用于修复错误。

"scripts": {
    "tslint": "tslint -p tsconfig.json",
    "tslint-fix": "tslint --fix -p tsconfig.json"
}