Skip to content

Commit c52fb0e

Browse files
authored
Merge pull request #24665 from Microsoft/builderAPIBreak29
[release-2.9] Fix the issue with createSemanticDiagnosticsBuilderProgram, createEmitAndSemanticDiagnosticsBuilderProgram and createAbstractBuilder not assignable to CreateProgram<T>
2 parents 4e17e77 + 42a1164 commit c52fb0e

File tree

9 files changed

+530
-12
lines changed

9 files changed

+530
-12
lines changed

src/compiler/builder.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -548,8 +548,8 @@ namespace ts {
548548
* Create the builder to manage semantic diagnostics and cache them
549549
*/
550550
export function createSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): SemanticDiagnosticsBuilderProgram;
551-
export function createSemanticDiagnosticsBuilderProgram(rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): SemanticDiagnosticsBuilderProgram;
552-
export function createSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | ReadonlyArray<string>, hostOrOptions: BuilderProgramHost | CompilerOptions, oldProgramOrHost?: CompilerHost | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: ReadonlyArray<Diagnostic> | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>) {
551+
export function createSemanticDiagnosticsBuilderProgram(rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): SemanticDiagnosticsBuilderProgram;
552+
export function createSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | ReadonlyArray<string> | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: ReadonlyArray<Diagnostic> | SemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>) {
553553
return createBuilderProgram(BuilderProgramKind.SemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics));
554554
}
555555

@@ -558,17 +558,17 @@ namespace ts {
558558
* to emit the those files and manage semantic diagnostics cache as well
559559
*/
560560
export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgram: Program, host: BuilderProgramHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): EmitAndSemanticDiagnosticsBuilderProgram;
561-
export function createEmitAndSemanticDiagnosticsBuilderProgram(rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): EmitAndSemanticDiagnosticsBuilderProgram;
562-
export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | ReadonlyArray<string>, hostOrOptions: BuilderProgramHost | CompilerOptions, oldProgramOrHost?: CompilerHost | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: ReadonlyArray<Diagnostic> | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>) {
561+
export function createEmitAndSemanticDiagnosticsBuilderProgram(rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): EmitAndSemanticDiagnosticsBuilderProgram;
562+
export function createEmitAndSemanticDiagnosticsBuilderProgram(newProgramOrRootNames: Program | ReadonlyArray<string> | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnosticsOrOldProgram?: ReadonlyArray<Diagnostic> | EmitAndSemanticDiagnosticsBuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>) {
563563
return createBuilderProgram(BuilderProgramKind.EmitAndSemanticDiagnosticsBuilderProgram, getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics));
564564
}
565565

