diff --git a/src/Compiler.ts b/src/Compiler.ts index 6b382d200..0659ea527 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -43,10 +43,34 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions): host = ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram); } + let fullRecompile = true; host.afterProgramCreate = program => { const transpiler = new LuaTranspiler(program.getProgram()); + let status = transpiler.reportErrors(); + + if (status === 0) { + if (fullRecompile) { + status = transpiler.emitFilesAndReportErrors(); + } else { + while (true) { + const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile(); + if (!currentFile) { break; } + + if ("fileName" in currentFile.affected) { // test if currentFile.affected is `ts.SourceFile` + const fileStatus = transpiler.emitSourceFile(currentFile.affected); + status |= fileStatus; + } else { + for (const sourceFile of currentFile.affected.getSourceFiles()) { + const fileStatus = transpiler.emitSourceFile(sourceFile); + status |= fileStatus; + } + } + } + } + // do a full recompile after transpiler error. + fullRecompile = status !== 0; + } - const status = transpiler.emitFilesAndReportErrors(); const errorDiagnostic: ts.Diagnostic = { category: undefined, code: 6194, diff --git a/src/LuaTranspiler.ts b/src/LuaTranspiler.ts index a4a4416a2..dc96e9801 100644 --- a/src/LuaTranspiler.ts +++ b/src/LuaTranspiler.ts @@ -38,7 +38,7 @@ export class LuaTranspiler { return options; } - private reportErrors(): number { + public reportErrors(): number { // Get all diagnostics, ignore unsupported extension const diagnostics = ts.getPreEmitDiagnostics(this.program).filter(diag => diag.code !== 6054); diagnostics.forEach(diag => this.reportDiagnostic(diag)); @@ -65,13 +65,15 @@ export class LuaTranspiler { } public emitFilesAndReportErrors(): number { - const error = this.reportErrors(); - if (error > 0) { - return error; + let status = this.reportErrors(); + + if (status > 0) { + return status; } this.program.getSourceFiles().forEach(sourceFile => { - this.emitSourceFile(sourceFile); + const sourceStatus = this.emitSourceFile(sourceFile); + status |= sourceStatus; }); // Copy lualib to target dir @@ -81,10 +83,10 @@ export class LuaTranspiler { this.emitLuaLib(); } - return 0; + return status; } - public emitSourceFile(sourceFile: ts.SourceFile): void { + public emitSourceFile(sourceFile: ts.SourceFile): number { if (!sourceFile.isDeclarationFile) { try { const rootDir = this.options.rootDir; @@ -119,11 +121,13 @@ export class LuaTranspiler { // Graciously handle transpilation errors console.error("Encountered error parsing file: " + exception.message); console.error(`${sourceFile.fileName} (${1 + pos.line},${pos.character})\n${exception.stack}`); + return 1; } else { throw exception; } } } + return 0; } public transpileSourceFile(sourceFile: ts.SourceFile): string {