【发布时间】:2019-03-18 14:49:25
【问题描述】:
所以,我一直在尝试让 TSLint 服装规则发挥作用,但无论我做什么,我似乎都无法让它发挥作用。
这个自定义规则我写好了,编译后放到对应的文件夹里:
//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);
}
}
据我了解,这应该会为它找到的每个接口声明引发错误。 (忽略这是毫无意义的事实,文件名与它的假定功能不对应,这是为了纯粹的测试目的。)
我已将生成的 interfacePascalCaseAndPrefixRule.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 似乎处于工作状态,因为当我将 "extends": "tslint:recommended" 添加到我的 tslint.json 时,它确实引发了一堆错误。
似乎也找到了该规则的实现,因为当我在tslint.json 文件中故意拼错它时会引发错误。
知道为什么会这样吗?任何帮助将不胜感激。
【问题讨论】:
标签: typescript tslint