【问题标题】:Use custom `tslint` rule without compilation from typescript使用自定义 `tslint` 规则,无需从 typescript 编译
【发布时间】:2019-11-30 18:18:45
【问题描述】:

我已经 made my own tslint rule,保存到 tslint-rules/disallowGetterInsideOfTheLoopRule.ts 并将其添加到 tslint 配置中:

{
    "defaultSeverity": "error",
    "extends": [
        "tslint:latest",
        "tslint-react"
    ],
    "jsRules": {},
    "rules": {
        "disallow-getter-inside-of-the-loop": [true, "functionWithSomeVeryUniqueName"]
    },
    "rulesDirectory": [
        "./tslint-rules"
    ]
}

但它只有在我将其编译为 JavaScript 并更新编译文件中的导入/导出语句时才有效:

var Lint = require("tslint");
var ts = require("typescript");
const arrayMethods = new Set(["find", "findIndex", "sort", "forEach", "filter", "flatMap", "map", "every", "some", "reduce", "reduceRight"]);
module.exports.Rule = class Rule extends Lint.Rules.AbstractRule {
    apply(sourceFile) {

如何在不编译的情况下在 TypeScript 中使用我的规则?

要运行 linting,我目前正在使用 npm run lint 和以下脚本:

"lint": "tslint --config tslint.json --project tsconfig.json \"src/**/*.ts{,x}\"",

【问题讨论】:

    标签: javascript typescript tslint


    【解决方案1】:

    现在配置编译无需手动更新:

    "compile-lint-rules": "for %f in (./tslint-rules/*.ts) do tsc \"./tslint-rules/%f\" --lib es2018 --skipLibCheck --outDir ./tslint-rules/bin",
    "lint": "npm run compile-lint-rules && tslint --config tslint.json --project tsconfig.json \"src/**/*.ts{,x}\"",
    

    当然 tslint 配置更改为

    "rulesDirectory": [
        "./tslint-rules/bin"
    ]
    

    【讨论】:

      猜你喜欢
      • 2019-10-24
      • 2016-02-04
      • 2019-07-30
      • 2020-03-24
      • 1970-01-01
      • 2019-03-12
      • 2017-11-17
      • 2018-12-31
      • 2015-02-03
      相关资源
      最近更新 更多