Skip to content

Instantly share code, notes, and snippets.

@zyf0330
Created April 27, 2023 07:39
Show Gist options
  • Save zyf0330/03899a6a235420a396778aaa552f988b to your computer and use it in GitHub Desktop.
Save zyf0330/03899a6a235420a396778aaa552f988b to your computer and use it in GitHub Desktop.
fix noImplicitOverride error
const cp = require("child_process")
const fs = require("fs/promises")
const path = require("path")
try {
cp.execSync("npx tsc --noEmit")
} catch (e) {
const output = e.stdout.toString()
const regexp = /^(.+\.ts)\((\d+?),(\d+?)\): error TS411(4|6):.+/
const overrideFiles = output.split("\n").reduce((m, line) => {
const result = line.match(regexp)
if (result) {
const [, filePath, row, col] = result
if (!m.has(filePath)) {
m.set(filePath, [])
}
const poss = m.get(filePath)
poss.push({row, col})
poss.sort(({row: r1}, {row: r2}) => r1 - r2)
}
return m
}, new Map());
(async () => {
await fs.mkdir("backup", {recursive: true});
console.log("old files are moved into backup")
await Promise.all([...overrideFiles.entries()].map(async ([filePath, positions]) => {
let remainContent = (await fs.readFile(filePath)).toString()
let finalContent = ""
let lastRow = 1;
positions.forEach(({row, col}) => {
let relativeRow = row - lastRow
lastRow = row;
let offset = 0
while (relativeRow-- > 0) {
offset = remainContent.indexOf("\n", offset + 1)
}
offset += 1
finalContent += remainContent.slice(0, offset)
remainContent = remainContent.slice(offset)
offset = col - 1
{
const errorLineContent = remainContent.slice(0, offset)
const asyncPos = errorLineContent.indexOf("async ")
const hasAsync = (asyncPos > -1 && asyncPos < col)
const fixedLineContent = errorLineContent.slice(0, hasAsync ? asyncPos : col) + "override " + errorLineContent.slice(hasAsync ? asyncPos : col)
finalContent += fixedLineContent;
}
remainContent = remainContent.slice(offset)
})
finalContent += remainContent;
await fs.cp(filePath, path.join("backup", filePath))
await fs.writeFile(filePath, finalContent)
}))
})()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment