简体   繁体   English

在TSLint中禁用regex使用某些字符串?

[英]Disable use of some strings by regex in TSLint?

I sometimes see in the code constructs like this as a result of JavaScript converted to TypeScript: 由于JavaScript转换为TypeScript,我有时会在代码构造中看到这样的结构:

private doSomething = function (params: IParams) {
}

While the correct variant is: 虽然正确的变体是:

private doSomething (params: IParams) {
}

Former one makes it a JS function, body of which is not analyzed and is compiled as is. 前一个使它成为JS函数,其主体未被分析并按原样编译。 Latter is checked by TypeScript compiler and if there are wrong calls to, eg private methods of another class, it would throw an error. Latter由TypeScript编译器检查,如果有错误的调用,例如另一个类的私有方法,则会抛出错误。

I'd like to have a rule in TSLint which would allow me to mark as erroneous strings like (private|public|protected)\\s+(.+)=\\s*function . 我想在TSLint中有一个规则,它允许我标记为错误的字符串,如(private|public|protected)\\s+(.+)=\\s*function

So, is it possible to add a rule to TSLint, may be as an existing plugin, to search for regex in strings? 那么,是否可以向TSLint添加规则,可以作为现有插件,在字符串中搜索正则表达式?

I'm afraid it is not that easy. 我担心这不容易。 Tslint allows you to declare custom rules using Syntax Walkers not regular expressions. Tslint允许您使用Syntax Walkers而不是正则表达式声明自定义规则

Your rule can parse the TypeScript generated abstract syntax tree (AST): 您的规则可以解析TypeScript生成的抽象语法树(AST):

Given a walker, TypeScript's parser visits the AST using the visitor pattern. 给定一个walker,TypeScript的解析器使用访问者模式访问AST。 So the rule walkers only need to override the appropriate visitor methods to enforce its checks. 因此规则步行者只需要覆盖适当的访问者方法来强制执行其检查。

I'm not sure but you probably need the visitPropertyAssignment walker. 我不确定,但你可能需要visitPropertyAssignment walker。

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

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