From f38359e43d8ade3924956d63d441a0529047b3c7 Mon Sep 17 00:00:00 2001 From: Casey Link Date: Tue, 15 Apr 2025 12:15:30 +0200 Subject: [PATCH] Add regex for detecting IIFEs as expressions to prevent broken returns. --- library/src/engine/engine.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/library/src/engine/engine.ts b/library/src/engine/engine.ts index 7e23d19d..a9599b34 100644 --- a/library/src/engine/engine.ts +++ b/library/src/engine/engine.ts @@ -291,6 +291,11 @@ function genRX( // double quotes "(\\"|[^\"])*" // single quotes '(\\'|[^'])*' // ticks `(\\`|[^`])*` + // iife \(\s*((function)\s*\(\s*\)|(\(\s*\))\s*=>)\s*(?:\{[\s\S]*?\}|[^;)\{]*)\s*\)\s*\(\s*\) + // + // The iife support is (intentionally) limited. It only supports + // function and arrow syntax with no arguments, and does not support nested + // IIFEs. // // We also want to match the non delimiter part of statements // note we only support ; statement delimiters: @@ -298,7 +303,7 @@ function genRX( // [^;] // const statementRe = - /(\/(\\\/|[^\/])*\/|"(\\"|[^\"])*"|'(\\'|[^'])*'|`(\\`|[^`])*`|[^;])+/gm + /(\/(\\\/|[^\/])*\/|"(\\"|[^\"])*"|'(\\'|[^'])*'|`(\\`|[^`])*`|\(\s*((function)\s*\(\s*\)|(\(\s*\))\s*=>)\s*(?:\{[\s\S]*?\}|[^;)\{]*)\s*\)\s*\(\s*\)|[^;])+/gm const statements = ctx.value.trim().match(statementRe) if (statements) { const lastIdx = statements.length - 1