EDITOR: Improved infinite loop checking (#1276)

This commit is contained in:
G4mingJon4s
2024-05-23 09:44:15 +02:00
committed by GitHub
parent 7ed64cbc9c
commit 08eb60d21b
3 changed files with 34 additions and 13 deletions
+11 -6
View File
@@ -199,16 +199,16 @@ function parseOnlyRamCalculate(otherScripts: Map<ScriptFilePath, Script>, code:
return { cost: ram, entries: detailedCosts.filter((e) => e.cost > 0) };
}
export function checkInfiniteLoop(code: string): number {
export function checkInfiniteLoop(code: string): number[] {
let ast: acorn.Node;
try {
ast = parse(code, { sourceType: "module", ecmaVersion: "latest" });
} catch (e) {
// If code cannot be parsed, do not provide infinite loop detection warning
return -1;
return [];
}
function nodeHasTrueTest(node: acorn.Node): boolean {
return node.type === "Literal" && "raw" in node && node.raw === "true";
return node.type === "Literal" && "raw" in node && (node.raw === "true" || node.raw === "1");
}
function hasAwait(ast: acorn.Node): boolean {
@@ -225,14 +225,19 @@ export function checkInfiniteLoop(code: string): number {
return hasAwait;
}
let missingAwaitLine = -1;
const possibleLines: number[] = [];
walk.recursive(
ast,
{},
{
WhileStatement: (node: Node, st: unknown, walkDeeper: walk.WalkerCallback<any>) => {
const previousLines = code.slice(0, node.start).trimEnd().split("\n");
const lineNumber = previousLines.length + 1;
if (previousLines[previousLines.length - 1].match(/^\s*\/\/\s*@ignore-infinite/)) {
return;
}
if (nodeHasTrueTest(node.test) && !hasAwait(node)) {
missingAwaitLine = (code.slice(0, node.start).match(/\n/g) || []).length + 1;
possibleLines.push(lineNumber);
} else {
node.body && walkDeeper(node.body, st);
}
@@ -240,7 +245,7 @@ export function checkInfiniteLoop(code: string): number {
},
);
return missingAwaitLine;
return possibleLines;
}
interface ParseDepsResult {