javascript - 如果在循环内部使用 getter 结果,则强制将其存储在变量中
问题描述
考虑下课
class Smth {
get functionWithSomeVeryUniqueName() {
// Some heavy calculations are here
return obj => obj; // Actually uses some vars from closure calculated above
}
}
我希望对循环内的 getter 的任何访问都出现 tslint 错误。
IE。以下任何一行都应该被认为是坏的:
for (var x of a) smth.functionWithSomeVeryUniqueName(x);
a.forEach(x => smth.functionWithSomeVeryUniqueName(x))
a.map(x => smth.functionWithSomeVeryUniqueName(x))
for (var q=0; q<a.length; ++q) smth.functionWithSomeVeryUniqueName(x);
以及以下任何一项 - 好:
var functionWithSomeVeryUniqueName = smth.functionWithSomeVeryUniqueName;
for (var x of a) functionWithSomeVeryUniqueName(x);
a.forEach(x => functionWithSomeVeryUniqueName(x))
a.map(x => functionWithSomeVeryUniqueName(x))
for (var q=0; q<a.length; ++q) functionWithSomeVeryUniqueName(x);
这个也很好,因为参数只计算一次:
a.map(smth.functionWithSomeVeryUniqueName)
并且在循环之外调用应该是有效的:
var x = smth.functionWithSomeVeryUniqueName(mySingleObject)
可以配置什么 tslint 规则来做这样的事情?
请注意,名称检查和访问中的点就足够了,我不需要确保该函数属于某个具体类。
解决方案
好像我已经制定了相应的规则(在 AstExplorer 上演示):
import * as Lint from "tslint";
import * as ts from "typescript";
const arrayMethods = new Set(["find", "findIndex", "sort", "forEach", "filter", "flatMap", "map", "every", "some", "reduce", "reduceRight"]);
export class Rule extends Lint.Rules.AbstractRule {
public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new DisallowGetterInsideOfTheLoopWalker(sourceFile, this.getOptions()));
}
}
class DisallowGetterInsideOfTheLoopWalker extends Lint.RuleWalker {
private loops = 0;
private names: Set<string>;
constructor(sourceFile, options) {
super(sourceFile, options);
this.loops = 0;
this.names = new Set(["functionWithSomeVeryUniqueName"] /* options.ruleArguments */);
}
public visitCallExpression(node: ts.CallExpression) {
var isLoop = node.expression.kind === ts.SyntaxKind.PropertyAccessExpression && arrayMethods.has(node.expression.name.text);
this.loops += isLoop as any;
super.visitPropertyAccessExpression(node);
this.loops -= isLoop as any;
}
public visitPropertyAccessExpression(node: ts.PropertyAccessExpression) {
if (this.loops && this.names.has(node.name.text) && (this.loops > 1 || node.parent.kind === ts.SyntaxKind.CallExpression && node.parent.expression === node)) {
this.addFailure(this.createFailure(node.name.pos, node.name.end - node.name.pos, `Do not get ${node.name.text} inside of the loop`));
}
super.visitPropertyAccessExpression(node);
}
public visitForOfStatement(node: ts.ForOfStatement) {
this.loops += 2;
super.visitForOfStatement(node);
this.loops -= 2;
}
public visitForInStatement(node: ts.ForInStatement) {
this.loops += 2;
super.visitForInStatement(node);
this.loops -= 2;
}
public visitForStatement(node: ts.ForStatement) {
this.loops += 2;
super.visitForStatement(node);
this.loops -= 2;
}
public visitDoStatement(node: ts.DoStatement) {
this.loops += 2;
super.visitDoStatement(node);
this.loops -= 2;
}
public visitWhileStatement(node: ts.WhileStatement) {
this.loops += 2;
super.visitWhileStatement(node);
this.loops -= 2;
}
}
无效案例:
for (var x of a) smth.functionWithSomeVeryUniqueName(x);
for (var x in obj) smth.functionWithSomeVeryUniqueName(x);
a.forEach(x => smth.functionWithSomeVeryUniqueName(x))
a.map(x => smth.functionWithSomeVeryUniqueName(x))
a.map(x => smth.functionWithSomeVeryUniqueName<T>(x))
for (var q=0; q<a.length; ++q) smth.functionWithSomeVeryUniqueName(x);
do smth.functionWithSomeVeryUniqueName(x); while (0)
while (1) smth.functionWithSomeVeryUniqueName(x);
while (1) (smth.functionWithSomeVeryUniqueName)(x);
while (1) var f = smth.functionWithSomeVeryUniqueName;
while (1) (smth as any).functionWithSomeVeryUniqueName;
有效案例:
var functionWithSomeVeryUniqueName = smth.functionWithSomeVeryUniqueName;
for (var x of a) functionWithSomeVeryUniqueName(x);
for (var x in obj) functionWithSomeVeryUniqueName(x);
a.forEach(x => functionWithSomeVeryUniqueName(x))
a.map(x => functionWithSomeVeryUniqueName(x))
for (var q=0; q<a.length; ++q) functionWithSomeVeryUniqueName(x);
do functionWithSomeVeryUniqueName(x); while (0)
while (1) functionWithSomeVeryUniqueName(x);
while (1) (functionWithSomeVeryUniqueName)(x);
a.map(smth.functionWithSomeVeryUniqueName)
smth.functionWithSomeVeryUniqueName(mySingleObject)
不需要的有效案例:
a.map(x => (smth.functionWithSomeVeryUniqueName)(x))
推荐阅读
- powershell - 使用 powershell 进行站点向上或向下检查
- python - 给定带有 Facebook 帖子的数据集的 Python 情绪分析
- ruby-on-rails - 如何在 Active Admin 的批处理操作中使用条件语句?
- python - 使用 pyspark 的 Py4JJavaError 决策树预测
- python - “表达式”对象没有属性“秒”
- laravel - Laravel 中间件未附加到某些请求
- java - 在测试类中初始化变量时,在java中使用父引用类型访问变量
- javascript - 移动菜单的切换无法正常工作
- python - 将多个小字符串连接成一个长字符串
- swift - 如何在 Alamofire 多部分数据上传中将空间作为空间而不是 %20 发送?