2018-04-24 19:48:55 +02:00
|
|
|
/**
|
|
|
|
* Test harness compiler functionality.
|
|
|
|
*/
|
2017-11-10 03:35:24 +01:00
|
|
|
namespace compiler {
|
2017-11-10 01:01:33 +01:00
|
|
|
export interface Project {
|
|
|
|
file: string;
|
|
|
|
config?: ts.ParsedCommandLine;
|
|
|
|
errors?: ts.Diagnostic[];
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:30:03 +02:00
|
|
|
export function readProject(host: fakes.ParseConfigHost, project: string | undefined, existingOptions?: ts.CompilerOptions): Project | undefined {
|
2017-11-10 01:01:33 +01:00
|
|
|
if (project) {
|
2018-05-09 02:07:54 +02:00
|
|
|
project = vpath.isTsConfigFile(project) ? project : vpath.combine(project, "tsconfig.json");
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-01-23 19:27:20 +01:00
|
|
|
[project] = host.vfs.scanSync(".", "ancestors-or-self", {
|
2018-01-23 19:40:56 +01:00
|
|
|
accept: (path, stats) => stats.isFile() && host.vfs.stringComparer(vpath.basename(path), "tsconfig.json") === 0
|
2018-01-23 19:27:20 +01:00
|
|
|
});
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (project) {
|
|
|
|
// TODO(rbuckton): Do we need to resolve this? Resolving breaks projects tests.
|
|
|
|
// project = vpath.resolve(host.vfs.currentDirectory, project);
|
|
|
|
|
|
|
|
// read the config file
|
|
|
|
const readResult = ts.readConfigFile(project, path => host.readFile(path));
|
|
|
|
if (readResult.error) {
|
|
|
|
return { file: project, errors: [readResult.error] };
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the config file
|
|
|
|
const config = ts.parseJsonConfigFileContent(readResult.config, host, vpath.dirname(project), existingOptions);
|
|
|
|
return { file: project, errors: config.errors, config };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 20:20:44 +01:00
|
|
|
/**
|
|
|
|
* Correlates compilation inputs and outputs
|
|
|
|
*/
|
2017-11-10 01:01:33 +01:00
|
|
|
export interface CompilationOutput {
|
2017-12-21 20:20:44 +01:00
|
|
|
readonly inputs: ReadonlyArray<documents.TextDocument>;
|
2017-11-10 03:35:24 +01:00
|
|
|
readonly js: documents.TextDocument | undefined;
|
|
|
|
readonly dts: documents.TextDocument | undefined;
|
|
|
|
readonly map: documents.TextDocument | undefined;
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class CompilationResult {
|
2018-04-19 20:30:03 +02:00
|
|
|
public readonly host: fakes.CompilerHost;
|
2017-11-10 01:01:33 +01:00
|
|
|
public readonly program: ts.Program | undefined;
|
|
|
|
public readonly result: ts.EmitResult | undefined;
|
|
|
|
public readonly options: ts.CompilerOptions;
|
2017-11-10 22:52:12 +01:00
|
|
|
public readonly diagnostics: ReadonlyArray<ts.Diagnostic>;
|
2018-01-30 20:57:47 +01:00
|
|
|
public readonly js: ReadonlyMap<string, documents.TextDocument>;
|
|
|
|
public readonly dts: ReadonlyMap<string, documents.TextDocument>;
|
|
|
|
public readonly maps: ReadonlyMap<string, documents.TextDocument>;
|
2017-11-10 01:01:33 +01:00
|
|
|
|
2017-11-10 03:35:24 +01:00
|
|
|
private _inputs: documents.TextDocument[] = [];
|
2018-04-24 19:48:55 +02:00
|
|
|
private _inputsAndOutputs: collections.SortedMap<string, CompilationOutput>;
|
2017-11-10 01:01:33 +01:00
|
|
|
|
2018-04-19 20:30:03 +02:00
|
|
|
constructor(host: fakes.CompilerHost, options: ts.CompilerOptions, program: ts.Program | undefined, result: ts.EmitResult | undefined, diagnostics: ts.Diagnostic[]) {
|
2017-11-10 01:01:33 +01:00
|
|
|
this.host = host;
|
|
|
|
this.program = program;
|
|
|
|
this.result = result;
|
|
|
|
this.diagnostics = diagnostics;
|
|
|
|
this.options = program ? program.getCompilerOptions() : options;
|
|
|
|
|
|
|
|
// collect outputs
|
2018-04-24 19:48:55 +02:00
|
|
|
const js = this.js = new collections.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
|
|
|
|
const dts = this.dts = new collections.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
|
|
|
|
const maps = this.maps = new collections.SortedMap<string, documents.TextDocument>({ comparer: this.vfs.stringComparer, sort: "insertion" });
|
2017-11-10 01:01:33 +01:00
|
|
|
for (const document of this.host.outputs) {
|
2018-05-04 00:35:54 +02:00
|
|
|
if (vpath.isJavaScript(document.file) || ts.fileExtensionIs(document.file, ts.Extension.Json)) {
|
2017-11-10 22:52:12 +01:00
|
|
|
js.set(document.file, document);
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
2018-04-20 01:47:07 +02:00
|
|
|
else if (vpath.isDeclaration(document.file)) {
|
2017-11-10 22:52:12 +01:00
|
|
|
dts.set(document.file, document);
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
2018-04-20 01:47:07 +02:00
|
|
|
else if (vpath.isSourceMap(document.file)) {
|
2017-11-10 22:52:12 +01:00
|
|
|
maps.set(document.file, document);
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// correlate inputs and outputs
|
2018-04-24 19:48:55 +02:00
|
|
|
this._inputsAndOutputs = new collections.SortedMap<string, CompilationOutput>({ comparer: this.vfs.stringComparer, sort: "insertion" });
|
2017-11-10 01:01:33 +01:00
|
|
|
if (program) {
|
2017-12-21 20:20:44 +01:00
|
|
|
if (this.options.out || this.options.outFile) {
|
2018-01-20 06:58:46 +01:00
|
|
|
const outFile = vpath.resolve(this.vfs.cwd(), this.options.outFile || this.options.out);
|
2017-12-21 20:20:44 +01:00
|
|
|
const inputs: documents.TextDocument[] = [];
|
|
|
|
for (const sourceFile of program.getSourceFiles()) {
|
|
|
|
if (sourceFile) {
|
|
|
|
const input = new documents.TextDocument(sourceFile.fileName, sourceFile.text);
|
|
|
|
this._inputs.push(input);
|
2018-04-20 01:47:07 +02:00
|
|
|
if (!vpath.isDeclaration(sourceFile.fileName)) {
|
2017-12-21 20:20:44 +01:00
|
|
|
inputs.push(input);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const outputs: CompilationOutput = {
|
|
|
|
inputs,
|
|
|
|
js: js.get(outFile),
|
|
|
|
dts: dts.get(vpath.changeExtension(outFile, ".d.ts")),
|
|
|
|
map: maps.get(outFile + ".map")
|
|
|
|
};
|
|
|
|
|
|
|
|
if (outputs.js) this._inputsAndOutputs.set(outputs.js.file, outputs);
|
|
|
|
if (outputs.dts) this._inputsAndOutputs.set(outputs.dts.file, outputs);
|
|
|
|
if (outputs.map) this._inputsAndOutputs.set(outputs.map.file, outputs);
|
|
|
|
|
|
|
|
for (const input of inputs) {
|
|
|
|
this._inputsAndOutputs.set(input.file, outputs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (const sourceFile of program.getSourceFiles()) {
|
|
|
|
if (sourceFile) {
|
|
|
|
const input = new documents.TextDocument(sourceFile.fileName, sourceFile.text);
|
|
|
|
this._inputs.push(input);
|
2018-04-20 01:47:07 +02:00
|
|
|
if (!vpath.isDeclaration(sourceFile.fileName)) {
|
2017-12-21 20:20:44 +01:00
|
|
|
const extname = ts.getOutputExtension(sourceFile, this.options);
|
|
|
|
const outputs: CompilationOutput = {
|
|
|
|
inputs: [input],
|
|
|
|
js: js.get(this.getOutputPath(sourceFile.fileName, extname)),
|
|
|
|
dts: dts.get(this.getOutputPath(sourceFile.fileName, ".d.ts")),
|
|
|
|
map: maps.get(this.getOutputPath(sourceFile.fileName, extname + ".map"))
|
|
|
|
};
|
|
|
|
|
|
|
|
this._inputsAndOutputs.set(sourceFile.fileName, outputs);
|
|
|
|
if (outputs.js) this._inputsAndOutputs.set(outputs.js.file, outputs);
|
|
|
|
if (outputs.dts) this._inputsAndOutputs.set(outputs.dts.file, outputs);
|
|
|
|
if (outputs.map) this._inputsAndOutputs.set(outputs.map.file, outputs);
|
|
|
|
}
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-10 22:52:12 +01:00
|
|
|
|
2017-11-10 23:55:04 +01:00
|
|
|
this.diagnostics = diagnostics;
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
|
2018-01-20 06:58:46 +01:00
|
|
|
public get vfs(): vfs.FileSystem {
|
2017-11-10 01:01:33 +01:00
|
|
|
return this.host.vfs;
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:35:24 +01:00
|
|
|
public get inputs(): ReadonlyArray<documents.TextDocument> {
|
2017-11-10 01:01:33 +01:00
|
|
|
return this._inputs;
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:35:24 +01:00
|
|
|
public get outputs(): ReadonlyArray<documents.TextDocument> {
|
2017-11-10 01:01:33 +01:00
|
|
|
return this.host.outputs;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get traces(): ReadonlyArray<string> {
|
|
|
|
return this.host.traces;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get emitSkipped(): boolean {
|
|
|
|
return this.result && this.result.emitSkipped || false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get singleFile(): boolean {
|
|
|
|
return !!this.options.outFile || !!this.options.out;
|
|
|
|
}
|
|
|
|
|
|
|
|
public get commonSourceDirectory(): string {
|
|
|
|
const common = this.program && this.program.getCommonSourceDirectory() || "";
|
2018-01-20 06:58:46 +01:00
|
|
|
return common && vpath.combine(this.vfs.cwd(), common);
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public getInputsAndOutputs(path: string): CompilationOutput | undefined {
|
2018-01-20 06:58:46 +01:00
|
|
|
return this._inputsAndOutputs.get(vpath.resolve(this.vfs.cwd(), path));
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
|
2017-12-21 20:20:44 +01:00
|
|
|
public getInputs(path: string): ReadonlyArray<documents.TextDocument> | undefined {
|
2017-11-10 01:01:33 +01:00
|
|
|
const outputs = this.getInputsAndOutputs(path);
|
2017-12-21 20:20:44 +01:00
|
|
|
return outputs && outputs.inputs;
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
|
2017-11-10 03:35:24 +01:00
|
|
|
public getOutput(path: string, kind: "js" | "dts" | "map"): documents.TextDocument | undefined {
|
2017-11-10 01:01:33 +01:00
|
|
|
const outputs = this.getInputsAndOutputs(path);
|
|
|
|
return outputs && outputs[kind];
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:52:12 +01:00
|
|
|
public getSourceMapRecord(): string | undefined {
|
|
|
|
if (this.result.sourceMaps && this.result.sourceMaps.length > 0) {
|
2018-04-17 23:39:20 +02:00
|
|
|
return Harness.SourceMapRecorder.getSourceMapRecord(this.result.sourceMaps, this.program, Array.from(this.js.values()), Array.from(this.dts.values()));
|
2017-11-10 22:52:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-10 03:35:24 +01:00
|
|
|
public getSourceMap(path: string): documents.SourceMap | undefined {
|
2018-04-20 01:47:07 +02:00
|
|
|
if (this.options.noEmit || vpath.isDeclaration(path)) return undefined;
|
2017-11-10 01:01:33 +01:00
|
|
|
if (this.options.inlineSourceMap) {
|
|
|
|
const document = this.getOutput(path, "js");
|
2017-11-10 03:35:24 +01:00
|
|
|
return document && documents.SourceMap.fromSource(document.text);
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
if (this.options.sourceMap) {
|
|
|
|
const document = this.getOutput(path, "map");
|
2017-11-10 03:35:24 +01:00
|
|
|
return document && new documents.SourceMap(document.file, document.text);
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-21 20:20:44 +01:00
|
|
|
public getOutputPath(path: string, ext: string): string {
|
|
|
|
if (this.options.outFile || this.options.out) {
|
2018-01-20 06:58:46 +01:00
|
|
|
path = vpath.resolve(this.vfs.cwd(), this.options.outFile || this.options.out);
|
2017-12-21 20:20:44 +01:00
|
|
|
}
|
|
|
|
else {
|
2018-01-20 06:58:46 +01:00
|
|
|
path = vpath.resolve(this.vfs.cwd(), path);
|
2017-12-21 20:20:44 +01:00
|
|
|
const outDir = ext === ".d.ts" ? this.options.declarationDir || this.options.outDir : this.options.outDir;
|
|
|
|
if (outDir) {
|
|
|
|
const common = this.commonSourceDirectory;
|
|
|
|
if (common) {
|
2018-01-20 06:58:46 +01:00
|
|
|
path = vpath.relative(common, path, this.vfs.ignoreCase);
|
|
|
|
path = vpath.combine(vpath.resolve(this.vfs.cwd(), this.options.outDir), path);
|
2017-12-21 20:20:44 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
2017-12-21 20:20:44 +01:00
|
|
|
return vpath.changeExtension(path, ext);
|
2017-11-10 01:01:33 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-19 20:30:03 +02:00
|
|
|
export function compileFiles(host: fakes.CompilerHost, rootFiles: string[] | undefined, compilerOptions: ts.CompilerOptions): CompilationResult {
|
2017-11-10 01:01:33 +01:00
|
|
|
if (compilerOptions.project || !rootFiles || rootFiles.length === 0) {
|
|
|
|
const project = readProject(host.parseConfigHost, compilerOptions.project, compilerOptions);
|
|
|
|
if (project) {
|
|
|
|
if (project.errors && project.errors.length > 0) {
|
|
|
|
return new CompilationResult(host, compilerOptions, /*program*/ undefined, /*result*/ undefined, project.errors);
|
|
|
|
}
|
|
|
|
if (project.config) {
|
|
|
|
rootFiles = project.config.fileNames;
|
|
|
|
compilerOptions = project.config.options;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete compilerOptions.project;
|
|
|
|
}
|
|
|
|
|
2017-11-10 22:52:12 +01:00
|
|
|
// establish defaults (aligns with old harness)
|
2017-11-10 01:01:33 +01:00
|
|
|
if (compilerOptions.target === undefined) compilerOptions.target = ts.ScriptTarget.ES3;
|
|
|
|
if (compilerOptions.newLine === undefined) compilerOptions.newLine = ts.NewLineKind.CarriageReturnLineFeed;
|
|
|
|
if (compilerOptions.skipDefaultLibCheck === undefined) compilerOptions.skipDefaultLibCheck = true;
|
|
|
|
if (compilerOptions.noErrorTruncation === undefined) compilerOptions.noErrorTruncation = true;
|
|
|
|
|
|
|
|
const program = ts.createProgram(rootFiles || [], compilerOptions, host);
|
|
|
|
const emitResult = program.emit();
|
|
|
|
const errors = ts.getPreEmitDiagnostics(program);
|
|
|
|
return new CompilationResult(host, compilerOptions, program, emitResult, errors);
|
|
|
|
}
|
2018-05-04 00:35:54 +02:00
|
|
|
}
|