Skip to content

Commit eaa4daa

Browse files
author
Yui T
committed
Return exit code
1 parent 80bab05 commit eaa4daa

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

src/compiler/tsc.ts

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -208,32 +208,32 @@ module ts {
208208
// setting up localization, report them and quit.
209209
if (commandLine.errors.length > 0) {
210210
reportDiagnostics(commandLine.errors);
211-
return sys.exit(1);
211+
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
212212
}
213213

214214
if (commandLine.options.version) {
215215
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Version_0, version));
216-
return sys.exit(0);
216+
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
217217
}
218218

219219
if (commandLine.options.help || commandLine.filenames.length === 0) {
220220
printVersion();
221221
printHelp();
222-
return sys.exit(0);
222+
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
223223
}
224224

225225
var defaultCompilerHost = createCompilerHost(commandLine.options);
226226

227227
if (commandLine.options.watch) {
228228
if (!sys.watchFile) {
229229
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"));
230-
return sys.exit(1);
230+
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
231231
}
232232

233233
watchProgram(commandLine, defaultCompilerHost);
234234
}
235235
else {
236-
var result = compile(commandLine, defaultCompilerHost).errors.length > 0 ? 1 : 0;
236+
var result = compile(commandLine, defaultCompilerHost).exitStatus
237237
return sys.exit(result);
238238
}
239239
}
@@ -328,23 +328,28 @@ module ts {
328328

329329
function compile(commandLine: ParsedCommandLine, compilerHost: CompilerHost) {
330330
var parseStart = new Date().getTime();
331-
var program = createProgram(commandLine.filenames, commandLine.options, compilerHost);
331+
var compilerOptions = commandLine.options;
332+
var program = createProgram(commandLine.filenames, compilerOptions, compilerHost);
332333

333334
var bindStart = new Date().getTime();
334-
var errors = program.getDiagnostics();
335-
if (errors.length) {
335+
var syntacticErrors = program.getDiagnostics();
336+
var emitErrors: Diagnostic[];
337+
var semanticErrors: Diagnostic[];
338+
var errors: Diagnostic[];
339+
340+
if (syntacticErrors.length) {
336341
var checkStart = bindStart;
337342
var emitStart = bindStart;
338343
var reportStart = bindStart;
339344
}
340345
else {
341346
var checker = program.getTypeChecker(/*fullTypeCheckMode*/ true);
342347
var checkStart = new Date().getTime();
343-
var semanticErrors = checker.getDiagnostics();
348+
semanticErrors = checker.getDiagnostics();
344349
var emitStart = new Date().getTime();
345-
var emitErrors = checker.emitFiles().errors;
350+
emitErrors = checker.emitFiles().errors;
346351
var reportStart = new Date().getTime();
347-
errors = concatenate(semanticErrors, emitErrors);
352+
errors = concatenate(syntacticErrors, concatenate(semanticErrors, emitErrors));
348353
}
349354

350355
reportDiagnostics(errors);
@@ -366,7 +371,21 @@ module ts {
366371
reportTimeStatistic("Total time", reportStart - parseStart);
367372
}
368373

369-
return { program: program, errors: errors };
374+
// Check types of diagnostics and return associated exit code
375+
if (syntacticErrors.length > 0) {
376+
return { program: program, exitStatus: EmitReturnStatus.AllOutputGenerationSkipped };
377+
} else if (semanticErrors.length > 0 && !compilerOptions.declaration) {
378+
// No '-d' is specified; javascript file is generated with semantic errors
379+
return { program: program, exitStatus: EmitReturnStatus.JSGeneratedWithSemanticErrors };
380+
} else if (semanticErrors.length > 0 && compilerOptions.declaration) {
381+
// '-d' is specified; javascript file will be emitted with semantic errors but declaration file will be skipped
382+
return { program: program, exitStatus: EmitReturnStatus.DeclarationGenerationSkipped };
383+
} else if (emitErrors.length > 0 && compilerOptions.declaration) {
384+
return { program: program, exitStatus: EmitReturnStatus.EmitErrorsEncountered };
385+
} else {
386+
// There is no error message
387+
return { program: program, exitStatus: EmitReturnStatus.Succeeded };
388+
}
370389

371390
}
372391

src/compiler/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,8 @@ module ts {
606606
AllOutputGenerationSkipped = 1, // No .js generated because of syntax errors, or compiler options errors, nothing generated
607607
JSGeneratedWithSemanticErrors = 2, // .js and .map generated with semantic errors
608608
DeclarationGenerationSkipped = 3, // .d.ts generation skipped because of semantic errors or declaration emitter specific errors; Output .js with semantic errors
609-
EmitErrorsEncountered = 4 // Emitter errors occurred during emitting process
609+
EmitErrorsEncountered = 4, // Emitter errors occurred during emitting process
610+
CompilerOptionsErrors = 5, // Errors occurred in parsing compiler options
610611
}
611612

612613
export interface EmitResult {

0 commit comments

Comments
 (0)