Skip to content

Commit 9e05abc

Browse files
committed
Make BuilderProgram as Program
1 parent 0cabb00 commit 9e05abc

File tree

7 files changed

+30
-149
lines changed

7 files changed

+30
-149
lines changed

src/compiler/builder.ts

Lines changed: 20 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -411,21 +411,13 @@ namespace ts {
411411
oldProgram = undefined;
412412
oldState = undefined;
413413

414-
const result: BuilderProgram = {
415-
getState: () => state,
416-
getProgram: () => state.program,
417-
getCompilerOptions: () => state.program.getCompilerOptions(),
418-
getSourceFile: fileName => state.program.getSourceFile(fileName),
419-
getSourceFiles: () => state.program.getSourceFiles(),
420-
getOptionsDiagnostics: cancellationToken => state.program.getOptionsDiagnostics(cancellationToken),
421-
getGlobalDiagnostics: cancellationToken => state.program.getGlobalDiagnostics(cancellationToken),
422-
getConfigFileParsingDiagnostics: () => configFileParsingDiagnostics || state.program.getConfigFileParsingDiagnostics(),
423-
getSyntacticDiagnostics: (sourceFile, cancellationToken) => state.program.getSyntacticDiagnostics(sourceFile, cancellationToken),
424-
getSemanticDiagnostics,
425-
emit,
426-
getAllDependencies: sourceFile => BuilderState.getAllDependencies(state, state.program, sourceFile),
427-
getCurrentDirectory: () => state.program.getCurrentDirectory()
428-
};
414+
const result = createRedirectObject(state.program) as BuilderProgram;
415+
result.getState = () => state;
416+
result.getProgram = () => state.program;
417+
result.getAllDependencies = sourceFile => BuilderState.getAllDependencies(state, state.program, sourceFile);
418+
result.getConfigFileParsingDiagnostics = () => configFileParsingDiagnostics;
419+
result.getSemanticDiagnostics = getSemanticDiagnostics;
420+
result.emit = emit;
429421

430422
if (kind === BuilderProgramKind.SemanticDiagnosticsBuilderProgram) {
431423
(result as SemanticDiagnosticsBuilderProgram).getSemanticDiagnosticsOfNextAffectedFile = getSemanticDiagnosticsOfNextAffectedFile;
@@ -595,45 +587,20 @@ namespace ts {
595587
/**
596588
* Builder to manage the program state changes
597589
*/
598-
export interface BuilderProgram {
590+
export interface BuilderProgram extends Program {
599591
/*@internal*/
600592
getState(): BuilderProgramState;
601593
/**
602594
* Returns current program
603595
*/
604596
getProgram(): Program;
605-
/**
606-
* Get compiler options of the program
607-
*/
608-
getCompilerOptions(): CompilerOptions;
609-
/**
610-
* Get the source file in the program with file name
611-
*/
612-
getSourceFile(fileName: string): SourceFile | undefined;
613-
/**
614-
* Get a list of files in the program
615-
*/
616-
getSourceFiles(): ReadonlyArray<SourceFile>;
617-
/**
618-
* Get the diagnostics for compiler options
619-
*/
620-
getOptionsDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
621-
/**
622-
* Get the diagnostics that dont belong to any file
623-
*/
624-
getGlobalDiagnostics(cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
625-
/**
626-
* Get the diagnostics from config file parsing
627-
*/
628-
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
629-
/**
630-
* Get the syntax diagnostics, for all source files if source file is not supplied
631-
*/
632-
getSyntacticDiagnostics(sourceFile?: SourceFile, cancellationToken?: CancellationToken): ReadonlyArray<Diagnostic>;
633597
/**
634598
* Get all the dependencies of the file
635599
*/
636600
getAllDependencies(sourceFile: SourceFile): ReadonlyArray<string>;
601+
602+
// These two are same signatures but because the doc comments are useful they are retained
603+
637604
/**
638605
* Gets the semantic diagnostics from the program corresponding to this state of file (if provided) or whole program
639606
* The semantic diagnostics are cached and managed here
@@ -655,10 +622,6 @@ namespace ts {
655622
* in that order would be used to write the files
656623
*/
657624
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
658-
/**
659-
* Get the current directory of the program
660-
*/
661-
getCurrentDirectory(): string;
662625
}
663626

664627
/**
@@ -710,22 +673,14 @@ namespace ts {
710673
export function createAbstractBuilder(newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): BuilderProgram;
711674
export function createAbstractBuilder(rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>, projectReferences?: ReadonlyArray<ProjectReference>): BuilderProgram;
712675
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 {
713-
const { newProgram: program } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics, projectReferences);
714-
return {
715-
// Only return program, all other methods are not implemented
716-
getProgram: () => program,
717-
getState: notImplemented,
718-
getCompilerOptions: notImplemented,
719-
getSourceFile: notImplemented,
720-
getSourceFiles: notImplemented,
721-
getOptionsDiagnostics: notImplemented,
722-
getGlobalDiagnostics: notImplemented,
723-
getConfigFileParsingDiagnostics: notImplemented,
724-
getSyntacticDiagnostics: notImplemented,
725-
getSemanticDiagnostics: notImplemented,
726-
emit: notImplemented,
727-
getAllDependencies: notImplemented,
728-
getCurrentDirectory: notImplemented
729-
};
676+
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;
730685
}
731686
}

src/compiler/core.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,6 +1370,10 @@ namespace ts {
13701370
return result;
13711371
}
13721372

1373+
export function createRedirectObject<T extends object>(redirectTarget: T): T {
1374+
return Object.create(redirectTarget);
1375+
}
1376+
13731377
export function extend<T1, T2>(first: T1, second: T2): T1 & T2 {
13741378
const result: T1 & T2 = <any>{};
13751379
for (const id in second) {

src/compiler/program.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2180,7 +2180,7 @@ namespace ts {
21802180
}
21812181

21822182
function createRedirectSourceFile(redirectTarget: SourceFile, unredirected: SourceFile, fileName: string, path: Path, resolvedPath: Path, originalFileName: string): SourceFile {
2183-
const redirect: SourceFile = Object.create(redirectTarget);
2183+
const redirect = createRedirectObject(redirectTarget);
21842184
redirect.fileName = fileName;
21852185
redirect.path = path;
21862186
redirect.resolvedPath = resolvedPath;

src/compiler/tsbuild.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,9 @@ namespace ts {
10651065

10661066
// Don't emit anything in the presence of syntactic errors or options diagnostics
10671067
const syntaxDiagnostics = [
1068-
...program.getOptionsDiagnostics(),
10691068
...program.getConfigFileParsingDiagnostics(),
1069+
...program.getOptionsDiagnostics(),
1070+
...program.getGlobalDiagnostics(),
10701071
...program.getSyntacticDiagnostics()];
10711072
if (syntaxDiagnostics.length) {
10721073
return buildErrors(syntaxDiagnostics, BuildResultFlags.SyntaxErrors, "Syntactic");

src/compiler/watch.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,21 +88,6 @@ namespace ts {
8888
return result;
8989
}
9090

91-
/**
92-
* Program structure needed to emit the files and report diagnostics
93-
*/
94-
export interface ProgramToEmitFilesAndReportErrors {
95-
getCurrentDirectory(): string;
96-
getCompilerOptions(): CompilerOptions;
97-
getSourceFiles(): ReadonlyArray<SourceFile>;
98-
getSyntacticDiagnostics(): ReadonlyArray<Diagnostic>;
99-
getOptionsDiagnostics(): ReadonlyArray<Diagnostic>;
100-
getGlobalDiagnostics(): ReadonlyArray<Diagnostic>;
101-
getSemanticDiagnostics(): ReadonlyArray<Diagnostic>;
102-
getConfigFileParsingDiagnostics(): ReadonlyArray<Diagnostic>;
103-
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback): EmitResult;
104-
}
105-
10691
export type ReportEmitErrorSummary = (errorCount: number) => void;
10792

10893
export function getErrorCountForSummary(diagnostics: ReadonlyArray<Diagnostic>) {
@@ -124,7 +109,7 @@ namespace ts {
124109
/**
125110
* Helper that emit files, report diagnostics and lists emitted and/or source files depending on compiler options
126111
*/
127-
export function emitFilesAndReportErrors(program: ProgramToEmitFilesAndReportErrors, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void, reportSummary?: ReportEmitErrorSummary, writeFile?: WriteFileCallback) {
112+
export function emitFilesAndReportErrors(program: Program, reportDiagnostic: DiagnosticReporter, writeFileName?: (s: string) => void, reportSummary?: ReportEmitErrorSummary, writeFile?: WriteFileCallback) {
128113
// First get and report any syntactic errors.
129114
const diagnostics = program.getConfigFileParsingDiagnostics().slice();
130115
const configFileParsingDiagnosticsLength = diagnostics.length;

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

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,39 +4245,11 @@ declare namespace ts {
42454245
/**
42464246
* Builder to manage the program state changes
42474247
*/
4248-
interface BuilderProgram {
4248+
interface BuilderProgram extends Program {
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>;
42814253
/**
42824254
* Get all the dependencies of the file
42834255
*/
@@ -4303,10 +4275,6 @@ declare namespace ts {
43034275
* in that order would be used to write the files
43044276
*/
43054277
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
4306-
/**
4307-
* Get the current directory of the program
4308-
*/
4309-
getCurrentDirectory(): string;
43104278
}
43114279
/**
43124280
* 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: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4245,39 +4245,11 @@ declare namespace ts {
42454245
/**
42464246
* Builder to manage the program state changes
42474247
*/
4248-
interface BuilderProgram {
4248+
interface BuilderProgram extends Program {
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>;
42814253
/**
42824254
* Get all the dependencies of the file
42834255
*/
@@ -4303,10 +4275,6 @@ declare namespace ts {
43034275
* in that order would be used to write the files
43044276
*/
43054277
emit(targetSourceFile?: SourceFile, writeFile?: WriteFileCallback, cancellationToken?: CancellationToken, emitOnlyDtsFiles?: boolean, customTransformers?: CustomTransformers): EmitResult;
4306-
/**
4307-
* Get the current directory of the program
4308-
*/
4309-
getCurrentDirectory(): string;
43104278
}
43114279
/**
43124280
* The builder that caches the semantic diagnostics for the program and handles the changed files and affected files

0 commit comments

Comments
 (0)