typescript - TSLint 似乎不适用服装规则?

标签 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);
  }
}

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

我已将生成的 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;这应该可以解决问题。

关于typescript - TSLint 似乎不适用服装规则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52795325/

相关文章:

node.js - 在 TypeScript 中,如何使用 (a : type, b :type): any?

angular - 如何运行@angular-eslint/schematics :convert-tslint-to-eslint

javascript - 用 3 种模式替换字符串 "space", "and", "or"

javascript - 同步代码中不会发生更改检测

angular - 没有 HttpClient 的提供者

javascript - tslint - 最后一行缺少尾随逗号 (trailing-comma)

javascript - 在javascript中将数组本身合并到另一个具有相同键的数组中

typescript :映射类型中的枚举键

javascript - 处理 'lambda in JSX attribute' 案例

typescript TS7053 : Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'