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

168

使用的IDE是WebStorm 11.0.3,已经配置并启用了tslint,但是因为尝试解析较大的*.d.ts库文件,导致卡顿。

有没有一种方法可以忽略特定的文件或目录?

10个回答

239

更新 tslint v5.8.0

正如Saugat Acharya所提到的,您现在可以更新tslint.json CLI选项:

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

更多信息 在此拉取请求中


这个功能是在 tslint 3.6 中引入的。

tslint \"src/**/*.ts\" -e \"**/__test__/**\"

您现在可以添加 --exclude(或 -e)请参见PRCLI
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

你知道如何排除多个路径吗? - Juan Henao
4
重复多次"exclude"参数。 - Markus Wagner
4
更新内容有小错误:应该是 linterOptions 而不是 cliOptions - JeB
4
值得一提的是,排除目录内的全局模式应相对于“tslint.json”进行。 - JeB

93

目前使用的是Visual Studio Code,禁用tslint的命令是

/* tslint:disable */

需要注意的是,上面的“disable”禁用了该页面上的所有tslint规则。如果要禁用特定的规则,则可以指定一个/多个规则。

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

或者启用一个规则

/* tslint:enable comment-format */

更深入了解TSLint规则标识


3
在所有答案中,这是唯一有效的答案。谢谢! - Oz Lodriguez
例如,/* tslint:disable:TS2322 */ 没有起作用。你确定你可以在第二个代码块中忽略规则吗? - Bullsized
你可能需要使用 // 代替 /* */,那个链接有一些深入的规则。 - DeadlyChambers

27

除了Michael的回答,还可以考虑第二种方法:在tslint.json中添加linterOptions.exclude

例如,您可能有以下行的tslint.json

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

19

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

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

关于这个更多信息在此处


2
这应该被标记为正确的(并更新于2017年11月)答案。 - Andres Elizondo
正确的做法是将 cliOptions 替换为 linterOptions - Saugat

15

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

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

10
作为补充说明 要禁用下一行的所有规则// tslint:disable-next-line 要禁用下一行的特定规则// tslint:disable-next-line:rule1 rule2... 要禁用当前行的所有规则someCode(); // tslint:disable-line 要禁用当前行的特定规则someCode(); // tslint:disable-line:rule1

9

6

linterOptions目前只由CLI处理。如果您没有使用CLI,则根据您使用的代码库,您需要在其他地方设置忽略。webpack、tsconfig等。


4

可以确认,在tslint 5.11.0版本中,通过在package.json文件的lint脚本中定义exclude参数来修改它可以正常运行:

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

干杯!!


1
你是在提到其他答案,还是想为这个问题提供另一种解答? - Dharman

4

将这个部分添加到您的代码示例中

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接