31 lines
1.4 KiB
JavaScript
31 lines
1.4 KiB
JavaScript
const fs=require('fs'),vm=require('vm');
|
|
const src=fs.readFileSync('index.html','utf8');
|
|
const m=src.match(/<script\b[^>]*>([\s\S]*?)<\/script>/i);
|
|
const code=m[1]; const lines=code.split('\n');
|
|
function test(E){
|
|
const full=lines.slice(0,E).join('\n')+'\n\n})();'; // close appended at line E+2
|
|
try{ new vm.Script(full,{filename:'g.js'}); return {ok:true}; }
|
|
catch(e){ const first=e.stack.split('\n')[0]; const lm=first.match(/g\.js:(\d+)/); return {ok:false, line: lm?+lm[1]:null, msg:e.message, first}; }
|
|
}
|
|
// fine scan: find smallest E (try every line near suspected region) where error line == E+2 (close error => bug at/before E)
|
|
// First, sample to find region: walk E, classify closeErr if line===E+2
|
|
let firstCloseErr=null, lastOk=null;
|
|
for(let E=100; E<=lines.length; E+=10){
|
|
const r=test(E);
|
|
if(r.ok){ lastOk=E; continue; }
|
|
if(r.line===E+2){ if(firstCloseErr===null) firstCloseErr=E; }
|
|
}
|
|
console.log('last balanced (OK) at E='+lastOk);
|
|
console.log('first close-error (bug at/before E) at E='+firstCloseErr);
|
|
// narrow between lastOk and firstCloseErr line by line
|
|
if(lastOk!==null && firstCloseErr!==null){
|
|
for(let E=lastOk; E<=firstCloseErr; E++){
|
|
const r=test(E);
|
|
if(!r.ok && r.line===E+2){ console.log('>>> unclosed brace introduced at/before script-line '+E+' (file line '+(E+17)+')');
|
|
// show context
|
|
for(let k=Math.max(0,E-3);k<=E;k++) console.log(' '+(k+1)+'|'+(lines[k]||''));
|
|
break;
|
|
}
|
|
}
|
|
}
|