const IDENT_START = /[A-Za-z_$]/; const IDENT_PART = /[A-Za-z0-9_$]/; export function rewriteGeneratedVarKeywords(code) { const assignmentCounts = countAssignments(stripStringsAndComments(code)); let result = ''; let i = 0; while (i < code.length) { const next = skipLiteral(code, i); if (next > i) { result += code.slice(i, next); i = next; continue; } if (isVarKeywordAt(code, i)) { const bindings = readVarBindings(code, i + 3); const forBinding = isForHeaderBinding(code, i); const useLet = !forBinding && bindings.some( (binding) => binding.uninitialized || (assignmentCounts.get(binding.name) || 0) > 1 ); result += useLet ? 'let' : 'const'; i += 3; continue; } result += code[i]; i += 1; } return result; } function countAssignments(code) { const counts = new Map(); const bump = (name) => counts.set(name, (counts.get(name) || 0) + 1); const assign = /(?>=|>>>=|=(?![=>]))/g; for (const match of code.matchAll(assign)) { bump(match[1]); } const prefix = /(?:\+\+|--)\s*([A-Za-z_$][\w$]*)/g; for (const match of code.matchAll(prefix)) { bump(match[1]); } return counts; } function stripStringsAndComments(code) { let out = ''; let i = 0; while (i < code.length) { const next = skipLiteral(code, i); if (next > i) { out += ' '.repeat(next - i); i = next; continue; } out += code[i]; i += 1; } return out; } function skipLiteral(code, i) { if (code.startsWith('//', i)) { const newline = code.indexOf('\n', i); return newline === -1 ? code.length : newline + 1; } if (code.startsWith('/*', i)) { const end = code.indexOf('*/', i + 2); return end === -1 ? code.length : end + 2; } const quote = code[i]; if (quote === "'" || quote === '"') { i += 1; while (i < code.length) { if (code[i] === '\\') { i += 2; continue; } if (code[i] === quote) { return i + 1; } i += 1; } return code.length; } if (quote === '`') { i += 1; while (i < code.length) { if (code[i] === '\\') { i += 2; continue; } if (code.startsWith('${', i)) { i += 2; let depth = 1; while (i < code.length && depth > 0) { const nested = skipLiteral(code, i); if (nested > i) { i = nested; continue; } if (code[i] === '{') { depth += 1; } else if (code[i] === '}') { depth -= 1; } if (depth > 0) { i += 1; } } continue; } if (code[i] === '`') { return i + 1; } i += 1; } return code.length; } return i; } function isVarKeywordAt(code, i) { if (!code.startsWith('var', i)) { return false; } if (i > 0 && IDENT_PART.test(code[i - 1])) { return false; } return i + 3 >= code.length || !IDENT_PART.test(code[i + 3]); } function isForHeaderBinding(code, varIndex) { let i = varIndex; while (i > 0 && /\s/.test(code[i - 1])) { i -= 1; } if (i === 0 || code[i - 1] !== '(') { return false; } i -= 1; while (i > 0 && /\s/.test(code[i - 1])) { i -= 1; } return /(?:^|[^A-Za-z0-9_$])for$/.test(code.slice(Math.max(0, i - 4), i)); } function readVarBindings(code, i) { const bindings = []; i = skipWs(code, i); while (i < code.length && IDENT_START.test(code[i])) { const start = i; i += 1; while (i < code.length && IDENT_PART.test(code[i])) { i += 1; } const name = code.slice(start, i); i = skipWs(code, i); let uninitialized = true; if (code[i] === '=') { uninitialized = false; i = skipInitializer(code, i + 1); i = skipWs(code, i); } bindings.push({ name, uninitialized }); if (code[i] !== ',') { break; } i = skipWs(code, i + 1); } return bindings; } function skipInitializer(code, i) { let depth = 0; while (i < code.length) { const next = skipLiteral(code, i); if (next > i) { i = next; continue; } const char = code[i]; if ((char === '(' || char === '[' || char === '{') ) { depth += 1; } else if (char === ')' || char === ']' || char === '}') { depth = Math.max(0, depth - 1); } else if ((char === ',' || char === ';') && depth === 0) { return i; } i += 1; } return i; } function skipWs(code, i) { while (i < code.length && /\s/.test(code[i])) { i += 1; } return i; }