mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2026-04-20 16:22:56 +02:00
EDITOR: Improved infinite loop checking (#1276)
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user