-
-
Save jorgeborges/ddd6333cae2818444ed6fcc390da3361 to your computer and use it in GitHub Desktop.
A script to auto add // @ts-ignore to lines with TypeScript compilation error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A script to auto add // @ts-ignore to lines with TypeScript compilation error | |
* Example usage: | |
* $ npx tsc > compilation-errors.log | |
* $ npx ts-node auto-ts-ignore.ts compilation-errors.log | |
*/ | |
import { readFile, writeFile } from 'fs/promises' | |
const errorLogFile = process.argv[2] | |
async function main() { | |
const errLineRegex = /(.+)\((\d+),\d+\): error/ | |
const errorLog = await readFile(errorLogFile, { encoding: 'utf-8' }) | |
const files: Record<string, number[]> = {} | |
for (const line of errorLog.split('\n')) { | |
const matched = errLineRegex.exec(line) | |
if (matched !== null) { | |
const filename = matched[1] | |
const lineNumber = parseInt(matched[2], 10) | |
files[filename] = files[filename] || [] | |
files[filename].push(lineNumber) | |
} | |
} | |
for (const filename in files) { | |
const lineNumbers = files[filename] | |
const fileContent = (await readFile(filename, { encoding: 'utf-8' })).split('\n') | |
// lineNumber is 1-based index | |
const affectedLineContent: string[] = lineNumbers.map(lineNumber => fileContent[lineNumber - 1]) | |
lineNumbers.forEach((lineNumber, index) => { | |
const lineContent = affectedLineContent[index] | |
const numLeadingSpaces = lineContent.search(/\S/) | |
const tsIgnoreContent = `${' '.repeat(numLeadingSpaces)}// @ts-ignore` | |
fileContent.splice(index + lineNumber - 1, 0, tsIgnoreContent) | |
}) | |
await writeFile(filename, fileContent.join('\n'), { encoding: 'utf-8' }) | |
} | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment