TSLint似乎不适用服装规则?

时间:2018-10-13 17:06:47

标签: typescript tslint

因此,我一直在尝试使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文件中拼错该规则时会引发错误。

有人知道为什么这种行为方式吗?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我相信您需要为def remove(id): return useful_elements 注册TypeScript解析,因此在调用node之前,请先安装tslint并将调用更改为:

ts-node

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