/** 扫源文件里有没有异常换行符(lone CR / U+2028 等),这类字符会让编译器行号和编辑器对不上。 */ import fs from 'node:fs'; const files = process.argv.slice(2); for (const file of files) { const buf = fs.readFileSync(file); const text = buf.toString('utf8'); let cr = 0; let crlf = 0; let lf = 0; let u2028 = 0; let u2029 = 0; let nul = 0; let bad = 0; for (let i = 0; i < text.length; i += 1) { const c = text[i]; if (c === '\r') { if (text[i + 1] === '\n') crlf += 1; else cr += 1; } else if (c === '\n') { lf += 1; } else if (c === '\u2028') u2028 += 1; else if (c === '\u2029') u2029 += 1; else if (c === '\u0000') nul += 1; else if (c === '\uFFFD') bad += 1; } const lines = text.split(/\r\n|\r|\n/).length; console.log(`\n${file}`); console.log(` 字节 ${buf.length} | 逻辑行数 ${lines}`); console.log(` CRLF=${crlf} 裸CR=${cr} 裸LF=${lf} U+2028=${u2028} U+2029=${u2029} NUL=${nul} 替换字符=${bad}`); }