Skip to content

Commit 941d97c

Browse files
committed
Handle global augmentation in the module
1 parent 43c4478 commit 941d97c

File tree

2 files changed

+72
-35
lines changed

2 files changed

+72
-35
lines changed

src/compiler/builderState.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ namespace ts.BuilderState {
368368
}
369369

370370
// If this is non module emit, or its a global file, it depends on all the source files
371-
if (!state.referencedMap || (!isExternalModule(sourceFile) && !containsOnlyAmbientModules(sourceFile))) {
371+
if (!state.referencedMap || isFileAffectingGlobalScope(sourceFile)) {
372372
return getAllFileNames(state, programOfThisState);
373373
}
374374

@@ -430,6 +430,22 @@ namespace ts.BuilderState {
430430
return true;
431431
}
432432

433+
/**
434+
* Return true if file contains anything that augments to global scope we need to build them as if
435+
* they are global files as well as module
436+
*/
437+
function containsGlobalScopeAugmentation(sourceFile: SourceFile) {
438+
return some(sourceFile.moduleAugmentations, augmentation => isGlobalScopeAugmentation(augmentation.parent as ModuleDeclaration));
439+
}
440+
441+
/**
442+
* Return true if the file will invalidate all files because it affectes global scope
443+
*/
444+
function isFileAffectingGlobalScope(sourceFile: SourceFile) {
445+
return containsGlobalScopeAugmentation(sourceFile) ||
446+
!isExternalModule(sourceFile) && !containsOnlyAmbientModules(sourceFile);
447+
}
448+
433449
/**
434450
* Gets all files of the program excluding the default library file
435451
*/
@@ -473,7 +489,7 @@ namespace ts.BuilderState {
473489
* When program emits modular code, gets the files affected by the sourceFile whose shape has changed
474490
*/
475491
function getFilesAffectedByUpdatedShapeWhenModuleEmit(state: BuilderState, programOfThisState: Program, sourceFileWithUpdatedShape: SourceFile, cacheToUpdateSignature: Map<string>, cancellationToken: CancellationToken | undefined, computeHash: ComputeHash | undefined, exportedModulesMapCache: ComputingExportedModulesMap | undefined) {
476-
if (!isExternalModule(sourceFileWithUpdatedShape) && !containsOnlyAmbientModules(sourceFileWithUpdatedShape)) {
492+
if (isFileAffectingGlobalScope(sourceFileWithUpdatedShape)) {
477493
return getAllFilesExcludingDefaultLibraryFile(state, programOfThisState, sourceFileWithUpdatedShape);
478494
}
479495

src/testRunner/unittests/tscWatchMode.ts

Lines changed: 54 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,15 +1565,13 @@ export class Data2 {
15651565
});
15661566
});
15671567

1568-
describe("updates errors in lib file when non module file changes", () => {
1568+
describe("updates errors in lib file", () => {
15691569
const currentDirectory = "/user/username/projects/myproject";
15701570
const field = "fullscreen";
1571-
const aFile: File = {
1572-
path: `${currentDirectory}/a.ts`,
1573-
content: `interface Document {
1571+
const fieldWithoutReadonly = `interface Document {
15741572
${field}: boolean;
1575-
}`
1576-
};
1573+
}`;
1574+
15771575
const libFileWithDocument: File = {
15781576
path: libFile.path,
15791577
content: `${libFile.content}
@@ -1586,40 +1584,63 @@ interface Document {
15861584
return getDiagnosticOfFileFromProgram(program, file.path, file.content.indexOf(field), field.length, Diagnostics.All_declarations_of_0_must_have_identical_modifiers, field);
15871585
}
15881586

1589-
const files = [aFile, libFileWithDocument];
1587+
function verifyLibFileErrorsWith(aFile: File) {
1588+
const files = [aFile, libFileWithDocument];
15901589

1591-
function verifyLibErrors(options: CompilerOptions) {
1592-
const host = createWatchedSystem(files, { currentDirectory });
1593-
const watch = createWatchOfFilesAndCompilerOptions([aFile.path], host, options);
1594-
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1595-
checkOutputErrorsInitial(host, getErrors());
1590+
function verifyLibErrors(options: CompilerOptions) {
1591+
const host = createWatchedSystem(files, { currentDirectory });
1592+
const watch = createWatchOfFilesAndCompilerOptions([aFile.path], host, options);
1593+
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1594+
checkOutputErrorsInitial(host, getErrors());
15961595

1597-
host.writeFile(aFile.path, "var x = 10;");
1598-
host.runQueuedTimeoutCallbacks();
1599-
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1600-
checkOutputErrorsIncremental(host, emptyArray);
1596+
host.writeFile(aFile.path, aFile.content.replace(fieldWithoutReadonly, "var x: string;"));
1597+
host.runQueuedTimeoutCallbacks();
1598+
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1599+
checkOutputErrorsIncremental(host, emptyArray);
16011600

1602-
host.writeFile(aFile.path, aFile.content);
1603-
host.runQueuedTimeoutCallbacks();
1604-
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1605-
checkOutputErrorsIncremental(host, getErrors());
1606-
1607-
function getErrors() {
1608-
return [
1609-
...(options.skipLibCheck || options.skipDefaultLibCheck ? [] : [getDiagnostic(watch(), libFileWithDocument)]),
1610-
getDiagnostic(watch(), aFile)
1611-
];
1601+
host.writeFile(aFile.path, aFile.content);
1602+
host.runQueuedTimeoutCallbacks();
1603+
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1604+
checkOutputErrorsIncremental(host, getErrors());
1605+
1606+
function getErrors() {
1607+
return [
1608+
...(options.skipLibCheck || options.skipDefaultLibCheck ? [] : [getDiagnostic(watch(), libFileWithDocument)]),
1609+
getDiagnostic(watch(), aFile)
1610+
];
1611+
}
16121612
}
1613+
1614+
it("with default options", () => {
1615+
verifyLibErrors({});
1616+
});
1617+
it("with skipLibCheck", () => {
1618+
verifyLibErrors({ skipLibCheck: true });
1619+
});
1620+
it("with skipDefaultLibCheck", () => {
1621+
verifyLibErrors({ skipDefaultLibCheck: true });
1622+
});
16131623
}
16141624

1615-
it("with default options", () => {
1616-
verifyLibErrors({});
1617-
});
1618-
it("with skipLibCheck", () => {
1619-
verifyLibErrors({ skipLibCheck: true });
1625+
describe("when non module file changes", () => {
1626+
const aFile: File = {
1627+
path: `${currentDirectory}/a.ts`,
1628+
content: `${fieldWithoutReadonly}
1629+
var y: number;`
1630+
};
1631+
verifyLibFileErrorsWith(aFile);
16201632
});
1621-
it("with skipDefaultLibCheck", () => {
1622-
verifyLibErrors({ skipDefaultLibCheck: true });
1633+
1634+
describe("when module file with global definitions changes", () => {
1635+
const aFile: File = {
1636+
path: `${currentDirectory}/a.ts`,
1637+
content: `export {}
1638+
declare global {
1639+
${fieldWithoutReadonly}
1640+
var y: number;
1641+
}`
1642+
};
1643+
verifyLibFileErrorsWith(aFile);
16231644
});
16241645
});
16251646

0 commit comments

Comments
 (0)