Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/Compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 11 additions & 7 deletions src/LuaTranspiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to make these constants or an enum.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, though we should probably discuss error codes and general error handling of tstl in a separate PR.

} else {
throw exception;
}
}
}
return 0;
}

public transpileSourceFile(sourceFile: ts.SourceFile): string {
Expand Down