Skip to content

Commit e102fee

Browse files
committed
Use the results from affected file enumerator apis as Affected File result
1 parent 85ce1d0 commit e102fee

4 files changed

Lines changed: 36 additions & 25 deletions

File tree

src/compiler/builder.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -331,33 +331,41 @@ namespace ts {
331331
}
332332
}
333333

334+
/**
335+
* Returns the result with affected file
336+
*/
337+
function toAffectedFileResult<T>(result: T, affectedFile?: SourceFile): AffectedFileResult<T> {
338+
return { result, affectedFile };
339+
}
340+
334341
/**
335342
* Emits the next affected file, and returns the EmitResult along with source files emitted
336343
* Returns undefined when iteration is complete
337344
*/
338-
function emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileEmitResult | undefined {
345+
function emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult> {
339346
const affectedFile = getNextAffectedFile(programOfThisState);
340347
if (!affectedFile) {
341348
// Done
342349
return undefined;
343350
}
344351
else if (affectedFile === programOfThisState) {
345352
// When whole program is affected, do emit only once (eg when --out or --outFile is specified)
346-
return programOfThisState.emit(/*targetSourceFile*/ undefined, writeFileCallback, cancellationToken, /*emitOnlyDtsFiles*/ false, customTransformers);
353+
return toAffectedFileResult(programOfThisState.emit(/*targetSourceFile*/ undefined, writeFileCallback, cancellationToken, /*emitOnlyDtsFiles*/ false, customTransformers));
347354
}
348355

349356
// Emit the affected file
350357
const targetSourceFile = affectedFile as SourceFile;
351-
const result = programOfThisState.emit(targetSourceFile, writeFileCallback, cancellationToken, /*emitOnlyDtsFiles*/ false, customTransformers) as AffectedFileEmitResult;
352-
result.affectedFile = targetSourceFile;
353-
return result;
358+
return toAffectedFileResult(
359+
programOfThisState.emit(targetSourceFile, writeFileCallback, cancellationToken, /*emitOnlyDtsFiles*/ false, customTransformers),
360+
targetSourceFile
361+
);
354362
}
355363

356364
/**
357365
* Return the semantic diagnostics for the next affected file or undefined if iteration is complete
358366
* If provided ignoreSourceFile would be called before getting the diagnostics and would ignore the sourceFile if the returned value was true
359367
*/
360-
function getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): ReadonlyArray<Diagnostic> {
368+
function getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<ReadonlyArray<Diagnostic>> {
361369
while (true) {
362370
const affectedFile = getNextAffectedFile(programOfThisState);
363371
if (!affectedFile) {
@@ -366,7 +374,7 @@ namespace ts {
366374
}
367375
else if (affectedFile === programOfThisState) {
368376
// When whole program is affected, get all semantic diagnostics (eg when --out or --outFile is specified)
369-
return programOfThisState.getSemanticDiagnostics(/*targetSourceFile*/ undefined, cancellationToken);
377+
return toAffectedFileResult(programOfThisState.getSemanticDiagnostics(/*targetSourceFile*/ undefined, cancellationToken));
370378
}
371379

372380
// Get diagnostics for the affected file if its not ignored
@@ -376,7 +384,10 @@ namespace ts {
376384
continue;
377385
}
378386

379-
return getSemanticDiagnosticsOfFile(programOfThisState, targetSourceFile, cancellationToken);
387+
return toAffectedFileResult(
388+
getSemanticDiagnosticsOfFile(programOfThisState, targetSourceFile, cancellationToken),
389+
targetSourceFile
390+
);
380391
}
381392
}
382393

@@ -682,9 +693,7 @@ namespace ts {
682693
text: string;
683694
}
684695

685-
export interface AffectedFileEmitResult extends EmitResult {
686-
affectedFile?: SourceFile;
687-
}
696+
export type AffectedFileResult<T> = { result: T; affectedFile?: SourceFile; } | undefined;
688697

689698
export interface BuilderOptions {
690699
getCanonicalFileName: (fileName: string) => string;
@@ -714,7 +723,7 @@ namespace ts {
714723
* Gets the semantic diagnostics from the program for the next affected file and caches it
715724
* Returns undefined if the iteration is complete
716725
*/
717-
getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): ReadonlyArray<Diagnostic>;
726+
getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<ReadonlyArray<Diagnostic>>;
718727

719728
/**
720729
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
@@ -733,7 +742,7 @@ namespace ts {
733742
/**
734743
* Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete
735744
*/
736-
emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileEmitResult | undefined;
745+
emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult>;
737746

738747
/**
739748
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program

src/compiler/watch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ namespace ts {
116116
let sourceMaps: SourceMapData[];
117117
let emitSkipped: boolean;
118118

119-
let affectedEmitResult: AffectedFileEmitResult;
119+
let affectedEmitResult: AffectedFileResult<EmitResult>;
120120
while (affectedEmitResult = builder.emitNextAffectedFile(program, writeFile)) {
121-
emitSkipped = emitSkipped || affectedEmitResult.emitSkipped;
122-
addRange(diagnostics, affectedEmitResult.diagnostics);
123-
sourceMaps = addRange(sourceMaps, affectedEmitResult.sourceMaps);
121+
emitSkipped = emitSkipped || affectedEmitResult.result.emitSkipped;
122+
addRange(diagnostics, affectedEmitResult.result.diagnostics);
123+
sourceMaps = addRange(sourceMaps, affectedEmitResult.result.sourceMaps);
124124
}
125125

126126
if (reportSemanticDiagnostics) {

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3771,9 +3771,10 @@ declare namespace ts {
37713771
writeByteOrderMark: boolean;
37723772
text: string;
37733773
}
3774-
interface AffectedFileEmitResult extends EmitResult {
3774+
type AffectedFileResult<T> = {
3775+
result: T;
37753776
affectedFile?: SourceFile;
3776-
}
3777+
} | undefined;
37773778
interface BuilderOptions {
37783779
getCanonicalFileName: (fileName: string) => string;
37793780
computeHash: (data: string) => string;
@@ -3799,7 +3800,7 @@ declare namespace ts {
37993800
* Gets the semantic diagnostics from the program for the next affected file and caches it
38003801
* Returns undefined if the iteration is complete
38013802
*/
3802-
getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): ReadonlyArray<Diagnostic>;
3803+
getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<ReadonlyArray<Diagnostic>>;
38033804
/**
38043805
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
38053806
* The semantic diagnostics are cached and managed here
@@ -3816,7 +3817,7 @@ declare namespace ts {
38163817
/**
38173818
* Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete
38183819
*/
3819-
emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileEmitResult | undefined;
3820+
emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult>;
38203821
/**
38213822
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
38223823
* The semantic diagnostics are cached and managed here

tests/baselines/reference/api/typescript.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3718,9 +3718,10 @@ declare namespace ts {
37183718
writeByteOrderMark: boolean;
37193719
text: string;
37203720
}
3721-
interface AffectedFileEmitResult extends EmitResult {
3721+
type AffectedFileResult<T> = {
3722+
result: T;
37223723
affectedFile?: SourceFile;
3723-
}
3724+
} | undefined;
37243725
interface BuilderOptions {
37253726
getCanonicalFileName: (fileName: string) => string;
37263727
computeHash: (data: string) => string;
@@ -3746,7 +3747,7 @@ declare namespace ts {
37463747
* Gets the semantic diagnostics from the program for the next affected file and caches it
37473748
* Returns undefined if the iteration is complete
37483749
*/
3749-
getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): ReadonlyArray<Diagnostic>;
3750+
getSemanticDiagnosticsOfNextAffectedFile(programOfThisState: Program, cancellationToken?: CancellationToken, ignoreSourceFile?: (sourceFile: SourceFile) => boolean): AffectedFileResult<ReadonlyArray<Diagnostic>>;
37503751
/**
37513752
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
37523753
* The semantic diagnostics are cached and managed here
@@ -3763,7 +3764,7 @@ declare namespace ts {
37633764
/**
37643765
* Emits the next affected file's emit result (EmitResult and sourceFiles emitted) or returns undefined if iteration is complete
37653766
*/
3766-
emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileEmitResult | undefined;
3767+
emitNextAffectedFile(programOfThisState: Program, writeFileCallback: WriteFileCallback, cancellationToken?: CancellationToken, customTransformers?: CustomTransformers): AffectedFileResult<EmitResult>;
37673768
/**
37683769
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
37693770
* The semantic diagnostics are cached and managed here

0 commit comments

Comments
 (0)