Skip to content

Commit 66a363f

Browse files
Simplify the API for emitting and reporting exit statuses to callers.
1 parent e7f6693 commit 66a363f

42 files changed

Lines changed: 199 additions & 260 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/compiler/emitter.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1508,7 +1508,8 @@ module ts {
15081508

15091509
// @internal
15101510
// targetSourceFile is when users only want one file in entire project to be emitted. This is used in compilerOnSave feature
1511-
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile): EmitResult {
1511+
export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFile: SourceFile): EmitResult
1512+
{
15121513
var compilerOptions = host.getCompilerOptions();
15131514
var languageVersion = compilerOptions.target || ScriptTarget.ES3;
15141515
var sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap ? [] : undefined;
@@ -1541,19 +1542,8 @@ module ts {
15411542
// Sort and make the unique list of diagnostics
15421543
diagnostics = sortAndDeduplicateDiagnostics(diagnostics);
15431544

1544-
// Update returnCode if there is any EmitterError
1545-
var hasEmitterError = forEach(diagnostics, diagnostic => diagnostic.category === DiagnosticCategory.Error);
1546-
1547-
// Check and update returnCode for syntactic and semantic
1548-
var emitResultStatus: EmitReturnStatus;
1549-
if (hasEmitterError) {
1550-
emitResultStatus = EmitReturnStatus.EmitErrorsEncountered;
1551-
} else {
1552-
emitResultStatus = EmitReturnStatus.Succeeded;
1553-
}
1554-
15551545
return {
1556-
emitResultStatus,
1546+
emitSkipped: false,
15571547
diagnostics,
15581548
sourceMaps: sourceMapDataList
15591549
};

src/compiler/program.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,20 +174,20 @@ module ts {
174174
// If the noEmitOnError flag is set, then check if we have any errors so far. If so,
175175
// immediately bail out.
176176
if (options.noEmitOnError && getPreEmitDiagnostics(this).length > 0) {
177-
return { diagnostics: [], sourceMaps: undefined, emitResultStatus: EmitReturnStatus.DiagnosticsPresent_AllOutputsSkipped };
177+
return { diagnostics: [], sourceMaps: undefined, emitSkipped: true };
178178
}
179179

180180
var start = new Date().getTime();
181181

182-
var result = emitFiles(
182+
var emitResult = emitFiles(
183183
getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile),
184184
getEmitHost(writeFileCallback),
185185
sourceFile);
186186

187187
emitTime += new Date().getTime() - start;
188-
return result;
188+
return emitResult;
189189
}
190-
190+
191191
function getSourceFile(fileName: string) {
192192
fileName = host.getCanonicalFileName(fileName);
193193
return hasProperty(filesByName, fileName) ? filesByName[fileName] : undefined;

src/compiler/tsc.ts

Lines changed: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ module ts {
165165
if (commandLine.options.locale) {
166166
if (!isJSONSupported()) {
167167
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--locale"));
168-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
168+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
169169
}
170170
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
171171
}
@@ -174,29 +174,29 @@ module ts {
174174
// setting up localization, report them and quit.
175175
if (commandLine.errors.length > 0) {
176176
reportDiagnostics(commandLine.errors);
177-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
177+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
178178
}
179179

180180
if (commandLine.options.version) {
181181
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Version_0, version));
182-
return sys.exit(EmitReturnStatus.Succeeded);
182+
return sys.exit(ExitStatus.Success);
183183
}
184184

185185
if (commandLine.options.help) {
186186
printVersion();
187187
printHelp();
188-
return sys.exit(EmitReturnStatus.Succeeded);
188+
return sys.exit(ExitStatus.Success);
189189
}
190190

191191
if (commandLine.options.project) {
192192
if (!isJSONSupported()) {
193193
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--project"));
194-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
194+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
195195
}
196196
configFileName = normalizePath(combinePaths(commandLine.options.project, "tsconfig.json"));
197197
if (commandLine.fileNames.length !== 0) {
198198
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Option_project_cannot_be_mixed_with_source_files_on_a_command_line));
199-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
199+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
200200
}
201201
}
202202
else if (commandLine.fileNames.length === 0 && isJSONSupported()) {
@@ -206,13 +206,13 @@ module ts {
206206
if (commandLine.fileNames.length === 0 && !configFileName) {
207207
printVersion();
208208
printHelp();
209-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
209+
return sys.exit(ExitStatus.Success);
210210
}
211211

212212
if (commandLine.options.watch) {
213213
if (!sys.watchFile) {
214214
reportDiagnostic(createCompilerDiagnostic(Diagnostics.The_current_host_does_not_support_the_0_option, "--watch"));
215-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
215+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
216216
}
217217
if (configFileName) {
218218
configFileWatcher = sys.watchFile(configFileName, configFileChanged);
@@ -229,12 +229,12 @@ module ts {
229229
var configObject = readConfigFile(configFileName);
230230
if (!configObject) {
231231
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Unable_to_open_file_0, configFileName));
232-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
232+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
233233
}
234234
var configParseResult = parseConfigFile(configObject, getDirectoryPath(configFileName));
235235
if (configParseResult.errors.length > 0) {
236236
reportDiagnostics(configParseResult.errors);
237-
return sys.exit(EmitReturnStatus.CompilerOptionsErrors);
237+
return sys.exit(ExitStatus.DiagnosticsPresent_OutputsSkipped);
238238
}
239239
rootFileNames = configParseResult.fileNames;
240240
compilerOptions = extend(commandLine.options, configParseResult.options);
@@ -362,33 +362,46 @@ module ts {
362362

363363
return { program, exitStatus };
364364

365-
function compileProgram(): EmitReturnStatus {
365+
function compileProgram(): ExitStatus {
366366
// First get any syntactic errors.
367-
var errors = program.getSyntacticDiagnostics();
368-
reportDiagnostics(errors);
367+
var diagnostics = program.getSyntacticDiagnostics();
368+
reportDiagnostics(diagnostics);
369369

370370
// If we didn't have any syntactic errors, then also try getting the global and
371371
// semantic errors.
372-
if (errors.length === 0) {
373-
var errors = program.getGlobalDiagnostics();
374-
reportDiagnostics(errors);
372+
if (diagnostics.length === 0) {
373+
var diagnostics = program.getGlobalDiagnostics();
374+
reportDiagnostics(diagnostics);
375375

376-
if (errors.length === 0) {
377-
var errors = program.getSemanticDiagnostics();
378-
reportDiagnostics(errors);
376+
if (diagnostics.length === 0) {
377+
var diagnostics = program.getSemanticDiagnostics();
378+
reportDiagnostics(diagnostics);
379379
}
380380
}
381381

382382
// If the user doesn't want us to emit, then we're done at this point.
383383
if (compilerOptions.noEmit) {
384-
return EmitReturnStatus.Succeeded;
384+
return diagnostics.length
385+
? ExitStatus.DiagnosticsPresent_OutputsSkipped
386+
: ExitStatus.Success;
385387
}
386388

387389
// Otherwise, emit and report any errors we ran into.
388390
var emitOutput = program.emit();
389391
reportDiagnostics(emitOutput.diagnostics);
390392

391-
return emitOutput.emitResultStatus;
393+
// If the emitter didn't emit anything, then pass that value along.
394+
if (emitOutput.emitSkipped) {
395+
return ExitStatus.DiagnosticsPresent_OutputsSkipped;
396+
}
397+
398+
// The emitter emitted something, inform the caller if that happened in the presence
399+
// of diagnostics or not.
400+
if (diagnostics.length > 0 || emitOutput.diagnostics.length > 0) {
401+
ExitStatus.DiagnosticsPresent_OutputsGenerated;
402+
}
403+
404+
return ExitStatus.Success;
392405
}
393406
}
394407

src/compiler/types.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -981,31 +981,21 @@ module ts {
981981
}
982982

983983
// Return code used by getEmitOutput function to indicate status of the function
984-
export enum EmitReturnStatus {
985-
// All outputs generated if requested (.js, .map, .d.ts), no errors reported
986-
Succeeded = 0,
984+
export enum ExitStatus {
985+
// Compiler ran successfully. Either this was a simple do-nothing compilation (for example,
986+
// when -version or -help was provided, or this was a normal compilation, no diagnostics
987+
// were produced, and all outputs were generated successfully.
988+
Success = 0,
987989

988-
// No .js, .map or d.ts generated because of diagnostics and the presence of the
989-
// -noEmitOnError optoin.
990-
DiagnosticsPresent_AllOutputsSkipped = 1,
990+
// Diagnostics were produced and because of them no code was generated.
991+
DiagnosticsPresent_OutputsSkipped = 1,
991992

992-
// .js and .map generated. However, diagnostics were generated as well.
993-
// No .d.ts was requested or generated.
994-
DiagnosticsPresent_JavaScriptGenerated = 2,
995-
996-
// .js, .map generated. .d.ts was requested but was not generated due to the
997-
// presence of diagnostics.
998-
DiagnosticsPresent_JavaScriptGenerated_DeclarationNotGenerated = 3,
999-
1000-
// Emitter errors occurred during emitting process.
1001-
EmitErrorsEncountered = 4,
1002-
1003-
// Errors occurred in parsing compiler options, nothing generated
1004-
CompilerOptionsErrors = 5,
993+
// Diagnostics were produced and outputs were generated in spite of them.
994+
DiagnosticsPresent_OutputsGenerated = 2,
1005995
}
1006996

1007997
export interface EmitResult {
1008-
emitResultStatus: EmitReturnStatus;
998+
emitSkipped: boolean;
1009999
diagnostics: Diagnostic[];
10101000
sourceMaps: SourceMapData[]; // Array of sourceMapData if compiler emitted sourcemaps
10111001
}

src/harness/fourslash.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1140,11 +1140,10 @@ module FourSlash {
11401140
// Loop through all the emittedFiles and emit them one by one
11411141
emitFiles.forEach(emitFile => {
11421142
var emitOutput = this.languageService.getEmitOutput(emitFile.fileName);
1143-
var emitOutputStatus = emitOutput.emitOutputStatus;
11441143
// Print emitOutputStatus in readable format
1145-
resultString += "EmitOutputStatus : " + ts.EmitReturnStatus[emitOutputStatus] + ts.sys.newLine;
1144+
resultString += "EmitSkipped: " + emitOutput.emitSkipped + ts.sys.newLine;
11461145

1147-
if (emitOutputStatus !== ts.EmitReturnStatus.Succeeded) {
1146+
if (emitOutput.emitSkipped) {
11481147
resultString += "Diagnostics:" + ts.sys.newLine;
11491148
var diagnostics = ts.getPreEmitDiagnostics(this.languageService.getProgram());
11501149
for (var i = 0, n = diagnostics.length; i < n; i++) {

src/services/services.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,7 +1122,7 @@ module ts {
11221122

11231123
export interface EmitOutput {
11241124
outputFiles: OutputFile[];
1125-
emitOutputStatus: EmitReturnStatus;
1125+
emitSkipped: boolean;
11261126
}
11271127

11281128
export const enum OutputFileType {
@@ -4664,7 +4664,7 @@ module ts {
46644664

46654665
return {
46664666
outputFiles,
4667-
emitOutputStatus: emitOutput.emitResultStatus
4667+
emitSkipped: emitOutput.emitSkipped
46684668
};
46694669
}
46704670

tests/baselines/reference/APISample_compile.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void
2525
console.log(`${diagnostic.file.fileName} (${lineChar.line},${lineChar.character}): ${ts.flattenDiagnosticMessageText(diagnostic.messageText, os.EOL)}`);
2626
});
2727

28-
console.log(`Process exiting with code '${emitResult.emitResultStatus}'.`);
29-
process.exit(emitResult.emitResultStatus);
28+
var exitCode = emitResult.emitSkipped ? 1 : 0;
29+
console.log(`Process exiting with code '${exitCode}'.`);
30+
process.exit(exitCode);
3031
}
3132

3233
compile(process.argv.slice(2), {
@@ -777,16 +778,13 @@ declare module "typescript" {
777778
sourceMapMappings: string;
778779
sourceMapDecodedMappings: SourceMapSpan[];
779780
}
780-
enum EmitReturnStatus {
781-
Succeeded = 0,
782-
DiagnosticsPresent_AllOutputsSkipped = 1,
783-
DiagnosticsPresent_JavaScriptGenerated = 2,
784-
DiagnosticsPresent_JavaScriptGenerated_DeclarationNotGenerated = 3,
785-
EmitErrorsEncountered = 4,
786-
CompilerOptionsErrors = 5,
781+
enum ExitStatus {
782+
Success = 0,
783+
DiagnosticsPresent_OutputsSkipped = 1,
784+
DiagnosticsPresent_OutputsGenerated = 2,
787785
}
788786
interface EmitResult {
789-
emitResultStatus: EmitReturnStatus;
787+
emitSkipped: boolean;
790788
diagnostics: Diagnostic[];
791789
sourceMaps: SourceMapData[];
792790
}
@@ -1723,7 +1721,7 @@ declare module "typescript" {
17231721
}
17241722
interface EmitOutput {
17251723
outputFiles: OutputFile[];
1726-
emitOutputStatus: EmitReturnStatus;
1724+
emitSkipped: boolean;
17271725
}
17281726
const enum OutputFileType {
17291727
JavaScript = 0,
@@ -1926,8 +1924,9 @@ function compile(fileNames, options) {
19261924
var lineChar = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start);
19271925
console.log(diagnostic.file.fileName + " (" + lineChar.line + "," + lineChar.character + "): " + ts.flattenDiagnosticMessageText(diagnostic.messageText, os.EOL));
19281926
});
1929-
console.log("Process exiting with code '" + emitResult.emitResultStatus + "'.");
1930-
process.exit(emitResult.emitResultStatus);
1927+
var exitCode = emitResult.emitSkipped ? 1 : 0;
1928+
console.log("Process exiting with code '" + exitCode + "'.");
1929+
process.exit(exitCode);
19311930
}
19321931
exports.compile = compile;
19331932
compile(process.argv.slice(2), {

0 commit comments

Comments
 (0)