Skip to content

Commit 56a76d8

Browse files
committed
Revert BuilderProgram to be redirected object to Program in preparation to set Program in state to undefined for storing.
1 parent 48baa42 commit 56a76d8

File tree

6 files changed

+150
-19
lines changed

6 files changed

+150
-19
lines changed

src/compiler/builder.ts

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -411,11 +411,9 @@ namespace ts {
411411
oldProgram = undefined;
412412
oldState = undefined;
413413

414-
const result = createRedirectObject(state.program) as BuilderProgram;
414+
const result = createRedirectedBuilderProgram(state, configFileParsingDiagnostics);
415415
result.getState = () => state;
416-
result.getProgram = () => state.program;
417416
result.getAllDependencies = sourceFile => BuilderState.getAllDependencies(state, state.program, sourceFile);
418-
result.getConfigFileParsingDiagnostics = () => configFileParsingDiagnostics;
419417
result.getSemanticDiagnostics = getSemanticDiagnostics;
420418
result.emit = emit;
421419

@@ -563,6 +561,25 @@ namespace ts {
563561
return diagnostics || emptyArray;
564562
}
565563
}
564+
565+
export function createRedirectedBuilderProgram(state: { program: Program; }, configFileParsingDiagnostics: ReadonlyArray<Diagnostic>): BuilderProgram {
566+
return {
567+
getState: notImplemented,
568+
getProgram: () => state.program,
569+
getCompilerOptions: () => state.program.getCompilerOptions(),
570+
getSourceFile: fileName => state.program.getSourceFile(fileName),
571+
getSourceFiles: () => state.program.getSourceFiles(),
572+
getOptionsDiagnostics: cancellationToken => state.program.getOptionsDiagnostics(cancellationToken),
573+
getGlobalDiagnostics: cancellationToken => state.program.getGlobalDiagnostics(cancellationToken),
574+
getConfigFileParsingDiagnostics: () => configFileParsingDiagnostics,
575+
getSyntacticDiagnostics: (sourceFile, cancellationToken) => state.program.getSyntacticDiagnostics(sourceFile, cancellationToken),
576+
getDeclarationDiagnostics: (sourceFile, cancellationToken) => state.program.getDeclarationDiagnostics(sourceFile, cancellationToken),
577+
getSemanticDiagnostics: (sourceFile, cancellationToken) => state.program.getSemanticDiagnostics(sourceFile, cancellationToken),
578+
emit: (sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers) => state.program.emit(sourceFile, writeFile, cancellationToken, emitOnlyDts, customTransformers),
579+
getAllDependencies: notImplemented,
580+
getCurrentDirectory: () => state.program.getCurrentDirectory()
581+
};
582+
}
566583
}
567584

