如何忽略 tslint 的特定目录或文件?

How to ignore a particular directory or file for tslint?

正在使用的 IDE 是 WebStorm 11.0.3,tslint 已配置并且可以正常工作,但是它挂起,因为它试图解析大型 *.d.ts 库文件。

有没有办法忽略特定文件或目录?

are others who encountered the problem. Unfortunately, there is only an open issue for excluding files: https://github.com/palantir/tslint/issues/73

恐怕答案是否定的。

tslint v5.8.0

更新

所述,您现在可以更新 tslint.json CLI 选项:

{
  "extends": "tslint:latest",
  "linterOptions": {
      "exclude": [
          "bin",
          "lib/*generated.js"
      ]
  }
}

更多信息in this pull request


此功能已在 tslint 3.6

中引入
tslint \"src/**/*.ts\" -e \"**/__test__/**\"

您现在可以添加 --exclude(或 -e),请参阅此处 PR

CLI

usage: tslint [options] file ...

Options:

-c, --config          configuration file
--force               return status code 0 even if there are lint errors
-h, --help            display detailed help
-i, --init            generate a tslint.json config file in the current working directory
-o, --out             output file
-r, --rules-dir       rules directory
-s, --formatters-dir  formatters directory
-e, --exclude         exclude globs from path expansion
-t, --format          output format (prose, json, verbose, pmd, msbuild, checkstyle)  [default: "prose"]
--test                test that tslint produces the correct output for the specified directory
-v, --version         current version

您正在考虑使用

-e, --exclude         exclude globs from path expansion

当前使用 Visual Studio 禁用 tslint 的代码和命令是

/* tslint:disable */

注意事项。上面的禁用禁用该页面上的所有 tslint 规则。如果您想禁用特定规则,您可以指定 one/multiple 条规则。

/* tslint:disable comment-format */
/* tslint:disable:rule1 rule2 rule3 etc.. */

或启用规则

/* tslint:enable comment-format */

More in depth on TSLint rule flags

除了 Michael 的回答之外,考虑第二种方法:将 linterOptions.exclude 添加到 tslint.json

例如,您可能 tslint.json 包含以下行:

{
  "linterOptions": {
    "exclude": [
      "someDirectory/*.d.ts"
    ]
  }
}

tslint v5.8.0 开始,您可以在 tslint.json 文件中的 linterOptions 键下设置 exclude 属性:

{
  "extends": "tslint:latest",
  "linterOptions": {
      "exclude": [
          "bin",
          "**/__test__",
          "lib/*generated.js"
      ]
  }
}

关于此的更多信息here

linterOptions 当前仅由 CLI 处理。 如果您不使用 CLI,那么根据您使用的代码库,您需要在其他地方设置忽略。 webpack、tsconfig 等

我必须使用 **/* 语法来排除文件夹中的文件:

    "linterOptions": {
        "exclude": [
          "src/auto-generated/**/*",
          "src/app/auto-generated/**/*"
        ]
    },

可以确认在 tslint 5.11.0 版本上它可以通过定义排除参数修改 package.json 中的 lint 脚本来确认:

"lint": "ng lint --exclude src/models/** --exclude package.json"

干杯!!

作为补充

禁用下一行的所有规则 // tslint:disable-next-line

禁用下一行的特定规则// tslint:disable-next-line:rule1 rule2...

禁用当前行的所有规则someCode(); // tslint:disable-line

禁用当前行的特定规则someCode(); // tslint:disable-line:rule1

在您的代码示例中添加该部分

"linterOptions": {
"exclude": [
  "node_modules/**/*.",
  "db/**/*.",
  "integrations/**/*."
]

},