swc/crates/swc_ecma_parser/scripts/tsc/copy-test.ts

68 lines
1.8 KiB
TypeScript
Raw Normal View History

#!/usr/bin/env npx ts-node
// Usage: ./scripts/copy-ts-tests.ts ~/projects/TypeScript
2020-06-02 13:22:41 +03:00
import * as ts from "typescript";
import * as fs from "fs";
import * as path from "path";
import { promisify } from "util";
2020-06-02 13:22:41 +03:00
const targetDir = path.resolve(__dirname, "..", "..", "tests", "tsc");
2020-06-02 13:22:41 +03:00
const fileToCheck = process.argv[2];
2020-06-02 13:22:41 +03:00
async function compile(
fileNames: string[],
options: ts.CompilerOptions
): Promise<boolean> {
2020-06-02 13:22:41 +03:00
options.noEmit = true;
options.jsx = ts.JsxEmit.Preserve;
let program = ts.createProgram(fileNames, options);
let emitResult = program.emit();
let allDiagnostics = ts
.getPreEmitDiagnostics(program)
.concat(emitResult.diagnostics);
for (const d of allDiagnostics) {
if (!d.file) continue;
// Parse failure
if (1000 <= d.code && d.code < 2000) return false;
if (10000 <= d.code) return false;
if (2000 <= d.code && d.code < 3000) continue;
let { line, character } = d.file.getLineAndCharacterOfPosition(
d.start!
);
2020-06-02 13:22:41 +03:00
let message = ts.flattenDiagnosticMessageText(d.messageText, "\n");
console.log(
`${d.code} ${d.file.fileName} (${line + 1},${
character + 1
}): ${message}`
);
2020-06-02 13:22:41 +03:00
}
return true;
}
async function check(f: string) {
const ok = await compile([f], {
noEmitOnError: true,
target: ts.ScriptTarget.Latest,
module: ts.ModuleKind.None,
2020-06-02 13:22:41 +03:00
});
if (ok) {
const filename = path.basename(f);
const target = path.join(targetDir, filename);
2020-06-02 13:22:41 +03:00
console.log("Creating", f, "->", target);
2020-06-02 13:22:41 +03:00
// We use rename as resumable copy
fs.renameSync(f, target);
2020-06-02 13:22:41 +03:00
// console.log('Created', target)
}
}
check(fileToCheck);