(Body Snatcher) Pass 3

This commit is contained in:
AlexKucera committed 2026-08-13 07:25:35 +02:00
1 parent 66ff103b6a
commit 2bd65880c9
36 files changed
+6416

No files matched your search

+29
View File
@@ -0,0 +1,29 @@
const fs=require('fs');
const espree=require('/opt/homebrew/lib/node_modules/eslint/node_modules/espree/dist/espree.cjs');
const src=fs.readFileSync('index.html','utf8');
const m=src.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i);
const code=m[1];
const base=src.slice(0,src.indexOf('<script')).split('\n').length;
const toks=espree.tokenize(code,{ecmaVersion:2022,loc:true});
function balanced(subset){const st=[];const open={')':'(',']':'[','}':'{'};for(const t of subset){const v=t.value;if(v==='{'||v==='('||v==='[')st.push(v);else if(v==='}'||v===')'||v===']'){if(!st.length||st[st.length-1]!==open[v])return false;st.pop();}}return st.length===0;}
const codeLines=code.split('\n');
function removeBraceAt(line,col){ // 1-based line, 0-based col in script
const nl=codeLines.slice();
nl[line-1]=nl[line-1].slice(0,col)+nl[line-1].slice(col+1);
return nl.join('\n');
}
function parses(c){try{espree.parse(c,{ecmaVersion:2022,sourceType:'script',allowReturnOutsideFunction:true});return true;}catch(e){return false;}}
// collect ALL { removal candidates that balance the count, then test-parse
let found=[];
for(let i=0;i<toks.length;i++){
if(toks[i].value==='{'){
if(balanced(toks.slice(0,i).concat(toks.slice(i+1)))){
const L=toks[i].loc.start.line, C=toks[i].loc.start.column;
const testCode=removeBraceAt(L,C);
if(parses(testCode)){ found.push({line:L,col:C,file:L+base-1}); }
}
}
}
console.log('=== { removals that make it PARSE (the culprit) ===');
for(const f of found){ console.log(' script-line '+f.line+' col '+f.col+' FILE line '+f.file+' : '+JSON.stringify(codeLines[f.line-1])); }
// Also try INSERTING a } before the final IIFE close (alternative fix): find where