Skip to content

Commit 8427e95

Browse files
committed
Tests to acknowledge the public watch api
1 parent 75d0852 commit 8427e95

7 files changed

+589
-0
lines changed
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();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
tests/cases/compiler/APISample_WatchWithOwnWatchHost.ts(15,11): error TS2322: Type '{ rootFiles: string[]; options: CompilerOptions; useCaseSensitiveFileNames: () => boolean; getNewLine: () => string; getCurrentDirectory: () => string; getDefaultLibFileName: (options: CompilerOptions) => string; fileExists: (path: string) => boolean; readFile: (path: string, encoding?: string | undefined) => string | undefined; directoryExists: (path: string) => boolean; getDirectories: (path: string) => string[]; readDirectory: (path: string, extensions?: ReadonlyArray<string> | undefined, exclude?: ReadonlyArray<string> | undefined, include?: ReadonlyArray<string> | undefined, depth?: number | undefined) => string[]; realpath: ((path: string) => string) | undefined; watchFile: (path: string, callback: FileWatcherCallback, pollingInterval?: number | undefined) => FileWatcher; watchDirectory: (path: string, callback: DirectoryWatcherCallback, recursive?: boolean | undefined) => FileWatcher; createProgram: { (newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost | undefined, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; }; }' is not assignable to type 'WatchCompilerHostOfFilesAndCompilerOptions<BuilderProgram>'.
2+
Types of property 'createProgram' are incompatible.
3+
Type '{ (newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost | undefined, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; }' is not assignable to type 'CreateProgram<BuilderProgram>'.
4+
Types of parameters 'newProgram' and 'rootNames' are incompatible.
5+
Type 'ReadonlyArray<string> | undefined' is not assignable to type 'Program'.
6+
Type 'undefined' is not assignable to type 'Program'.
7+
8+
9+
==== tests/cases/compiler/APISample_WatchWithOwnWatchHost.ts (1 errors) ====
10+
/*
11+
* Note: This test is a public API sample. This sample verifies creating abstract builder to watch list of root files
12+
* Please log a "breaking change" issue for any API breaking change affecting this issue
13+
*/
14+
15+
declare var console: any;
16+
17+
import ts = require("typescript");
18+
19+
function watchMain() {
20+
// get list of files and compiler options somehow
21+
const files: string[] = [];
22+
const options: ts.CompilerOptions = {};
23+
24+
const host: ts.WatchCompilerHostOfFilesAndCompilerOptions<ts.BuilderProgram> = {
25+
~~~~
26+
!!! error TS2322: Type '{ rootFiles: string[]; options: CompilerOptions; useCaseSensitiveFileNames: () => boolean; getNewLine: () => string; getCurrentDirectory: () => string; getDefaultLibFileName: (options: CompilerOptions) => string; fileExists: (path: string) => boolean; readFile: (path: string, encoding?: string | undefined) => string | undefined; directoryExists: (path: string) => boolean; getDirectories: (path: string) => string[]; readDirectory: (path: string, extensions?: ReadonlyArray<string> | undefined, exclude?: ReadonlyArray<string> | undefined, include?: ReadonlyArray<string> | undefined, depth?: number | undefined) => string[]; realpath: ((path: string) => string) | undefined; watchFile: (path: string, callback: FileWatcherCallback, pollingInterval?: number | undefined) => FileWatcher; watchDirectory: (path: string, callback: DirectoryWatcherCallback, recursive?: boolean | undefined) => FileWatcher; createProgram: { (newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost | undefined, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; }; }' is not assignable to type 'WatchCompilerHostOfFilesAndCompilerOptions<BuilderProgram>'.
27+
!!! error TS2322: Types of property 'createProgram' are incompatible.
28+
!!! error TS2322: Type '{ (newProgram: Program, host: BuilderProgramHost, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; (rootNames: ReadonlyArray<string>, options: CompilerOptions, host?: CompilerHost | undefined, oldProgram?: BuilderProgram | undefined, configFileParsingDiagnostics?: ReadonlyArray<Diagnostic> | undefined): BuilderProgram; }' is not assignable to type 'CreateProgram<BuilderProgram>'.
29+
!!! error TS2322: Types of parameters 'newProgram' and 'rootNames' are incompatible.
30+
!!! error TS2322: Type 'ReadonlyArray<string> | undefined' is not assignable to type 'Program'.
31+
!!! error TS2322: Type 'undefined' is not assignable to type 'Program'.
32+
rootFiles: files,
33+
options,
34+
35+
useCaseSensitiveFileNames: () => ts.sys.useCaseSensitiveFileNames,
36+
getNewLine: () => ts.sys.newLine,
37+
getCurrentDirectory: ts.sys.getCurrentDirectory,
38+
getDefaultLibFileName: options => ts.getDefaultLibFilePath(options),
39+
40+
fileExists: ts.sys.fileExists,
41+
readFile: ts.sys.readFile,
42+
directoryExists: ts.sys.directoryExists,
43+
getDirectories: ts.sys.getDirectories,
44+
readDirectory: ts.sys.readDirectory,
45+
realpath: ts.sys.realpath,
46+
47+
watchFile: ts.sys.watchFile!,
48+
watchDirectory: ts.sys.watchDirectory!,
49+
createProgram: ts.createAbstractBuilder
50+
};
51+
52+
// You can technically override any given hook on the host, though you probably don't need to.
53+
// Note that we're assuming `origCreateProgram` and `origPostProgramCreate` doesn't use `this` at all.
54+
const origCreateProgram = host.createProgram;
55+
host.createProgram = (rootNames, options, host, oldProgram) => {
56+
console.log("** We're about to create the program! **");
57+
return origCreateProgram(rootNames, options, host, oldProgram);
58+
}
59+
const origPostProgramCreate = host.afterProgramCreate;
60+
61+
host.afterProgramCreate = program => {
62+
console.log("** We finished making the program! **");
63+
origPostProgramCreate!(program);
64+
};
65+
66+
// `createWatchProgram` creates an initial program, watches files, and updates the program over time.
67+
ts.createWatchProgram(host);
68+
}
69+
70+
watchMain();
71+

0 commit comments

Comments
 (0)