繁体   English   中英

TSLint似乎不适用服装规则?

[英]TSLint appears to not apply costum rule?

因此,我一直在尝试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);
  }
}

据我了解,这应该为它找到的每个接口声明引发一个错误。 (忽略这一事实是没有意义的,并且文件名与其应有的功能不符,这只是出于纯测试目​​的。)

我将生成的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文件中拼错它时会引发错误。

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

我相信您需要为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,否则我也无法加载它们。 这应该可以解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM