Don't rewrite it in Rust. Rewrite the 5% that's slow: profile your app, port the hot functions with your coding agent, prove they behave the same. Python, JS/TS, Ruby, Java, Kotlin, Go, PHP, C/C++, C#, R.
Rust agent-skillsai-agentsclaude-codeclaude-code-plugincodexdeveloper-toolsffimcpmcp-servernapi-rsnodejsoptimizationperformanceprofilerpyo3pythonrewrite-it-in-rustrubyrusttypescript
node: recognize regex literals in codeOnly so quotes inside regexes do not hide functions
RRFZ740b4e9d5dce
diff --git a/src/node.rs b/src/node.rsindex 609cc32..e13f5fa 100644--- a/src/node.rs+++ b/src/node.rs@@ -846,4 +846,160 @@ mod tests {let on = backend::append_marked(original, "//", &port.id, &esm);assert_eq!(backend::strip_marked(&on, "//", &port.id), original);}++ #[test]+ fn scan_handles_regex_literals_and_division_ambiguity() {+ if !Command::new("node").arg("--version").output().is_ok_and(|o| o.status.success()) {+ eprintln!("no node on PATH, skipping");+ return;+ }+ let dir = std::env::temp_dir().join(format!("fa-node-regex-lexer-{}", std::process::id()));+ let _ = fs::remove_dir_all(&dir);+ fs::create_dir_all(&dir).unwrap();+ fs::write(+ dir.join("work.mjs"),+ "export function stripQuotes(items) {\n\+ \x20 let total = 0;\n\+ \x20 for (const s of items) total += s.replace(/['\"]/g, \"\").length;\n\+ \x20 return total;\n\+ }\n\n\+ export function classWithSlash(items) {\n\+ \x20 let total = 0;\n\+ \x20 for (const s of items) total += s.replace(/[/']/g, \"-\").length;\n\+ \x20 return total;\n\+ }\n\n\+ export function returnRegex(items) {\n\+ \x20 let count = 0;\n\+ \x20 for (const s of items) if (hasQuote(s)) count++;\n\+ \x20 return count;\n\+ }\n\n\+ function hasQuote(s) {\n\+ \x20 return /'/.test(s);\n\+ }\n\n\+ export function chainedDivision(n) {\n\+ \x20 let total = 0;\n\+ \x20 const obj = { return: 100 };\n\+ \x20 for (let i = 1; i <= n; i++) {\n\+ \x20 const x = i / 2 // comment with 'quote' and \"double\"\n\+ \x20 + obj.return / 2;\n\+ \x20 total += x / 3 / 5;\n\+ \x20 }\n\+ \x20 return total;\n\+ }\n\n\+ export function templateAndComments(items) {\n\+ \x20 let total = 0;\n\+ \x20 /* block comment with 'quote' and /regex/ */\n\+ \x20 for (let k = 0; k < 4; k++) {\n\+ \x20 for (const s of items) {\n\+ \x20 const tag = `a / b / 'c' ${s}`;\n\+ \x20 total += tag.length;\n\+ \x20 }\n\+ \x20 }\n\+ \x20 return total;\n\+ }\n\n\+ export function afterRegex(n) {\n\+ \x20 let t = 0;\n\+ \x20 for (let i = 0; i < n; i++) t += (i * i) % 97;\n\+ \x20 return t;\n\+ }\n",+ )+ .unwrap();+ fs::write(+ dir.join("app.mjs"),+ "import { stripQuotes, classWithSlash, returnRegex, chainedDivision, templateAndComments, afterRegex } from './work.mjs';\n\+ const items = Array.from({ length: 2000 }, (_, i) => `a/b'c\"${i}`);\n\+ let s = 0;\n\+ for (let r = 0; r < 100; r++) {\n\+ \x20 s += stripQuotes(items);\n\+ \x20 s += classWithSlash(items);\n\+ \x20 s += returnRegex(items);\n\+ \x20 s += chainedDivision(80000);\n\+ \x20 s += templateAndComments(items);\n\+ \x20 s += afterRegex(100000);\n\+ }\n\+ console.log(s);\n",+ )+ .unwrap();+ let scanned = crate::pipeline::scan(&dir, vec!["node".into(), "app.mjs".into()], None);+ let _ = fs::remove_dir_all(&dir);+ let scanned = scanned.unwrap();+ for name in+ ["stripQuotes", "classWithSlash", "returnRegex", "chainedDivision", "templateAndComments", "afterRegex"]+ {+ let h = scanned.hotspots.iter().find(|h| h.name == name).unwrap_or_else(|| {+ panic!("missing hotspot {name}; got {:?}", scanned.hotspots.iter().map(|h| &h.name).collect::<Vec<_>>())+ });+ assert!(h.portable(), "{name} should be portable, got blockers {:?}", h.blockers);+ }+ }++ #[test]+ fn code_only_handles_regex_and_division_deterministically() {+ if !Command::new("node").arg("--version").output().is_ok_and(|o| o.status.success()) {+ eprintln!("no node on PATH, skipping");+ return;+ }+ let dir = std::env::temp_dir().join(format!("fa-node-codeonly-{}", std::process::id()));+ let _ = fs::remove_dir_all(&dir);+ fs::create_dir_all(&dir).unwrap();+ // Export codeOnly from a temporary copy of runner.mjs to assert exact blanking on tricky tokens+ // (and verify linear time on adversarial unterminated-slash lines).+ let stripped = RUNNER.split("const [mode, jobPath]").next().unwrap();+ fs::write(dir.join("lib.mjs"), format!("{stripped}\nexport {{ codeOnly, describe }};\n")).unwrap();+ fs::write(+ dir.join("check.mjs"),+ r#"import assert from "node:assert/strict";+import fs from "node:fs";+import path from "node:path";+import { codeOnly, describe } from "./lib.mjs";++// Division preserved intact+assert.equal(codeOnly("const r = a / b / c;"), "const r = a / b / c;");+assert.equal(codeOnly("const r = (a + b) / 2 / arr[0];"), "const r = (a + b) / 2 / arr[0];");+assert.equal(codeOnly("const r = obj.return / 2;"), "const r = obj.return / 2;");+assert.equal(codeOnly("const r = i++ / 2;"), "const r = i++ / 2;");++// Division followed by line comment containing quotes+const divComment = codeOnly("const x = y / 2 // it's a \"comment\"\nconst z = 1;");+assert.equal(divComment.length, "const x = y / 2 // it's a \"comment\"\nconst z = 1;".length);+assert.ok(divComment.startsWith("const x = y / 2 "));+assert.ok(divComment.endsWith("\nconst z = 1;"));++// Regex literals with quotes, slashes in character class, escapes, and after keywords+assert.equal(codeOnly("s.replace(/['\"]/g, \"\")"), "s.replace(/ /g, \"\")");+assert.equal(codeOnly("s.replace(/[/]/g, \"-\")"), "s.replace(/ /g, \" \")");+assert.equal(codeOnly("return /'/.test(s);"), "return / /.test(s);");+assert.equal(codeOnly("return /https?:\\/\\//.test(s);"), "return / /.test(s);");+assert.equal(codeOnly("const t = `a / b / 'c'`;\nconst ok = 1;"), "const t = ` `;\nconst ok = 1;");++// Escaped quotes inside regex and comments don't leak into later functions+const modFile = path.join(path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1")), "sample.mjs");+const sample = [+ "export function f1(s) { return s.replace(/['\"]/g, ''); }",+ "export function f2(s) { return s.replace(/[/]/g, '-'); }",+ "export function f3(s) { return /'/.test(s); }",+ "export function f4(a, b, c) { return a / b / c; }",+ "export function f5(y) { const x = y / 2; // don't break\n return x; }",+ "export function f6(s) { const t = `a / b`; return t + s; }",+].join("\n") + "\n";+fs.writeFileSync(modFile, sample);+const root = path.dirname(modFile);+for (const [line0, name] of [[0, "f1"], [1, "f2"], [2, "f3"], [3, "f4"], [4, "f5"], [6, "f6"]]) {+ const info = describe(modFile, line0, name, null, root);+ assert.ok(info, `describe missed ${name}`);+ assert.deepEqual(info.blockers, [], `${name} had unexpected blockers: ${info.blockers}`);+}++// Adversarial input with many slashes stays linear and preserves length+const worst = "return " + "/ ".repeat(20000) + "\nconst ok = 1;";+const res = codeOnly(worst);+assert.equal(res.length, worst.length);+assert.ok(res.endsWith("\nconst ok = 1;"));+"#,+ )+ .unwrap();+ let out = Command::new("node").arg("check.mjs").current_dir(&dir).output().unwrap();+ let _ = fs::remove_dir_all(&dir);+ assert!(out.status.success(), "{}", combined(&out));+ }}diff --git a/src/node/runner.mjs b/src/node/runner.mjsindex 435472f..6486bf6 100644--- a/src/node/runner.mjs+++ b/src/node/runner.mjs@@ -15,13 +15,32 @@ const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");// ------------------------------------------------------------------ source scanning-/** The source with string contents and comments blanked out (same length, same lines). */+const REGEX_KEYWORDS = new Set([+ "return",+ "case",+ "throw",+ "in",+ "of",+ "typeof",+ "void",+ "delete",+ "new",+ "yield",+ "await",+ "else",+ "do",+]);++/** The source with string/regex contents and comments blanked out (same length, same lines). */function codeOnly(src) {const out = src.split("");const blank = (a, b) => {for (let k = a; k < b && k < out.length; k++) if (out[k] !== "\n") out[k] = " ";};let i = 0;+ let regexAllowed = true;+ let afterDot = false;+ let noRegexBefore = -1;while (i < src.length) {const c = src[i];if (c === "/" && src[i + 1] === "/") {@@ -37,7 +56,70 @@ function codeOnly(src) {while (j < src.length && src[j] !== c) j += src[j] === "\\" ? 2 : 1;blank(i + 1, j);i = j + 1;- } else i++;+ regexAllowed = false;+ afterDot = false;+ } else if (c === "/" && regexAllowed && i >= noRegexBefore) {+ let j = i + 1;+ let inClass = false;+ let closed = false;+ while (j < src.length) {+ const ch = src[j];+ if (ch === "\n" || ch === "\r") break;+ if (ch === "\\") {+ if (j + 1 >= src.length || src[j + 1] === "\n" || src[j + 1] === "\r") break;+ j += 2;+ continue;+ }+ if (ch === "[") inClass = true;+ else if (ch === "]") inClass = false;+ else if (ch === "/" && !inClass) {+ closed = true;+ break;+ }+ j++;+ }+ if (closed) {+ blank(i + 1, j);+ j++;+ while (j < src.length && /[A-Za-z]/.test(src[j])) j++;+ i = j;+ regexAllowed = false;+ afterDot = false;+ } else {+ // Unterminated on this line: fall back to treating `/` as division and don't rescan the same tail.+ if (!inClass) noRegexBefore = j;+ i++;+ regexAllowed = true;+ afterDot = false;+ }+ } else if (c === " " || c === "\t" || c === "\r" || c === "\n") {+ i++;+ } else if (/[A-Za-z_$]/.test(c)) {+ let j = i + 1;+ while (j < src.length && /[\w$]/.test(src[j])) j++;+ const word = src.slice(i, j);+ regexAllowed = !afterDot && REGEX_KEYWORDS.has(word);+ afterDot = false;+ i = j;+ } else if (c >= "0" && c <= "9") {+ let j = i + 1;+ while (j < src.length && /[\w.]/.test(src[j])) j++;+ regexAllowed = false;+ afterDot = false;+ i = j;+ } else if ((c === "+" && src[i + 1] === "+") || (c === "-" && src[i + 1] === "-")) {+ regexAllowed = false;+ afterDot = false;+ i += 2;+ } else if (c === ".") {+ regexAllowed = false;+ afterDot = true;+ i++;+ } else {+ regexAllowed = c !== ")" && c !== "]";+ afterDot = false;+ i++;+ }}return out.join("");}