Skip to content

Commit 7457e5d

Browse files
committed
Pull out the incremental compilation into a function so we can test it
1 parent 021444a commit 7457e5d

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

src/compiler/watch.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,15 @@ namespace ts {
392392
return host;
393393
}
394394

395-
export interface IncrementalProgramOptions<T extends BuilderProgram> {
395+
interface IncrementalProgramOptions<T extends BuilderProgram> {
396396
rootNames: ReadonlyArray<string>;
397397
options: CompilerOptions;
398398
configFileParsingDiagnostics?: ReadonlyArray<Diagnostic>;
399399
projectReferences?: ReadonlyArray<ProjectReference>;
400400
host?: CompilerHost;
401401
createProgram?: CreateProgram<T>;
402402
}
403-
export function createIncrementalProgram<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({
403+
function createIncrementalProgram<T extends BuilderProgram = EmitAndSemanticDiagnosticsBuilderProgram>({
404404
rootNames, options, configFileParsingDiagnostics, projectReferences, host, createProgram
405405
}: IncrementalProgramOptions<T>): T {
406406
host = host || createIncrementalCompilerHost(options);
@@ -427,11 +427,11 @@ namespace ts {
427427
const exitStatus = emitFilesAndReportErrors(
428428
builderProgram,
429429
input.reportDiagnostic || createDiagnosticReporter(system),
430-
s => host!.trace && host!.trace!(s),
430+
s => host.trace && host.trace(s),
431431
input.reportErrorSummary || input.options.pretty ? errorCount => system.write(getErrorSummaryText(errorCount, system.newLine)) : undefined
432432
);
433433
if (input.afterProgramEmitAndDiagnostics) input.afterProgramEmitAndDiagnostics(builderProgram);
434-
return exitStatus;
434+
return exitStatus;
435435
}
436436
}
437437

src/testRunner/unittests/tscWatch/helpers.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,23 @@ namespace ts.tscWatch {
5757
function checkOutputErrors(
5858
host: WatchedSystem,
5959
logsBeforeWatchDiagnostic: string[] | undefined,
60-
preErrorsWatchDiagnostic: Diagnostic,
60+
preErrorsWatchDiagnostic: Diagnostic | undefined,
6161
logsBeforeErrors: string[] | undefined,
6262
errors: ReadonlyArray<Diagnostic> | ReadonlyArray<string>,
6363
disableConsoleClears?: boolean | undefined,
64-
...postErrorsWatchDiagnostics: Diagnostic[]
64+
...postErrorsWatchDiagnostics: Diagnostic[] | string[]
6565
) {
6666
let screenClears = 0;
6767
const outputs = host.getOutput();
68-
const expectedOutputCount = 1 + errors.length + postErrorsWatchDiagnostics.length +
69-
(logsBeforeWatchDiagnostic ? logsBeforeWatchDiagnostic.length : 0) + (logsBeforeErrors ? logsBeforeErrors.length : 0);
68+
const expectedOutputCount = (preErrorsWatchDiagnostic ? 1 : 0) +
69+
errors.length +
70+
postErrorsWatchDiagnostics.length +
71+
(logsBeforeWatchDiagnostic ? logsBeforeWatchDiagnostic.length : 0) +
72+
(logsBeforeErrors ? logsBeforeErrors.length : 0);
7073
assert.equal(outputs.length, expectedOutputCount, JSON.stringify(outputs));
7174
let index = 0;
7275
forEach(logsBeforeWatchDiagnostic, log => assertLog("logsBeforeWatchDiagnostic", log));
73-
assertWatchDiagnostic(preErrorsWatchDiagnostic);
76+
if (preErrorsWatchDiagnostic) assertWatchDiagnostic(preErrorsWatchDiagnostic);
7477
forEach(logsBeforeErrors, log => assertLog("logBeforeError", log));
7578
// Verify errors
7679
forEach(errors, assertDiagnostic);
@@ -98,13 +101,18 @@ namespace ts.tscWatch {
98101
index++;
99102
}
100103

101-
function assertWatchDiagnostic(diagnostic: Diagnostic) {
102-
const expected = getWatchDiagnosticWithoutDate(diagnostic);
103-
if (!disableConsoleClears && contains(screenStartingMessageCodes, diagnostic.code)) {
104-
assert.equal(host.screenClears[screenClears], index, `Expected screen clear at this diagnostic: ${expected}`);
105-
screenClears++;
104+
function assertWatchDiagnostic(diagnostic: Diagnostic | string) {
105+
if (isString(diagnostic)) {
106+
assert.equal(outputs[index], diagnostic, getOutputAtFailedMessage("Diagnostic", diagnostic));
107+
}
108+
else {
109+
const expected = getWatchDiagnosticWithoutDate(diagnostic);
110+
if (!disableConsoleClears && contains(screenStartingMessageCodes, diagnostic.code)) {
111+
assert.equal(host.screenClears[screenClears], index, `Expected screen clear at this diagnostic: ${expected}`);
112+
screenClears++;
113+
}
114+
assert.isTrue(endsWith(outputs[index], expected), getOutputAtFailedMessage("Watch diagnostic", expected));
106115
}
107-
assert.isTrue(endsWith(outputs[index], expected), getOutputAtFailedMessage("Watch diagnostic", expected));
108116
index++;
109117
}
110118

@@ -159,6 +167,18 @@ namespace ts.tscWatch {
159167
assert.equal(host.exitCode, expectedExitCode);
160168
}
161169

170+
export function checkNormalBuildErrors(host: WatchedSystem, errors: ReadonlyArray<Diagnostic> | ReadonlyArray<string>, reportErrorSummary?: boolean) {
171+
checkOutputErrors(
172+
host,
173+
/*logsBeforeWatchDiagnostic*/ undefined,
174+
/*preErrorsWatchDiagnostic*/ undefined,
175+
/*logsBeforeErrors*/ undefined,
176+
errors,
177+
/*disableConsoleClears*/ undefined,
178+
...(reportErrorSummary ? [getErrorSummaryText(errors.length, host.newLine)] : emptyArray)
179+
);
180+
}
181+
162182
function isDiagnosticMessageChain(message: DiagnosticMessage | DiagnosticMessageChain): message is DiagnosticMessageChain {
163183
return !!(message as DiagnosticMessageChain).messageText;
164184
}

src/testRunner/unittests/tscWatch/incremental.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,28 @@ namespace ts.tscWatch {
2626
it("with tsc", () => {
2727
verifyIncrementalWatchEmitWorker({
2828
input,
29-
emitAndReportErrors: ,
30-
verifyErrors
29+
emitAndReportErrors: incrementalBuild,
30+
verifyErrors: checkNormalBuildErrors
3131
});
3232
});
3333
}
3434

35+
function incrementalBuild(configFile: string, host: WatchedSystem) {
36+
const reportDiagnostic = createDiagnosticReporter(host);
37+
const config = parseConfigFileWithSystem(configFile, {}, host, reportDiagnostic);
38+
if (config) {
39+
performIncrementalCompilation({
40+
rootNames: config.fileNames,
41+
options: config.options,
42+
projectReferences: config.projectReferences,
43+
configFileParsingDiagnostics: getConfigFileParsingDiagnostics(config),
44+
reportDiagnostic,
45+
system: host
46+
});
47+
}
48+
return { close: noop };
49+
}
50+
3551
interface VerifyIncrementalWatchEmitWorkerInput {
3652
input: VerifyIncrementalWatchEmitInput;
3753
emitAndReportErrors: (configFile: string, host: WatchedSystem) => { close(): void; };
@@ -83,10 +99,10 @@ namespace ts.tscWatch {
8399
}
84100
function verifyBuild({ host, writtenFiles, emitAndReportErrors, verifyErrors, expectedEmit, expectedErrors }: VerifyBuildWorker) {
85101
writtenFiles.clear();
86-
const watch = emitAndReportErrors("tsconfig.json", host);
102+
const result = emitAndReportErrors("tsconfig.json", host);
87103
checkFileEmit(writtenFiles, expectedEmit);
88104
verifyErrors(host, expectedErrors);
89-
watch.close();
105+
result.close();
90106
}
91107

92108
function checkFileEmit(actual: Map<string>, expected: ReadonlyArray<File>) {

src/tsc/tsc.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,15 @@ namespace ts {
261261
function performIncrementalCompilation(config: ParsedCommandLine) {
262262
const { options, fileNames, projectReferences } = config;
263263
enableStatistics(options);
264-
const exitStatus = ts.performIncrementalCompilation({
264+
return sys.exit(ts.performIncrementalCompilation({
265265
rootNames: fileNames,
266266
options,
267267
configFileParsingDiagnostics: getConfigFileParsingDiagnostics(config),
268268
projectReferences,
269269
reportDiagnostic,
270270
reportErrorSummary: createReportErrorSummary(options),
271271
afterProgramEmitAndDiagnostics: builderProgram => reportStatistics(builderProgram.getProgram())
272-
});
273-
return sys.exit(exitStatus);
272+
}));
274273
}
275274

276275
function updateCreateProgram<T extends BuilderProgram>(host: { createProgram: CreateProgram<T>; }) {

0 commit comments

Comments
 (0)