From 35655209cbdbff3e95cbe17a9a717ebd425e305b Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Mon, 18 Mar 2019 22:18:36 +0100 Subject: [PATCH 1/7] emit only affected files --- src/Compiler.ts | 15 ++++++++++++++- src/LuaTranspiler.ts | 10 +++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/Compiler.ts b/src/Compiler.ts index 6b382d200..6378eee70 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -43,10 +43,23 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions): host = ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram); } + let emitLuaLib = true; host.afterProgramCreate = program => { const transpiler = new LuaTranspiler(program.getProgram()); - const status = transpiler.emitFilesAndReportErrors(); + const status = transpiler.reportErrors(); + + while (true) { + const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile(); + if (!currentFile) { break; } + transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); + } + + if (emitLuaLib) { + transpiler.emitLuaLibIfRequired(); + emitLuaLib = false; + } + const errorDiagnostic: ts.Diagnostic = { category: undefined, code: 6194, diff --git a/src/LuaTranspiler.ts b/src/LuaTranspiler.ts index a4a4416a2..fd9d9061d 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)); @@ -75,13 +75,17 @@ export class LuaTranspiler { }); // Copy lualib to target dir + this.emitLuaLibIfRequired(); + + return 0; + } + + public emitLuaLibIfRequired(): void{ if (this.options.luaLibImport === LuaLibImportKind.Require || this.options.luaLibImport === LuaLibImportKind.Always ) { this.emitLuaLib(); } - - return 0; } public emitSourceFile(sourceFile: ts.SourceFile): void { From 7e53123498b48b615ae9cbec9ef392881092a530 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Mon, 18 Mar 2019 22:36:54 +0100 Subject: [PATCH 2/7] track transpiler errors --- src/Compiler.ts | 4 ++-- src/LuaTranspiler.ts | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/Compiler.ts b/src/Compiler.ts index 6378eee70..4e1a5fa60 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -47,12 +47,12 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions): host.afterProgramCreate = program => { const transpiler = new LuaTranspiler(program.getProgram()); - const status = transpiler.reportErrors(); + let status = transpiler.reportErrors(); while (true) { const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile(); if (!currentFile) { break; } - transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); + status = status || transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); } if (emitLuaLib) { diff --git a/src/LuaTranspiler.ts b/src/LuaTranspiler.ts index fd9d9061d..dbdcfca51 100644 --- a/src/LuaTranspiler.ts +++ b/src/LuaTranspiler.ts @@ -65,19 +65,19 @@ export class LuaTranspiler { } public emitFilesAndReportErrors(): number { - const error = this.reportErrors(); + let error = this.reportErrors(); if (error > 0) { return error; } this.program.getSourceFiles().forEach(sourceFile => { - this.emitSourceFile(sourceFile); + error = error || this.emitSourceFile(sourceFile); }); // Copy lualib to target dir this.emitLuaLibIfRequired(); - return 0; + return error; } public emitLuaLibIfRequired(): void{ @@ -88,7 +88,7 @@ export class LuaTranspiler { } } - public emitSourceFile(sourceFile: ts.SourceFile): void { + public emitSourceFile(sourceFile: ts.SourceFile): number { if (!sourceFile.isDeclarationFile) { try { const rootDir = this.options.rootDir; @@ -116,6 +116,7 @@ export class LuaTranspiler { // Write output ts.sys.writeFile(outPath, lua); + return 1; } catch (exception) { /* istanbul ignore else: Testing else part would require to add a bug/exception to our code */ if (exception.node) { @@ -123,6 +124,7 @@ 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; } From e4381976419095cafd81fe1d2815328ea5a36e75 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Mon, 18 Mar 2019 22:38:05 +0100 Subject: [PATCH 3/7] return 0 for success --- src/LuaTranspiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuaTranspiler.ts b/src/LuaTranspiler.ts index dbdcfca51..aa16d94f8 100644 --- a/src/LuaTranspiler.ts +++ b/src/LuaTranspiler.ts @@ -116,7 +116,7 @@ export class LuaTranspiler { // Write output ts.sys.writeFile(outPath, lua); - return 1; + return 0; } catch (exception) { /* istanbul ignore else: Testing else part would require to add a bug/exception to our code */ if (exception.node) { From 0f2485805a05e54046e73e34357936a06e401261 Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Mon, 18 Mar 2019 22:40:41 +0100 Subject: [PATCH 4/7] return 0 for declarations --- src/LuaTranspiler.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuaTranspiler.ts b/src/LuaTranspiler.ts index aa16d94f8..2206db8b3 100644 --- a/src/LuaTranspiler.ts +++ b/src/LuaTranspiler.ts @@ -116,7 +116,6 @@ export class LuaTranspiler { // Write output ts.sys.writeFile(outPath, lua); - return 0; } catch (exception) { /* istanbul ignore else: Testing else part would require to add a bug/exception to our code */ if (exception.node) { @@ -130,6 +129,7 @@ export class LuaTranspiler { } } } + return 0; } public transpileSourceFile(sourceFile: ts.SourceFile): string { From 18e69b7580cf80c1caeb9629a14670a484c12f9c Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Tue, 19 Mar 2019 09:59:07 +0100 Subject: [PATCH 5/7] do a full recompile after transpiler error --- src/Compiler.ts | 23 +++++++++++++---------- src/LuaTranspiler.ts | 8 ++------ 2 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/Compiler.ts b/src/Compiler.ts index 4e1a5fa60..5200a6feb 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -43,21 +43,24 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions): host = ts.createWatchCompilerHost(fileNames, options, ts.sys, ts.createSemanticDiagnosticsBuilderProgram); } - let emitLuaLib = true; + let fullRecompile = true; host.afterProgramCreate = program => { const transpiler = new LuaTranspiler(program.getProgram()); let status = transpiler.reportErrors(); - while (true) { - const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile(); - if (!currentFile) { break; } - status = status || transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); - } - - if (emitLuaLib) { - transpiler.emitLuaLibIfRequired(); - emitLuaLib = false; + if (status === 0) { + if (fullRecompile) { + status = transpiler.emitFilesAndReportErrors(); + } else { + while (true) { + const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile(); + if (!currentFile) { break; } + status = status || transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); + } + } + // do a full recompile after transpiler error. + fullRecompile = status !== 0; } const errorDiagnostic: ts.Diagnostic = { diff --git a/src/LuaTranspiler.ts b/src/LuaTranspiler.ts index 2206db8b3..727fc75c2 100644 --- a/src/LuaTranspiler.ts +++ b/src/LuaTranspiler.ts @@ -75,17 +75,13 @@ export class LuaTranspiler { }); // Copy lualib to target dir - this.emitLuaLibIfRequired(); - - return error; - } - - public emitLuaLibIfRequired(): void{ if (this.options.luaLibImport === LuaLibImportKind.Require || this.options.luaLibImport === LuaLibImportKind.Always ) { this.emitLuaLib(); } + + return error; } public emitSourceFile(sourceFile: ts.SourceFile): number { From 0aefd1d6383819c82815008d736bcc911e46095a Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Tue, 19 Mar 2019 12:59:05 +0100 Subject: [PATCH 6/7] forgot about lazy evaluation --- src/Compiler.ts | 3 ++- src/LuaTranspiler.ts | 12 +++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/Compiler.ts b/src/Compiler.ts index 5200a6feb..f8b7c0807 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -56,7 +56,8 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions): while (true) { const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile(); if (!currentFile) { break; } - status = status || transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); + const fileStatus = transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); + status |= fileStatus; } } // do a full recompile after transpiler error. diff --git a/src/LuaTranspiler.ts b/src/LuaTranspiler.ts index 727fc75c2..dc96e9801 100644 --- a/src/LuaTranspiler.ts +++ b/src/LuaTranspiler.ts @@ -65,13 +65,15 @@ export class LuaTranspiler { } public emitFilesAndReportErrors(): number { - let error = this.reportErrors(); - if (error > 0) { - return error; + let status = this.reportErrors(); + + if (status > 0) { + return status; } this.program.getSourceFiles().forEach(sourceFile => { - error = error || this.emitSourceFile(sourceFile); + const sourceStatus = this.emitSourceFile(sourceFile); + status |= sourceStatus; }); // Copy lualib to target dir @@ -81,7 +83,7 @@ export class LuaTranspiler { this.emitLuaLib(); } - return error; + return status; } public emitSourceFile(sourceFile: ts.SourceFile): number { From 7051b14868e9c14680876a9df74379c42d1dfb3e Mon Sep 17 00:00:00 2001 From: Lars Melchior Date: Thu, 21 Mar 2019 21:16:24 +0100 Subject: [PATCH 7/7] test if affected file is a SourceFile --- src/Compiler.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/Compiler.ts b/src/Compiler.ts index f8b7c0807..0659ea527 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -46,7 +46,6 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions): let fullRecompile = true; host.afterProgramCreate = program => { const transpiler = new LuaTranspiler(program.getProgram()); - let status = transpiler.reportErrors(); if (status === 0) { @@ -56,8 +55,16 @@ export function watchWithOptions(fileNames: string[], options: CompilerOptions): while (true) { const currentFile = program.getSemanticDiagnosticsOfNextAffectedFile(); if (!currentFile) { break; } - const fileStatus = transpiler.emitSourceFile(currentFile.affected as ts.SourceFile); - status |= fileStatus; + + 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.