Last active
July 10, 2020 06:39
-
-
Save mizchi/ed3893c95986b6a1c5f47cbcd5ad7ee3 to your computer and use it in GitHub Desktop.
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
import * as ts from "typescript"; | |
import { uniq } from "lodash"; | |
import fs from "fs"; | |
import path from "path"; | |
function compile(fileNames: string[], options: ts.CompilerOptions): void { | |
let program = ts.createProgram(fileNames, options); | |
let emitResult = program.emit(); | |
let allDiagnostics = ts | |
.getPreEmitDiagnostics(program) | |
.concat(emitResult.diagnostics); | |
let errorLines: { [k: string]: number[] } = {}; | |
allDiagnostics.forEach((diagnostic) => { | |
if (diagnostic.file) { | |
let { line } = diagnostic.file.getLineAndCharacterOfPosition( | |
diagnostic.start! | |
); | |
let last = errorLines[diagnostic.file.fileName]; | |
console.log(last); | |
if (last == null) { | |
last = errorLines[diagnostic.file.fileName] = []; | |
} | |
errorLines[diagnostic.file.fileName] = uniq([...last, line]); | |
} | |
}); | |
console.log("", errorLines); | |
Object.entries(errorLines).map(([file, errors]) => { | |
const content = fs.readFileSync(file, "utf-8"); | |
const lines = content.split("\n"); | |
const newLines = lines.map((line, idx) => { | |
const m = line.match(/^\s*/); | |
let head = ""; | |
if (m) { | |
head = m[0]; | |
} | |
if (errors.includes(idx)) { | |
return `${head}// @ts-expect-error\n${line}`; | |
} else { | |
return line; | |
} | |
}); | |
const out = newLines.join("\n"); | |
// 確認したら writeFileSync にする | |
console.log(out); | |
// fs.writeFileSync(file, out); | |
}); | |
} | |
const configPath = path.join(process.cwd(), "tsconfig.json"); | |
const tsconfig = ts.parseConfigFileTextToJson( | |
configPath, | |
fs.readFileSync(configPath, "utf-8") | |
); | |
const files = process.argv.slice(2); | |
compile(process.argv.slice(2), tsconfig.config!); | |
// $ npx ts-node script/add-expect-error.ts src/**/*.ts |
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
import * as ts from "typescript"; | |
import { uniq } from "lodash"; | |
import fs from "fs"; | |
import path from "path"; | |
type ErrorLinesMap = { [k: string]: number[] }; | |
function rewriteToTS(files: string[]) { | |
files.map((f) => { | |
fs.renameSync(f, f.replace(/\.js$/, ".ts")); | |
console.log("rename", f, f.replace(/\.js$/, ".ts")); | |
}); | |
} | |
function compile( | |
tsFileNames: string[], | |
options: ts.CompilerOptions | |
): ErrorLinesMap { | |
const program = ts.createProgram(tsFileNames, options); | |
const emitResult = program.emit(); | |
const allDiagnostics = ts | |
.getPreEmitDiagnostics(program) | |
.concat(emitResult.diagnostics); | |
const errorLines: ErrorLinesMap = {}; | |
allDiagnostics.forEach((diagnostic) => { | |
if (diagnostic.file) { | |
let { line } = diagnostic.file.getLineAndCharacterOfPosition( | |
diagnostic.start! | |
); | |
let last = errorLines[diagnostic.file.fileName]; | |
console.log(last); | |
if (last == null) { | |
last = errorLines[diagnostic.file.fileName] = []; | |
} | |
errorLines[diagnostic.file.fileName] = uniq([...last, line]); | |
} | |
}); | |
return errorLines; | |
} | |
function rewriteFiles(errorLines: ErrorLinesMap) { | |
Object.entries(errorLines).map(([file, errors]) => { | |
const content = fs.readFileSync(file, "utf-8"); | |
const lines = content.split("\n"); | |
const newLines = lines.map((line, idx) => { | |
const m = line.match(/^\s*/); | |
let head = ""; | |
if (m) { | |
head = m[0]; | |
} | |
if (errors.includes(idx)) { | |
return `${head}// @ts-expect-error\n${line}`; | |
} else { | |
return line; | |
} | |
}); | |
const out = newLines.join("\n"); | |
// 動作確認したら writeFileSync にする | |
// console.log(out); | |
fs.writeFileSync(file, out); | |
console.log("rename", file, file.replace(/\.js$/, ".ts")); | |
}); | |
} | |
const configPath = path.join(process.cwd(), "tsconfig.json"); | |
const tsconfig = ts.parseConfigFileTextToJson( | |
configPath, | |
fs.readFileSync(configPath, "utf-8") | |
); | |
tsconfig.config!.compilerOptions!.noEmit = true; | |
console.log(tsconfig.config); | |
const files = process.argv.slice(2); | |
rewriteToTS(files); | |
const tsFiles = files.map((f) => f.replace(".js", ".ts")); | |
const errors = compile(tsFiles, tsconfig.config!); | |
rewriteFiles(errors); |
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
import * as ts from "typescript"; | |
import { uniq } from "lodash"; | |
import fs from "fs"; | |
import path from "path"; | |
type ErrorLinesMap = { [k: string]: number[] }; | |
function rewriteToTS(files: string[]) { | |
files.map((f) => { | |
fs.renameSync(f, f.replace(/\.js$/, ".ts")); | |
console.log("rename", f, f.replace(/\.js$/, ".ts")); | |
}); | |
} | |
function compile( | |
tsFileNames: string[], | |
options: ts.CompilerOptions | |
): ErrorLinesMap { | |
const program = ts.createProgram(tsFileNames, options); | |
// skip write file. only diagnostics | |
const emitResult = program.emit(undefined, () => {}); | |
const allDiagnostics = ts | |
.getPreEmitDiagnostics(program) | |
.concat(emitResult.diagnostics); | |
const errorLines: ErrorLinesMap = {}; | |
allDiagnostics.forEach((diagnostic) => { | |
if (diagnostic.file) { | |
let { line } = diagnostic.file.getLineAndCharacterOfPosition( | |
diagnostic.start! | |
); | |
let last = errorLines[diagnostic.file.fileName]; | |
console.log(last); | |
if (last == null) { | |
last = errorLines[diagnostic.file.fileName] = []; | |
} | |
errorLines[diagnostic.file.fileName] = uniq([...last, line]); | |
} | |
}); | |
return errorLines; | |
} | |
function rewriteFiles(errorLines: ErrorLinesMap) { | |
Object.entries(errorLines).map(([file, errors]) => { | |
const content = fs.readFileSync(file, "utf-8"); | |
const lines = content.split("\n"); | |
const newLines = lines.map((line, idx) => { | |
const m = line.match(/^\s*/); | |
let head = ""; | |
if (m) { | |
head = m[0]; | |
} | |
if (errors.includes(idx)) { | |
return `${head}// @ts-expect-error\n${line}`; | |
} else { | |
return line; | |
} | |
}); | |
const out = newLines.join("\n"); | |
fs.writeFileSync(file, out); | |
console.log("rename", file, file.replace(/\.js$/, ".ts")); | |
}); | |
} | |
const configPath = path.join(process.cwd(), "tsconfig.json"); | |
const tsconfig = ts.parseConfigFileTextToJson( | |
configPath, | |
fs.readFileSync(configPath, "utf-8") | |
); | |
const files = process.argv.slice(2); | |
rewriteToTS(files); | |
const tsFiles = files.map((f) => f.replace(".js", ".ts")); | |
const errors = compile(tsFiles, tsconfig.config!); | |
rewriteFiles(errors); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment