From eed65a0334337de99da9de824b738a097a57138f Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 16 Feb 2016 22:01:28 -0800 Subject: [PATCH 1/2] Port #7106 to master --- src/compiler/emitter.ts | 2 +- src/compiler/program.ts | 20 ++++++++++++--- src/compiler/tsc.ts | 25 ++++++------------- src/compiler/types.ts | 2 +- src/harness/projectsRunner.ts | 2 +- .../baselines/reference/APISample_compile.js | 4 +-- tests/cases/compiler/APISample_compile.ts | 2 +- 7 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index f08082d72c8..d2c96a11774 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -389,7 +389,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge return { emitSkipped, - diagnostics: emitterDiagnostics.getDiagnostics(), + declarationDiagnostics: emitterDiagnostics.getDiagnostics(), sourceMaps: sourceMapDataList }; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index 9aee98a10ec..f29784ccf3c 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -953,13 +953,27 @@ namespace ts { } function emitWorker(program: Program, sourceFile: SourceFile, writeFileCallback: WriteFileCallback, cancellationToken: CancellationToken): EmitResult { + let declarationDiagnostics: Diagnostic[] = []; + + if (options.noEmit) { + return { declarationDiagnostics, sourceMaps: undefined, emitSkipped: true }; + } + // If the noEmitOnError flag is set, then check if we have any errors so far. If so, // immediately bail out. Note that we pass 'undefined' for 'sourceFile' so that we // get any preEmit diagnostics, not just the ones if (options.noEmitOnError) { - const preEmitDiagnostics = getPreEmitDiagnostics(program, /*sourceFile:*/ undefined, cancellationToken); - if (preEmitDiagnostics.length > 0) { - return { diagnostics: preEmitDiagnostics, sourceMaps: undefined, emitSkipped: true }; + let diagnostics = program.getOptionsDiagnostics(cancellationToken).concat( + program.getSyntacticDiagnostics(sourceFile, cancellationToken), + program.getGlobalDiagnostics(cancellationToken), + program.getSemanticDiagnostics(sourceFile, cancellationToken)); + + if (diagnostics.length === 0 && program.getCompilerOptions().declaration) { + declarationDiagnostics = program.getDeclarationDiagnostics(/*sourceFile*/ undefined, cancellationToken); + } + + if (diagnostics.length > 0 || declarationDiagnostics.length > 0) { + return { declarationDiagnostics, sourceMaps: undefined, emitSkipped: true }; } } diff --git a/src/compiler/tsc.ts b/src/compiler/tsc.ts index c69bfca1877..59212ed849b 100644 --- a/src/compiler/tsc.ts +++ b/src/compiler/tsc.ts @@ -590,30 +590,21 @@ namespace ts { } } - reportDiagnostics(diagnostics, compilerHost); - - // If the user doesn't want us to emit, then we're done at this point. - if (compilerOptions.noEmit) { - return diagnostics.length - ? ExitStatus.DiagnosticsPresent_OutputsSkipped - : ExitStatus.Success; - } - // Otherwise, emit and report any errors we ran into. const emitOutput = program.emit(); - reportDiagnostics(emitOutput.diagnostics, compilerHost); + diagnostics = diagnostics.concat(emitOutput.declarationDiagnostics); - // If the emitter didn't emit anything, then pass that value along. - if (emitOutput.emitSkipped) { + reportDiagnostics(sortAndDeduplicateDiagnostics(diagnostics), compilerHost); + + if (emitOutput.emitSkipped && diagnostics.length > 0) { + // If the emitter didn't emit anything, then pass that value along. return ExitStatus.DiagnosticsPresent_OutputsSkipped; } - - // The emitter emitted something, inform the caller if that happened in the presence - // of diagnostics or not. - if (diagnostics.length > 0 || emitOutput.diagnostics.length > 0) { + else if (diagnostics.length > 0) { + // The emitter emitted something, inform the caller if that happened in the presence + // of diagnostics or not. return ExitStatus.DiagnosticsPresent_OutputsGenerated; } - return ExitStatus.Success; } } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index d7d7518c886..e8fca86c4b2 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1680,7 +1680,7 @@ namespace ts { export interface EmitResult { emitSkipped: boolean; - diagnostics: Diagnostic[]; + /* @internal */ declarationDiagnostics: Diagnostic[]; /* @internal */ sourceMaps: SourceMapData[]; // Array of sourceMapData if compiler emitted sourcemaps } diff --git a/src/harness/projectsRunner.ts b/src/harness/projectsRunner.ts index 96fcfedc6b4..b8280ed24b9 100644 --- a/src/harness/projectsRunner.ts +++ b/src/harness/projectsRunner.ts @@ -127,7 +127,7 @@ class ProjectRunner extends RunnerBase { let errors = ts.getPreEmitDiagnostics(program); const emitResult = program.emit(); - errors = ts.concatenate(errors, emitResult.diagnostics); + errors = ts.concatenate(errors, emitResult.declarationDiagnostics); const sourceMapData = emitResult.sourceMaps; // Clean up source map data that will be used in baselining diff --git a/tests/baselines/reference/APISample_compile.js b/tests/baselines/reference/APISample_compile.js index 3830e53697c..5f55c04c9f9 100644 --- a/tests/baselines/reference/APISample_compile.js +++ b/tests/baselines/reference/APISample_compile.js @@ -16,7 +16,7 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void var program = ts.createProgram(fileNames, options); var emitResult = program.emit(); - var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); + var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(diagnostic => { var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); @@ -45,7 +45,7 @@ var ts = require("typescript"); function compile(fileNames, options) { var program = ts.createProgram(fileNames, options); var emitResult = program.emit(); - var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); + var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(function (diagnostic) { var _a = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start), line = _a.line, character = _a.character; var message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); diff --git a/tests/cases/compiler/APISample_compile.ts b/tests/cases/compiler/APISample_compile.ts index c63009f7d63..9672b9863fa 100644 --- a/tests/cases/compiler/APISample_compile.ts +++ b/tests/cases/compiler/APISample_compile.ts @@ -18,7 +18,7 @@ export function compile(fileNames: string[], options: ts.CompilerOptions): void var program = ts.createProgram(fileNames, options); var emitResult = program.emit(); - var allDiagnostics = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics); + var allDiagnostics = ts.getPreEmitDiagnostics(program); allDiagnostics.forEach(diagnostic => { var { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); From 2b52ae1cb7fd65392f1e5b4d3c41dee7a9180d84 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Tue, 16 Feb 2016 22:57:27 -0800 Subject: [PATCH 2/2] Fix lint errors --- src/compiler/program.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/program.ts b/src/compiler/program.ts index f29784ccf3c..2b1a38d2084 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -963,7 +963,7 @@ namespace ts { // immediately bail out. Note that we pass 'undefined' for 'sourceFile' so that we // get any preEmit diagnostics, not just the ones if (options.noEmitOnError) { - let diagnostics = program.getOptionsDiagnostics(cancellationToken).concat( + const diagnostics = program.getOptionsDiagnostics(cancellationToken).concat( program.getSyntacticDiagnostics(sourceFile, cancellationToken), program.getGlobalDiagnostics(cancellationToken), program.getSemanticDiagnostics(sourceFile, cancellationToken));