Skip to content

Commit 89a313e

Browse files
committed
Populate if emit was skipped correctly as part of emit result
1 parent 34706df commit 89a313e

10 files changed

+50
-15
lines changed

src/compiler/declarationEmitter.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -1608,12 +1608,14 @@ namespace ts {
16081608
/* @internal */
16091609
export function writeDeclarationFile(declarationFilePath: string, sourceFile: SourceFile, host: EmitHost, resolver: EmitResolver, diagnostics: Diagnostic[]) {
16101610
let emitDeclarationResult = emitDeclarations(host, resolver, diagnostics, declarationFilePath, sourceFile);
1611-
if (!emitDeclarationResult.reportedDeclarationError && !host.isDeclarationEmitBlocked(declarationFilePath, sourceFile)) {
1611+
let emitSkipped = emitDeclarationResult.reportedDeclarationError || host.isDeclarationEmitBlocked(declarationFilePath, sourceFile);
1612+
if (!emitSkipped) {
16121613
let declarationOutput = emitDeclarationResult.referencePathsOutput
16131614
+ getDeclarationOutput(emitDeclarationResult.synchronousDeclarationOutput, emitDeclarationResult.moduleElementDeclarationEmitInfo);
16141615
let compilerOptions = host.getCompilerOptions();
16151616
writeFile(host, diagnostics, declarationFilePath, declarationOutput, compilerOptions.emitBOM);
16161617
}
1618+
return emitSkipped;
16171619

16181620
function getDeclarationOutput(synchronousDeclarationOutput: string, moduleElementDeclarationEmitInfo: ModuleElementDeclarationEmitInfo[]) {
16191621
let appliedSyncOutputPos = 0;

src/compiler/emitter.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -323,35 +323,36 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
323323
let modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None;
324324
let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined;
325325
let diagnostics: Diagnostic[] = [];
326+
let emitSkipped = false;
326327
let newLine = host.getNewLine();
327328

328329
if (targetSourceFile === undefined) {
329330
forEach(host.getSourceFiles(), sourceFile => {
330331
if (shouldEmitToOwnFile(sourceFile, compilerOptions)) {
331-
emitFile(getEmitFileNames(sourceFile, host), sourceFile);
332+
emitSkipped = emitFile(getEmitFileNames(sourceFile, host), sourceFile) || emitSkipped;
332333
}
333334
});
334335

335336
if (compilerOptions.outFile || compilerOptions.out) {
336-
emitFile(getBundledEmitFileNames(compilerOptions));
337+
emitSkipped = emitFile(getBundledEmitFileNames(compilerOptions)) || emitSkipped;
337338
}
338339
}
339340
else {
340341
// targetSourceFile is specified (e.g calling emitter from language service or calling getSemanticDiagnostic from language service)
341342
if (shouldEmitToOwnFile(targetSourceFile, compilerOptions)) {
342-
emitFile(getEmitFileNames(targetSourceFile, host), targetSourceFile);
343+
emitSkipped = emitFile(getEmitFileNames(targetSourceFile, host), targetSourceFile) || emitSkipped;
343344
}
344345
else if (!isDeclarationFile(targetSourceFile) &&
345346
(compilerOptions.outFile || compilerOptions.out)) {
346-
emitFile(getBundledEmitFileNames(compilerOptions));
347+
emitSkipped = emitFile(getBundledEmitFileNames(compilerOptions)) || emitSkipped;
347348
}
348349
}
349350

350351
// Sort and make the unique list of diagnostics
351352
diagnostics = sortAndDeduplicateDiagnostics(diagnostics);
352353

353354
return {
354-
emitSkipped: false,
355+
emitSkipped,
355356
diagnostics,
356357
sourceMaps: sourceMapDataList
357358
};
@@ -7597,13 +7598,16 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
75977598

75987599
function emitFile({ jsFilePath, sourceMapFilePath, declarationFilePath}: { jsFilePath: string, sourceMapFilePath: string, declarationFilePath: string }, sourceFile?: SourceFile) {
75997600
// Make sure not to write js File and source map file if any of them cannot be written
7600-
if (!host.isEmitBlocked(jsFilePath) && (!sourceMapFilePath || !host.isEmitBlocked(sourceMapFilePath))) {
7601+
let emitSkipped = host.isEmitBlocked(jsFilePath) || (sourceMapFilePath && host.isEmitBlocked(sourceMapFilePath));
7602+
if (!emitSkipped) {
76017603
emitJavaScript(jsFilePath, sourceMapFilePath, sourceFile);
76027604
}
76037605

76047606
if (compilerOptions.declaration) {
7605-
writeDeclarationFile(declarationFilePath, sourceFile, host, resolver, diagnostics);
7607+
emitSkipped = writeDeclarationFile(declarationFilePath, sourceFile, host, resolver, diagnostics) || emitSkipped;
76067608
}
7609+
7610+
return emitSkipped;
76077611
}
76087612
}
76097613
}

src/compiler/program.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ namespace ts {
293293
program.getSemanticDiagnostics(sourceFile, cancellationToken));
294294

295295
if (program.getCompilerOptions().declaration) {
296-
diagnostics.concat(program.getDeclarationDiagnostics(sourceFile, cancellationToken));
296+
diagnostics = diagnostics.concat(program.getDeclarationDiagnostics(sourceFile, cancellationToken));
297297
}
298298

299299
return sortAndDeduplicateDiagnostics(diagnostics);

src/harness/harness.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ namespace Harness {
11211121

11221122
let emitResult = program.emit();
11231123

1124-
let errors = ts.getPreEmitDiagnostics(program).concat(emitResult.diagnostics);
1124+
let errors = ts.getPreEmitDiagnostics(program);
11251125
this.lastErrors = errors;
11261126

11271127
let result = new CompilerResult(fileOutputs, errors, program, Harness.IO.getCurrentDirectory(), emitResult.sourceMaps);
@@ -1666,7 +1666,7 @@ namespace Harness {
16661666
let encoded_actual = Utils.encodeString(actual);
16671667
if (expected != encoded_actual) {
16681668
// Overwrite & issue error
1669-
let errMsg = "The baseline file " + relativeFileName + " has changed.\nExpected:\n" + expected + "\nActual:\n" + encoded_actual;
1669+
let errMsg = "The baseline file " + relativeFileName + " has changed.";
16701670
throw new Error(errMsg);
16711671
}
16721672
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
EmitSkipped: true
2+
Diagnostics:
3+
Cannot write file 'tests/cases/fourslash/b.js' which is one of the input files.
4+
5+
EmitSkipped: false
6+
FileName : tests/cases/fourslash/a.js
7+
function foo2() { return 30; } // no error - should emit a.js
8+

tests/baselines/reference/getEmitOutputWithEmitterErrors.baseline

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
EmitSkipped: false
1+
EmitSkipped: true
2+
Diagnostics:
3+
Exported variable 'foo' has or is using private name 'C'.
24
FileName : tests/cases/fourslash/inputFile.js
35
var M;
46
(function (M) {

tests/baselines/reference/getEmitOutputWithEmitterErrors2.baseline

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
EmitSkipped: false
1+
EmitSkipped: true
2+
Diagnostics:
3+
Exported variable 'foo' has or is using private name 'C'.
24
FileName : tests/cases/fourslash/inputFile.js
35
define(["require", "exports"], function (require, exports) {
46
var C = (function () {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
EmitSkipped: false
1+
EmitSkipped: true
2+
Diagnostics:
3+
Type 'string' is not assignable to type 'number'.
24
FileName : tests/cases/fourslash/inputFile.js
35
var x = "hello world";
46

tests/baselines/reference/getEmitOutputWithSemanticErrorsForMultipleFiles2.baseline

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
EmitSkipped: false
1+
EmitSkipped: true
2+
Diagnostics:
3+
Type 'string' is not assignable to type 'boolean'.
24
FileName : out.js
35
// File to emit, does not contain semantic errors, but --out is passed
46
// expected to not generate declarations because of the semantic errors in the other file
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @BaselineFile: compileOnSaveWorksWhenEmitBlockingErrorOnOtherFile.baseline
4+
// @jsExtensions: js
5+
// @Filename: b.js
6+
// @emitThisFile: true
7+
////function foo() { } // This has error because js file cannot be overwritten - emitSkipped should be true
8+
9+
// @Filename: a.ts
10+
// @emitThisFile: true
11+
////function foo2() { return 30; } // no error - should emit a.js
12+
13+
verify.baselineGetEmitOutput();

0 commit comments

Comments
 (0)