gpt4 book ai didi

javascript - 如果在循环内部使用,则强制将 getter 结果存储在变量中

转载 作者:搜寻专家 更新时间:2023-10-30 21:44:57 26 4
gpt4 key购买 nike

考虑下课

class Smth {
get functionWithSomeVeryUniqueName() {
// Some heavy calculations are here
return obj => obj; // Actually uses some vars from closure calculated above
}
}

我想在循环内访问 getter 时出现 tslint 错误。

我。 e.以下任何行都应被视为错误:

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 规则来做这样的事情?

请注意,访问中的名称检查和点就足够了,我不需要确保函数属于某个具体类。

最佳答案

似乎我制定了相应的规则(demo on 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))

关于javascript - 如果在循环内部使用,则强制将 getter 结果存储在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57146862/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com