566566
/**
567567
* Creates a builder thats just abstraction over program and can be used with watch
568568
*/
569569
export function createAbstractBuilder(newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): BuilderProgram;
570-
export function createAbstractBuilder(rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): BuilderProgram;
571-
export function createAbstractBuilder(newProgramOrRootNames: Program | ReadonlyArray<string>, hostOrOptions: BuilderProgramHost | CompilerOptions, oldProgramOrHost?: CompilerHost | BuilderProgram, configFileParsingDiagnosticsOrOldProgram?: ReadonlyArray<Diagnostic> | BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): BuilderProgram {
570+
export function createAbstractBuilder(rootNames: ReadonlyArray<string> | undefined, options: CompilerOptions | undefined, host?: CompilerHost, oldProgram?: BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): BuilderProgram;
571+
export function createAbstractBuilder(newProgramOrRootNames: Program | ReadonlyArray<string> | undefined, hostOrOptions: BuilderProgramHost | CompilerOptions | undefined, oldProgramOrHost?: CompilerHost | BuilderProgram, configFileParsingDiagnosticsOrOldProgram?: ReadonlyArray<Diagnostic> | BuilderProgram, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>): BuilderProgram {
572572
const { newProgram: program } = getBuilderCreationParameters(newProgramOrRootNames, hostOrOptions, oldProgramOrHost, configFileParsingDiagnosticsOrOldProgram, configFileParsingDiagnostics);
573573
return {
574574
// Only return program, all other methods are not implemented
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
//// [APISample_Watch.ts]
2+
/*
3+
* Note: This test is a public API sample. The sample sources can be found
4+
at: https://github.com/Microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md#writing-an-incremental-program-watcher
5+
* Please log a "breaking change" issue for any API breaking change affecting this issue
6+
*/
7+
8+
declare var process: any;
9+
declare var console: any;
10+
declare var os: any;
11+
12+
import ts = require("typescript");
13+
14+
const formatHost: ts.FormatDiagnosticsHost = {
15+
getCanonicalFileName: path => path,
16+
getCurrentDirectory: ts.sys.getCurrentDirectory,
17+
getNewLine: () => ts.sys.newLine,
18+
}
19+
20+
function watchMain() {
21+
const configPath = ts.findConfigFile(/*searchPath*/ "./", ts.sys.fileExists, "tsconfig.json");
22+
if (!configPath) {
23+
throw new Error("Could not find a valid 'tsconfig.json'.");
24+
}
25+
26+
// TypeScript can use several different program creation "strategies":
27+
// * ts.createEmitAndSemanticDiagnosticsBuilderProgram,
28+
// * ts.createSemanticDiagnosticsBuilderProgram
29+
// * ts.createAbstractBuilder
30+
// The first two produce "builder programs". These use an incremental strategy to only re-check and emit files whose
31+
// contents may have changed, or whose dependencies may have changes which may impact change the result of prior type-check and emit.
32+
// The last uses an ordinary program which does a full type check after every change.
33+
// Between `createEmitAndSemanticDiagnosticsBuilderProgram` and `createSemanticDiagnosticsBuilderProgram`, the only difference is emit.
34+
// For pure type-checking scenarios, or when another tool/process handles emit, using `createSemanticDiagnosticsBuilderProgram` may be more desirable.
35+
36+
// Note that there is another overload for `createWatchCompilerHost` that takes a set of root files.
37+
const host = ts.createWatchCompilerHost(configPath, {}, ts.sys,
38+
ts.createSemanticDiagnosticsBuilderProgram,
39+
reportDiagnostic,
40+
reportWatchStatusChanged,
41+
);
42+
43+
// You can technically override any given hook on the host, though you probably don't need to.
44+
// Note that we're assuming `origCreateProgram` and `origPostProgramCreate` doesn't use `this` at all.
45+
const origCreateProgram = host.createProgram;
46+
host.createProgram = (rootNames: ReadonlyArray<string>, options, host, oldProgram) => {
47+
console.log("** We're about to create the program! **");
48+
return origCreateProgram(rootNames, options, host, oldProgram);
49+
}
50+
const origPostProgramCreate = host.afterProgramCreate;
51+
52+
host.afterProgramCreate = program => {
53+
console.log("** We finished making the program! **");
54+
origPostProgramCreate!(program);
55+
};
56+
57+
// `createWatchProgram` creates an initial program, watches files, and updates the program over time.
58+
ts.createWatchProgram(host);
59+
}
60+
61+
function reportDiagnostic(diagnostic: ts.Diagnostic) {
62+
console.error("Error", diagnostic.code, ":",
63+
ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine())
64+
);
65+
}
66+
67+
/**
68+
* Prints a diagnostic every time the watch status changes.
69+
* This is mainly for messages like "Starting compilation" or "Compilation completed".
70+
*/
71+
function reportWatchStatusChanged(diagnostic: ts.Diagnostic) {
72+
console.info(ts.formatDiagnostic(diagnostic, formatHost));
73+
}
74+
75+
watchMain();
76+
77+
78+
//// [APISample_Watch.js]
79+
"use strict";
80+
/*
81+
* Note: This test is a public API sample. The sample sources can be found
82+
at: https://github.com/Microsoft/TypeScript-wiki/blob/master/Using-the-Compiler-API.md#writing-an-incremental-program-watcher
83+
* Please log a "breaking change" issue for any API breaking change affecting this issue
84+
*/
85+
exports.__esModule = true;
86+
var ts = require("typescript");
87+
var formatHost = {
88+
getCanonicalFileName: function (path) { return path; },
89+
getCurrentDirectory: ts.sys.getCurrentDirectory,
90+
getNewLine: function () { return ts.sys.newLine; }
91+
};
92+
function watchMain() {
93+
var configPath = ts.findConfigFile(/*searchPath*/ "./", ts.sys.fileExists, "tsconfig.json");
94+
if (!configPath) {
95+
throw new Error("Could not find a valid 'tsconfig.json'.");
96+
}
97+
// TypeScript can use several different program creation "strategies":
98+
// * ts.createEmitAndSemanticDiagnosticsBuilderProgram,
99+
// * ts.createSemanticDiagnosticsBuilderProgram
100+
// * ts.createAbstractBuilder
101+
// The first two produce "builder programs". These use an incremental strategy to only re-check and emit files whose
102+
// contents may have changed, or whose dependencies may have changes which may impact change the result of prior type-check and emit.
103+
// The last uses an ordinary program which does a full type check after every change.
104+
// Between `createEmitAndSemanticDiagnosticsBuilderProgram` and `createSemanticDiagnosticsBuilderProgram`, the only difference is emit.
105+
// For pure type-checking scenarios, or when another tool/process handles emit, using `createSemanticDiagnosticsBuilderProgram` may be more desirable.
106+
// Note that there is another overload for `createWatchCompilerHost` that takes a set of root files.
107+
var host = ts.createWatchCompilerHost(configPath, {}, ts.sys, ts.createSemanticDiagnosticsBuilderProgram, reportDiagnostic, reportWatchStatusChanged);
108+
// You can technically override any given hook on the host, though you probably don't need to.
109+
// Note that we're assuming `origCreateProgram` and `origPostProgramCreate` doesn't use `this` at all.
110+
var origCreateProgram = host.createProgram;
111+
host.createProgram = function (rootNames, options, host, oldProgram) {
112+
console.log("** We're about to create the program! **");
113+
return origCreateProgram(rootNames, options, host, oldProgram);
114+
};
115+
var origPostProgramCreate = host.afterProgramCreate;
116+
host.afterProgramCreate = function (program) {
117+
console.log("** We finished making the program! **");
118+
origPostProgramCreate(program);
119+
};
120+
// `createWatchProgram` creates an initial program, watches files, and updates the program over time.
121+
ts.createWatchProgram(host);
122+
}
123+
function reportDiagnostic(diagnostic) {
124+
console.error("Error", diagnostic.code, ":", ts.flattenDiagnosticMessageText(diagnostic.messageText, formatHost.getNewLine()));
125+
}
126+
/**
127+
* Prints a diagnostic every time the watch status changes.
128+
* This is mainly for messages like "Starting compilation" or "Compilation completed".
129+
*/
130+
function reportWatchStatusChanged(diagnostic) {
131+
console.info(ts.formatDiagnostic(diagnostic, formatHost));
132+
}
133+
watchMain();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//// [APISample_WatchWithDefaults.ts]
2+
/*
3+
* Note: This test is a public API sample. This uses default sys interface without having to pass anything
4+
* Please log a "breaking change" issue for any API breaking change affecting this issue
5+
*/
6+
7+
declare var console: any;
8+
9+
import ts = require("typescript");
10+
11+
function watchMain() {
12+
const configPath = ts.findConfigFile(/*searchPath*/ "./", ts.sys.fileExists, "tsconfig.json");
13+
if (!configPath) {
14+
throw new Error("Could not find a valid 'tsconfig.json'.");
15+
}
16+
17+
// TypeScript can use several different program creation "strategies":
18+
// * ts.createEmitAndSemanticDiagnosticsBuilderProgram,
19+
// * ts.createSemanticDiagnosticsBuilderProgram
20+
// * ts.createAbstractBuilder
21+
// The first two produce "builder programs". These use an incremental strategy to only re-check and emit files whose
22+
// contents may have changed, or whose dependencies may have changes which may impact change the result of prior type-check and emit.
23+
// The last uses an ordinary program which does a full type check after every change.
24+
// Between `createEmitAndSemanticDiagnosticsBuilderProgram` and `createSemanticDiagnosticsBuilderProgram`, the only difference is emit.
25+
// For pure type-checking scenarios, or when another tool/process handles emit, using `createSemanticDiagnosticsBuilderProgram` may be more desirable.
26+
27+
// Note that there is another overload for `createWatchCompilerHost` that takes a set of root files.
28+
const host = ts.createWatchCompilerHost(configPath, {}, ts.sys);
29+
30+
// You can technically override any given hook on the host, though you probably don't need to.
31+
// Note that we're assuming `origCreateProgram` and `origPostProgramCreate` doesn't use `this` at all.
32+
const origCreateProgram = host.createProgram;
33+
host.createProgram = (rootNames, options, host, oldProgram) => {
34+
console.log("** We're about to create the program! **");
35+
return origCreateProgram(rootNames, options, host, oldProgram);
36+
}
37+
const origPostProgramCreate = host.afterProgramCreate;
38+
39+
host.afterProgramCreate = program => {
40+
console.log("** We finished making the program! **");
41+
origPostProgramCreate!(program);
42+
};
43+
44+
// `createWatchProgram` creates an initial program, watches files, and updates the program over time.
45+
ts.createWatchProgram(host);
46+
}
47+
48+
watchMain();
49+
50+
51+
//// [APISample_WatchWithDefaults.js]
52+
"use strict";
53+
/*
54+
* Note: This test is a public API sample. This uses default sys interface without having to pass anything
55+
* Please log a "breaking change" issue for any API breaking change affecting this issue
56+
*/
57+
exports.__esModule = true;
58+
var ts = require("typescript");
59+
function watchMain() {
60+
var configPath = ts.findConfigFile(/*searchPath*/ "./", ts.sys.fileExists, "tsconfig.json");
61+
if (!configPath) {
62+
throw new Error("Could not find a valid 'tsconfig.json'.");
63+
}
64+
// TypeScript can use several different program creation "strategies":
65+
// * ts.createEmitAndSemanticDiagnosticsBuilderProgram,
66+
// * ts.createSemanticDiagnosticsBuilderProgram
67+
// * ts.createAbstractBuilder
68+
// The first two produce "builder programs". These use an incremental strategy to only re-check and emit files whose
69+
// contents may have changed, or whose dependencies may have changes which may impact change the result of prior type-check and emit.
70+
// The last uses an ordinary program which does a full type check after every change.
71+
// Between `createEmitAndSemanticDiagnosticsBuilderProgram` and `createSemanticDiagnosticsBuilderProgram`, the only difference is emit.
72+
// For pure type-checking scenarios, or when another tool/process handles emit, using `createSemanticDiagnosticsBuilderProgram` may be more desirable.
73+
// Note that there is another overload for `createWatchCompilerHost` that takes a set of root files.
74+
var host = ts.createWatchCompilerHost(configPath, {}, ts.sys);
75+
// You can technically override any given hook on the host, though you probably don't need to.
76+
// Note that we're assuming `origCreateProgram` and `origPostProgramCreate` doesn't use `this` at all.
77+
var origCreateProgram = host.createProgram;
78+
host.createProgram = function (rootNames, options, host, oldProgram) {
79+
console.log("** We're about to create the program! **");
80+
return origCreateProgram(rootNames, options, host, oldProgram);
81+
};
82+
var origPostProgramCreate = host.afterProgramCreate;
83+
host.afterProgramCreate = function (program) {
84+
console.log("** We finished making the program! **");
85+
origPostProgramCreate(program);
86+
};
87+
// `createWatchProgram` creates an initial program, watches files, and updates the program over time.
88+
ts.createWatchProgram(host);
89+
}
90+
watchMain();

0 commit comments

Comments
 (0)