22 lines
1.1 KiB
JavaScript
22 lines
1.1 KiB
JavaScript
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});
|
|
const stack=[]; // {ch, line, col}
|
|
const open={'}':'{',']':'[',')':'('};
|
|
let extra=null;
|
|
for(const t of toks){
|
|
const v=t.value;
|
|
if(v==='{'||v==='('||v==='['){ stack.push({ch:v,line:t.loc.start.line,col:t.loc.start.column}); }
|
|
else if(v==='}'||v===')'||v===']'){
|
|
// pop until matching
|
|
if(stack.length && stack[stack.length-1].ch===open[v]){ stack.pop(); }
|
|
else { console.log('EXTRA close '+v+' at script-line '+t.loc.start.line+' (file '+(t.loc.start.line+base-1)+')'); extra=v; }
|
|
}
|
|
}
|
|
console.log('=== unmatched openers remaining (top = most recent) ===');
|
|
for(const s of stack.slice().reverse()){ console.log(s.ch+' at script-line '+s.line+' col '+s.col+' => FILE line '+(s.line+base-1)); }
|