26 lines
1.4 KiB
JavaScript
26 lines
1.4 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 codeLines=code.split('\n');
|
|
function parses(c){try{espree.parse(c,{ecmaVersion:2022,sourceType:'script',allowReturnOutsideFunction:true});return true;}catch(e){return false;}}
|
|
// also test inserting } right before the final })(); (i.e., extra close for the IIFE body)
|
|
let hits=[];
|
|
for(let L=1;L<=codeLines.length;L++){
|
|
// insert '}' after end of line L
|
|
const mod=codeLines.slice(0,L).join('\n')+'\n}\n'+codeLines.slice(L).join('\n');
|
|
if(parses(mod)) hits.push({type:'}', line:L, file:L+base-1, ctx:codeLines[L-1]});
|
|
}
|
|
// test inserting ')' similarly (rare)
|
|
let phits=[];
|
|
for(let L=1;L<=codeLines.length;L++){
|
|
const mod=codeLines.slice(0,L).join('\n')+'\n)\n'+codeLines.slice(L).join('\n');
|
|
if(parses(mod)) phits.push({line:L, file:L+base-1, ctx:codeLines[L-1]});
|
|
}
|
|
console.log('} insertions that PARSE: '+hits.length);
|
|
for(const h of hits.slice(0,12)) console.log(' +} after FILE line '+h.file+' :: '+JSON.stringify(h.ctx));
|
|
console.log(') insertions that PARSE: '+phits.length);
|
|
for(const h of phits.slice(0,6)) console.log(' +) after FILE line '+h.file+' :: '+JSON.stringify(h.ctx));
|