我在TypeScript中为TSlint编写了一个自定义规则,可以在我的Linux VM上使用的脚本中使用ts-node
来运行该规则:
#!/bin/bash
set -e
ROOT_DIR=$(dirname $(dirname $0))
cd $ROOT_DIR
find "$ROOT_DIR/src" -name "*.ts" -o -name "*.tsx" | xargs $(yarn bin)/ts-node $(yarn bin)/tslint
这使tslint
可以直接运行TS规则,而无需先进行编译,并且可以正常工作。
问题在于,即使ms-vscode.vscode-typescript-tslint-plugin
规则中已明确启用tsconfig.json
,也未遵循该规则。我在具有Windows主机的单独计算机上运行VSCode,并通过Linux VM提供的Samba挂载文件系统。对于安装和内置的规则,将遵守并显示文件中的其他规则,例如no-console
等。
如何获取有关为什么未应用该规则的信息?我需要在插件中进行配置才能使其正常工作吗?
答案 0 :(得分:0)
我认为这可能与问题有关。
让我们将规则文件命名为noImportsRule.ts
。规则在tslint.json
中以其烤肉串大小写的标识符进行引用,因此"no-imports": true
将配置规则。
不能使用自定义实现覆盖核心规则。
自定义规则还可以像核心规则一样接受选项(通过this.getOptions())
从TSLint v5.7.0开始,您不再需要在使用自定义规则之前对其进行编译。您需要例如通过使用ts-node来告诉node.js如何加载.ts文件:
重要约定:
camelCasedRule.ts
)。Rule
。Lint.Rules.AbstractRule
扩展针对所有details
答案 1 :(得分:0)
不幸的是,VScode / vscode-typescript-tslint-plugin不支持此功能,并且似乎不在他们的议程中。
Here is a closed issue on their GitHub asking for this feature.
您当然可以先将规则编译为JS,但这不是您要的。
答案 2 :(得分:0)
我开发的骇客是这样的:
./rules
下,目录如下:rules/
|- registerRule.js
|- oneRule.ts
|- twoRule.ts
oneRule.ts
和twoRule.ts
是用打字稿编写的实际规则,但是registerRule.js
是“间谍”。该间谍规则的目的只是为了让tslint
实例自己将其拾取并将ts-node/register
注入节点运行时。// registerRule.js
require('ts-node/register')
const Lint = require('tslint')
class Rule extends Lint.Rules.AbstractRule {
apply(sourceFile) {
return []
}
}
module.exports.Rule = Rule
tslint.json
{
"rulesDirectory": "./rules",
"rules": {
"register": true,
"one": true,
"two": true
}
}
我已经测试过。此技巧将教vscode tslint插件了解.ts
自定义规则。