TSLint似乎不适用服装规则?



所以,我一直在尝试 TSLint costum 规则来工作,但无论我做什么,我似乎都无法让它工作。

我编写了这个自定义规则,编译了它并将其放在相应的文件夹中:

//filename is interfacePascalCaseAndPrefixRule.ts 
import * as Lint from "tslint"; 
import * as ts from "typescript";
export class Rule extends Lint.Rules.AbstractRule {
static FAILURE_STRING =
"Interfaces have to be Pascal cased and prefixed with an I (first two letters are capitalized).";
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new Walk(sourceFile, this.getOptions()));
}
}
class Walk extends Lint.RuleWalker {
protected visitInterfaceDeclaration(node: ts.InterfaceDeclaration) {
this.addFailureAtNode(node, Rule.FAILURE_STRING);
super.visitInterfaceDeclaration(node);
}
}

据我了解,这应该会为它找到的每个接口声明引发错误。(忽略这是毫无意义的,文件名与它所谓的功能不对应的事实,这是纯粹的测试目的。

我已经将生成的接口PascalCaseAndPrefixRule.ts放在TypeScript项目的rules/-文件夹中。tslint.json如下所示:

{
"defaultSeverity": "error",
"rulesDirectory": [
"rules/"
],
"rules": {
"interface-pascal-case-and-prefix": true, // <-- This is the costum rule that doesn't do shit
"class-name": true, //Enforces pascal case for classes eg. "MyClass"
"indent": [
true,
"spaces",
4
], //4 spaces as indents (is probably broken)
"align": [
true,
"statements",
"members"
], //aligns things. (might be broken as well)
"encoding": true //encoding is UTF-8
}
}

tsconfig.json如下所示:

{
"compileOnSave": true,
"compilerOptions": {
"outDir": "dist",
"module": "commonjs",
"target": "es6",
"sourceMap": true,
"strictNullChecks": true
},
"include": ["src/**/*"]
}

当我运行 tslint 时,实际上什么也没发生(即使它肯定会引发一些错误(。控制台输出如下所示:

:~/Desktop/bubblesbot$ tslint -p .
:~/Desktop/bubblesbot$ 

TSLint 似乎处于工作状态,因为当我向tslint.json添加"extends": "tslint:recommended"时,它确实会引发一堆错误。

该规则的实现似乎也被发现了,因为当我故意在tslint.json文件中拼写错误时,它会引发错误。

知道为什么它会这样吗?任何帮助将不胜感激。

我相信你需要为node注册 TypeScript 解析,所以在调用tslint之前,先安装ts-node并将调用更改为:

# if using yarn
$(yarn bin)/ts-node $(yarn bin)/tslint -p .
# if using npm
$(npm bin)/ts-node $(yarn bin)/tslint -p .

我也无法加载 TS 规则,除非它们首先编译为 JS;这应该可以解决问题。

最新更新