javascript - 在 TSLint 中禁用正则表达式对某些字符串的使用?

标签 javascript typescript tslint

我有时会在代码中看到这样的结构,这是将 JavaScript 转换为 TypeScript 的结果:

private doSomething = function (params: IParams) {
}

虽然正确的变体是:

private doSomething (params: IParams) {
}

前者做成JS函数,函数体不解析,直接编译。 后者由 TypeScript 编译器检查,如果有错误的调用,例如另一个类的私有(private)方法,它会抛出错误。

我想在 TSLint 中有一个规则,它允许我将错误字符串标记为 (private|public|protected)\s+(.+)=\s*function

那么,是否可以将规则添加到 TSLint,作为现有插件,以在字符串中搜索正则表达式?

最佳答案

恐怕没那么容易。 Tslint 允许您声明 custom rules使用 Syntax Walkers不是正则表达式。

您的规则可以解析 TypeScript 生成的抽象语法树 (AST):

Given a walker, TypeScript's parser visits the AST using the visitor pattern. So the rule walkers only need to override the appropriate visitor methods to enforce its checks.

我不确定,但您可能需要 visitPropertyAssignment沃克。

关于javascript - 在 TSLint 中禁用正则表达式对某些字符串的使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40927303/

相关文章:

angular - 如何在 Intellij 中执行自动 Tslint

typescript - Lint 为什么不允许赋值 null?

javascript - 如何减少 DOM 操作?

javascript - 在javascript中递归复制二维数组的算法

constructor - TypeScript 类成员速记错误?

angular - 有 Angular 迁移 TSLint 到 ESLint Banana-In-Box 错误

javascript - 如何在 benchmark.js 中为每个测试添加设置和拆卸

javascript - D3 强制布局 : How do I set the size of every node?

html - 从 @Input 或对象属性渲染 CSS

javascript - Typescript 动态从对象推断类型