improved sanity check

This commit is contained in:
David Meincke
2023-06-02 11:10:10 +02:00
parent 221183962c
commit 74f812c5aa
2 changed files with 5 additions and 5 deletions
+3 -3
View File
@@ -1,8 +1,8 @@
export function sanityConvert(code: string, maxLoops: number = 2000) {
var beforeConidition = "var conditionCount = " + maxLoops + "; \n";
var beforeCondition = "var conditionCount = " + maxLoops + "; \n";
var insideCondition = "\n --conditionCount; \n if (conditionCount <= 0) { break; } \n";
const regex = /(while|for|foreach)(\s*\([^)]*\)\s*)({[^}]*}|[^{;]*;?)/gs;
const regex = /(?<!function.*)(while|for|if)(\s*\((?:[^()]|\([^()]*\))*\)\s*)(.*?{[^}]*}|.*?;)/gs;
const output = code.replace(regex, (match, p1, p2, p3) => {
let statement = p3.trim();
if (statement.startsWith('{') && statement.endsWith('}')) {
@@ -11,7 +11,7 @@ export function sanityConvert(code: string, maxLoops: number = 2000) {
statement = statement.endsWith(';') ? statement.slice(0, -1) : statement;
statement = `{${insideCondition}${statement};}`;
}
return `${beforeConidition}${p1}${p2}${statement}`;
return `${beforeCondition}${p1}${p2}${statement}`;
});
return output;
}