在 TSLint 中禁用正则表达式对某些字符串的使用?
Disable use of some strings by regex in TSLint?
由于 JavaScript 转换为 TypeScript,我有时会在代码中看到这样的结构:
private doSomething = function (params: IParams) {
}
而正确的变体是:
private doSomething (params: IParams) {
}
前者做成JS函数,函数体不解析,直接编译。
后者由 TypeScript 编译器检查,如果有错误的调用,例如另一个 class 的私有方法,它会抛出错误。
我想在 TSLint 中有一个规则,它允许我将错误字符串标记为 (private|public|protected)\s+(.+)=\s*function
。
那么,是否可以向 TSLint 添加一个规则,可以作为现有插件,在字符串中搜索正则表达式?
恐怕没那么容易。 Tslint 允许您声明 custom rules using 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 转换为 TypeScript,我有时会在代码中看到这样的结构:
private doSomething = function (params: IParams) {
}
而正确的变体是:
private doSomething (params: IParams) {
}
前者做成JS函数,函数体不解析,直接编译。 后者由 TypeScript 编译器检查,如果有错误的调用,例如另一个 class 的私有方法,它会抛出错误。
我想在 TSLint 中有一个规则,它允许我将错误字符串标记为 (private|public|protected)\s+(.+)=\s*function
。
那么,是否可以向 TSLint 添加一个规则,可以作为现有插件,在字符串中搜索正则表达式?
恐怕没那么容易。 Tslint 允许您声明 custom rules using 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
助行器。