|
| 1 | +// Find the *first* bad line in a module file. |
| 2 | +const fs = require('fs'); |
| 3 | + |
| 4 | +const prefix = (n) => `import { default as x } from 'data:text/javascript,';`; // not used; keep imports intact |
| 5 | + |
| 6 | +const src = fs.readFileSync('C:/Users/punko/Downloads/PoMiniGames/served-game.js', 'utf8'); |
| 7 | +const lines = src.split('\n'); |
| 8 | + |
| 9 | +async function tryImport(s) { |
| 10 | + const tmp = 'C:/Users/punko/Downloads/PoMiniGames/scripts/_tmp2.mjs'; |
| 11 | + fs.writeFileSync(tmp, s); |
| 12 | + try { |
| 13 | + await import('file://' + tmp.replace(/\\/g, '/')); |
| 14 | + return 'OK'; |
| 15 | + } catch (e) { |
| 16 | + return e.message; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +(async () => { |
| 21 | + // Just binarily search the source — the parse fails at the IMPORT-prefix |
| 22 | + // boundary until we have a complete module. |
| 23 | + let lo = 1, hi = lines.length; |
| 24 | + while (hi - lo > 1) { |
| 25 | + const mid = (lo + hi) >> 1; |
| 26 | + // Slice to lines [0..mid] — enough to bisect. |
| 27 | + // We need a complete declaration by the end, but bisection needs only |
| 28 | + // parseable-at-all characters. So we close any open braces/brackets. |
| 29 | + const prefix = lines.slice(0, mid).join('\n'); |
| 30 | + const res = await tryImport(prefix); |
| 31 | + if (res === 'OK') lo = mid; |
| 32 | + else hi = mid; |
| 33 | + } |
| 34 | + console.log('hi =', hi); |
| 35 | + console.log('error at hi lines =', await tryImport(lines.slice(0, hi).join('\n'))); |
| 36 | + for (let i = Math.max(0, hi - 6); i < Math.min(lines.length, hi + 3); i++) { |
| 37 | + console.log(`${i + 1}: ${lines[i].slice(0, 200)}`); |
| 38 | + } |
| 39 | +})().catch(e => console.error(e)); |
0 commit comments