Skip to content

Commit 03a3978

Browse files
committed
Use ambient modules as references to keep track of reporting and usage of modules correctly
Fixes #27585
1 parent ca840ee commit 03a3978

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

src/compiler/builderState.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,39 @@ namespace ts.BuilderState {
148148
});
149149
}
150150

151+
// Add module augmentation as references
152+
if (sourceFile.moduleAugmentations.length) {
153+
const checker = program.getTypeChecker();
154+
for (const moduleName of sourceFile.moduleAugmentations) {
155+
if (!isStringLiteral(moduleName)) { continue; }
156+
const symbol = checker.getSymbolAtLocation(moduleName);
157+
if (!symbol) { continue; }
158+
159+
// Add any file other than our own as reference
160+
addReferenceFromAmbientModule(symbol);
161+
}
162+
}
163+
164+
// From ambient modules
165+
for (const ambientModule of program.getTypeChecker().getAmbientModules()) {
166+
if (ambientModule.declarations.length > 1) {
167+
addReferenceFromAmbientModule(ambientModule);
168+
}
169+
}
170+
151171
return referencedFiles;
152172

173+
function addReferenceFromAmbientModule(symbol: Symbol) {
174+
// Add any file other than our own as reference
175+
for (const declaration of symbol.declarations) {
176+
const declarationSourceFile = getSourceFileOfNode(declaration);
177+
if (declarationSourceFile &&
178+
declarationSourceFile !== sourceFile) {
179+
addReferencedFile(declarationSourceFile.resolvedPath);
180+
}
181+
}
182+
}
183+
153184
function addReferencedFile(referencedPath: Path) {
154185
if (!referencedFiles) {
155186
referencedFiles = createMap<true>();

src/testRunner/unittests/tscWatchMode.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,6 +1386,41 @@ foo().hello`
13861386
// File a need not be rewritten
13871387
assert.equal(host.getModifiedTime(`${currentDirectory}/a.js`), modifiedTimeOfAJs);
13881388
});
1389+
1390+
it("updates errors when ambient modules of program changes", () => {
1391+
const currentDirectory = "/user/username/projects/myproject";
1392+
const aFile: File = {
1393+
path: `${currentDirectory}/a.ts`,
1394+
content: `declare module 'a' {
1395+
type foo = number;
1396+
}`
1397+
};
1398+
const config: File = {
1399+
path: `${currentDirectory}/tsconfig.json`,
1400+
content: "{}"
1401+
};
1402+
const files = [aFile, config, libFile];
1403+
const host = createWatchedSystem(files, { currentDirectory });
1404+
const watch = createWatchOfConfigFile("tsconfig.json", host);
1405+
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1406+
checkOutputErrorsInitial(host, emptyArray);
1407+
1408+
// Create bts with same file contents
1409+
const bTsPath = `${currentDirectory}/b.ts`;
1410+
host.writeFile(bTsPath, aFile.content);
1411+
host.runQueuedTimeoutCallbacks();
1412+
checkProgramActualFiles(watch(), [aFile.path, "b.ts", libFile.path]);
1413+
checkOutputErrorsIncremental(host, [
1414+
"a.ts(2,8): error TS2300: Duplicate identifier 'foo'.\n",
1415+
"b.ts(2,8): error TS2300: Duplicate identifier 'foo'.\n"
1416+
]);
1417+
1418+
// Delete bTs
1419+
host.deleteFile(bTsPath);
1420+
host.runQueuedTimeoutCallbacks();
1421+
checkProgramActualFiles(watch(), [aFile.path, libFile.path]);
1422+
checkOutputErrorsIncremental(host, emptyArray);
1423+
});
13891424
});
13901425

13911426
describe("tsc-watch emit with outFile or out setting", () => {

0 commit comments

Comments
 (0)