Skip to content

Instantly share code, notes, and snippets.

@john-griffin
Forked from zorji/auto-ts-ignore.ts
Created October 5, 2023 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save john-griffin/5d91f0ad863bf68e240287909b0f1744 to your computer and use it in GitHub Desktop.
Save john-griffin/5d91f0ad863bf68e240287909b0f1744 to your computer and use it in GitHub Desktop.
A script to auto add // @ts-ignore to lines with TypeScript compilation error
/**
* 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