568585
namespace ts {
@@ -587,20 +604,50 @@ namespace ts {
587604
/**
588605
* Builder to manage the program state changes
589606
*/
590-
export interface BuilderProgram extends Program {
607+
export interface BuilderProgram {
591608
/*@internal*/
592609
getState(): BuilderProgramState;
593610
/**
594611
* Returns current program
595612
*/
596613
getProgram(): Program;
614+
/**
615+
* Get compiler options of the program
616+
*/
617+
getCompilerOptions(): CompilerOptions;
618+
/**
619+
* Get the source file in the program with file name
620+
*/
621+
getSourceFile(fileName: string): SourceFile | undefined;
622+
/**
623+
* Get a list of files in the program
624+
*/
625+
getSourceFiles(): ReadonlyArray<SourceFile>;
626+
/**
627+
* Get the diagnostics for compiler options
628+
*/
629+
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
630+
/**
631+
* Get the diagnostics that dont belong to any file
632+
*/
633+
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
634+
/**
635+
* Get the diagnostics from config file parsing
636+
*/
637+
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
638+
/**
639+
* Get the syntax diagnostics, for all source files if source file is not supplied
640+
*/
641+
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
642+
/**
643+
* Get the declaration diagnostics, for all source files if source file is not supplied
644+
*/
645+
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
597646
/**
598647
* Get all the dependencies of the file
599648
*/
600649
getAllDependencies(sourceFile: SourceFile): ReadonlyArray<string>;
601650

602-
// These two are same signatures but because the doc comments are useful they are retained
603-
604651
/**
605652
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
606653
* The semantic diagnostics are cached and managed here
@@ -622,6 +669,10 @@ namespace ts {
622669
* in that order would be used to write the files
623670
*/
624671
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
672+
/**
673+
* Get the current directory of the program
674+
*/
675+
getCurrentDirectory(): string;
625676
}
626677

627678
/**
@@ -674,13 +725,6 @@ namespace ts {
674725
export function createAbstractBuilder(rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>, projectReferences?: ReadonlyArray<ProjectReference>): BuilderProgram;
675726
export function createAbstractBuilder(newProgramOrRootNames: Program | ReadonlyArray<string> | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | BuilderProgram, configFileParsingDiagnosticsOrOldProgram?: ReadonlyArray<Diagnostic> | BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>, projectReferences?: ReadonlyArray<ProjectReference>): BuilderProgram {
676727
const { newProgram, configFileParsingDiagnostics: newConfigFileParsingDiagnostics } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences);
677-
const builderProgram = createRedirectObject(newProgram) as BuilderProgram;
678-
builderProgram.getState = notImplemented;
679-
builderProgram.getProgram = () => newProgram;
680-
builderProgram.getAllDependencies = notImplemented;
681-
682-
// Always return latest config file diagnostics
683-
builderProgram.getConfigFileParsingDiagnostics = () => newConfigFileParsingDiagnostics;
684-
return builderProgram;
728+
return createRedirectedBuilderProgram({ program: newProgram }, newConfigFileParsingDiagnostics);
685729
}
686730
}

src/compiler/tsbuild.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ namespace ts {
331331

332332
// TODO: To do better with watch mode and normal build mode api that creates program and emits files
333333
// This currently helps enable --diagnostics and --extendedDiagnostics
334-
afterProgramEmitAndDiagnostics?(program: Program): void;
334+
afterProgramEmitAndDiagnostics?(program: T): void;
335335
}
336336

337337
export interface SolutionBuilderHost<T extends BuilderProgram> extends SolutionBuilderHostBase<T> {

src/compiler/watch.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,25 @@ namespace ts {
106106
return `${newLine}${flattenDiagnosticMessageText(d.messageText, newLine)}${newLine}${newLine}`;
107107
}
108108

109+
/**
110+
* Program structure needed to emit the files and report diagnostics
111+
*/
112+
export interface ProgramToEmitFilesAndReportErrors {
113+
getCurrentDirectory(): string;
114+
getCompilerOptions(): CompilerOptions;
115+
getSourceFiles(): ReadonlyArray<SourceFile>;
116+
getSyntacticDiagnostics(): ReadonlyArray<Diagnostic>;
117+
getOptionsDiagnostics(): ReadonlyArray<Diagnostic>;
118+
getGlobalDiagnostics(): ReadonlyArray<Diagnostic>;
119+
getSemanticDiagnostics(): ReadonlyArray<Diagnostic>;
120+
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
121+
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult;
122+
}
123+
109124
/**
110125
* Helper that emit files, report diagnostics and lists emitted and/or source files depending on compiler options
111126
*/
112-
export function emitFilesAndReportErrors(program: Program, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void, reportSummary?: ReportEmitErrorSummary, writeFile?: WriteFileCallback) {
127+
export function emitFilesAndReportErrors(program: ProgramToEmitFilesAndReportErrors, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void, reportSummary?: ReportEmitErrorSummary, writeFile?: WriteFileCallback) {
113128
// First get and report any syntactic errors.
114129
const diagnostics = program.getConfigFileParsingDiagnostics().slice();
115130
const configFileParsingDiagnosticsLength = diagnostics.length;

src/tsc/tsc.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ namespace ts {
209209
createSolutionBuilderWithWatchHost(sys, createSemanticDiagnosticsBuilderProgram, reportDiagnostic, createBuilderStatusReporter(sys, shouldBePretty()), createWatchStatusReporter()) :
210210
createSolutionBuilderHost(sys, createAbstractBuilder, reportDiagnostic, createBuilderStatusReporter(sys, shouldBePretty()), createReportErrorSummary(buildOptions));
211211
updateCreateProgram(buildHost);
212-
buildHost.afterProgramEmitAndDiagnostics = reportStatistics;
212+
buildHost.afterProgramEmitAndDiagnostics = (program: BuilderProgram) => reportStatistics(program.getProgram());
213213

214214
const builder = createSolutionBuilder(buildHost, projects, buildOptions);
215215
if (buildOptions.clean) {

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4245,11 +4245,43 @@ declare namespace ts {
42454245
/**
42464246
* Builder to manage the program state changes
42474247
*/
4248-
interface BuilderProgram extends Program {
4248+
interface BuilderProgram {
42494249
/**
42504250
* Returns current program
42514251
*/
42524252
getProgram(): Program;
4253+
/**
4254+
* Get compiler options of the program
4255+
*/
4256+
getCompilerOptions(): CompilerOptions;
4257+
/**
4258+
* Get the source file in the program with file name
4259+
*/
4260+
getSourceFile(fileName: string): SourceFile | undefined;
4261+
/**
4262+
* Get a list of files in the program
4263+
*/
4264+
getSourceFiles(): ReadonlyArray<SourceFile>;
4265+
/**
4266+
* Get the diagnostics for compiler options
4267+
*/
4268+
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
4269+
/**
4270+
* Get the diagnostics that dont belong to any file
4271+
*/
4272+
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
4273+
/**
4274+
* Get the diagnostics from config file parsing
4275+
*/
4276+
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
4277+
/**
4278+
* Get the syntax diagnostics, for all source files if source file is not supplied
4279+
*/
4280+
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
4281+
/**
4282+
* Get the declaration diagnostics, for all source files if source file is not supplied
4283+
*/
4284+
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
42534285
/**
42544286
* Get all the dependencies of the file
42554287
*/
@@ -4275,6 +4307,10 @@ declare namespace ts {
42754307
* in that order would be used to write the files
42764308
*/
42774309
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
4310+
/**
4311+
* Get the current directory of the program
4312+
*/
4313+
getCurrentDirectory(): string;
42784314
}
42794315
/**
42804316
* The builder that caches the semantic diagnostics for the program and handles the changed files and affected files

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

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4245,11 +4245,43 @@ declare namespace ts {
42454245
/**
42464246
* Builder to manage the program state changes
42474247
*/
4248-
interface BuilderProgram extends Program {
4248+
interface BuilderProgram {
42494249
/**
42504250
* Returns current program
42514251
*/
42524252
getProgram(): Program;
4253+
/**
4254+
* Get compiler options of the program
4255+
*/
4256+
getCompilerOptions(): CompilerOptions;
4257+
/**
4258+
* Get the source file in the program with file name
4259+
*/
4260+
getSourceFile(fileName: string): SourceFile | undefined;
4261+
/**
4262+
* Get a list of files in the program
4263+
*/
4264+
getSourceFiles(): ReadonlyArray<SourceFile>;
4265+
/**
4266+
* Get the diagnostics for compiler options
4267+
*/
4268+
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
4269+
/**
4270+
* Get the diagnostics that dont belong to any file
4271+
*/
4272+
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
4273+
/**
4274+
* Get the diagnostics from config file parsing
4275+
*/
4276+
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
4277+
/**
4278+
* Get the syntax diagnostics, for all source files if source file is not supplied
4279+
*/
4280+
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
4281+
/**
4282+
* Get the declaration diagnostics, for all source files if source file is not supplied
4283+
*/
4284+
getDeclarationDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<DiagnosticWithLocation>;
42534285
/**
42544286
* Get all the dependencies of the file
42554287
*/
@@ -4275,6 +4307,10 @@ declare namespace ts {
42754307
* in that order would be used to write the files
42764308
*/
42774309
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
4310+
/**
4311+
* Get the current directory of the program
4312+
*/
4313+
getCurrentDirectory(): string;
42784314
}
42794315
/**
42804316
* The builder that caches the semantic diagnostics for the program and handles the changed files and affected files

0 commit comments

Comments
 (0)