mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-06-17 12:19:32 -05:00
Consider module augmentations in files referenced by imports in watch/incremental (#45156)
* Consider module augmentations in files referenced by importsin watch/incremental * Accept baselines * Accept other baseline * Hooray optional chaining * Delete outdated and unuseful comment
This commit is contained in:
@@ -178,22 +178,16 @@ namespace ts {
|
||||
*/
|
||||
export type ComputeHash = ((data: string) => string) | undefined;
|
||||
|
||||
/**
|
||||
* Get the referencedFile from the imported module symbol
|
||||
*/
|
||||
function getReferencedFileFromImportedModuleSymbol(symbol: Symbol) {
|
||||
if (symbol.declarations && symbol.declarations[0]) {
|
||||
const declarationSourceFile = getSourceFileOfNode(symbol.declarations[0]);
|
||||
return declarationSourceFile && declarationSourceFile.resolvedPath;
|
||||
}
|
||||
function getReferencedFilesFromImportedModuleSymbol(symbol: Symbol): Path[] {
|
||||
return mapDefined(symbol.declarations, declaration => getSourceFileOfNode(declaration)?.resolvedPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the referencedFile from the import name node from file
|
||||
* Get the module source file and all augmenting files from the import name node from file
|
||||
*/
|
||||
function getReferencedFileFromImportLiteral(checker: TypeChecker, importName: StringLiteralLike) {
|
||||
function getReferencedFilesFromImportLiteral(checker: TypeChecker, importName: StringLiteralLike): Path[] | undefined {
|
||||
const symbol = checker.getSymbolAtLocation(importName);
|
||||
return symbol && getReferencedFileFromImportedModuleSymbol(symbol);
|
||||
return symbol && getReferencedFilesFromImportedModuleSymbol(symbol);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -215,10 +209,8 @@ namespace ts {
|
||||
if (sourceFile.imports && sourceFile.imports.length > 0) {
|
||||
const checker: TypeChecker = program.getTypeChecker();
|
||||
for (const importName of sourceFile.imports) {
|
||||
const declarationSourceFilePath = getReferencedFileFromImportLiteral(checker, importName);
|
||||
if (declarationSourceFilePath) {
|
||||
addReferencedFile(declarationSourceFilePath);
|
||||
}
|
||||
const declarationSourceFilePaths = getReferencedFilesFromImportLiteral(checker, importName);
|
||||
declarationSourceFilePaths?.forEach(addReferencedFile);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -458,7 +450,7 @@ namespace ts {
|
||||
}
|
||||
|
||||
let exportedModules: Set<Path> | undefined;
|
||||
exportedModulesFromDeclarationEmit.forEach(symbol => addExportedModule(getReferencedFileFromImportedModuleSymbol(symbol)));
|
||||
exportedModulesFromDeclarationEmit.forEach(symbol => addExportedModule(getReferencedFilesFromImportedModuleSymbol(symbol)));
|
||||
if (exportedModules) {
|
||||
exportedModulesMapCache.set(sourceFile.resolvedPath, exportedModules);
|
||||
}
|
||||
@@ -466,12 +458,12 @@ namespace ts {
|
||||
exportedModulesMapCache.deleteKey(sourceFile.resolvedPath);
|
||||
}
|
||||
|
||||
function addExportedModule(exportedModulePath: Path | undefined) {
|
||||
if (exportedModulePath) {
|
||||
function addExportedModule(exportedModulePaths: Path[] | undefined) {
|
||||
if (exportedModulePaths?.length) {
|
||||
if (!exportedModules) {
|
||||
exportedModules = new Set();
|
||||
}
|
||||
exportedModules.add(exportedModulePath);
|
||||
exportedModulePaths.forEach(path => exportedModules!.add(path));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -355,5 +355,22 @@ export const Fragment: unique symbol;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("editing module augmentation", () => {
|
||||
verifyIncrementalWatchEmit({
|
||||
subScenario: "editing module augmentation",
|
||||
files: () => [
|
||||
{ path: libFile.path, content: libContent },
|
||||
{ path: `${project}/node_modules/classnames/index.d.ts`, content: `export interface Result {} export default function classNames(): Result;` },
|
||||
{ path: `${project}/src/types/classnames.d.ts`, content: `export {}; declare module "classnames" { interface Result { foo } }` },
|
||||
{ path: `${project}/src/index.ts`, content: `import classNames from "classnames"; classNames().foo;` },
|
||||
{ path: configFile.path, content: JSON.stringify({ compilerOptions: { module: "commonjs", incremental: true } }) },
|
||||
],
|
||||
modifyFs: host => {
|
||||
// delete 'foo'
|
||||
host.writeFile(`${project}/src/types/classnames.d.ts`, `export {}; declare module "classnames" { interface Result {} }`);
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user