@@ -498,7 +498,7 @@ namespace ts {
498498 const moduleNotFoundError = !isInAmbientContext(moduleName.parent.parent)
499499 ? Diagnostics.Invalid_module_name_in_augmentation_module_0_cannot_be_found
500500 : undefined;
501- let mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError);
501+ let mainModule = resolveExternalModuleNameWorker(moduleName, moduleName, moduleNotFoundError, /*isForAugmentation*/ true );
502502 if (!mainModule) {
503503 return;
504504 }
@@ -1074,7 +1074,7 @@ namespace ts {
10741074 const moduleSymbol = resolveExternalModuleName(node, (<ImportDeclaration>node.parent).moduleSpecifier);
10751075
10761076 if (moduleSymbol) {
1077- const exportDefaultSymbol = isUntypedModuleSymbol (moduleSymbol) ?
1077+ const exportDefaultSymbol = isShorthandAmbientModuleSymbol (moduleSymbol) ?
10781078 moduleSymbol :
10791079 moduleSymbol.exports["export="] ?
10801080 getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") :
@@ -1150,7 +1150,7 @@ namespace ts {
11501150 if (targetSymbol) {
11511151 const name = specifier.propertyName || specifier.name;
11521152 if (name.text) {
1153- if (isUntypedModuleSymbol (moduleSymbol)) {
1153+ if (isShorthandAmbientModuleSymbol (moduleSymbol)) {
11541154 return moduleSymbol;
11551155 }
11561156
@@ -1351,16 +1351,16 @@ namespace ts {
13511351 return resolveExternalModuleNameWorker(location, moduleReferenceExpression, Diagnostics.Cannot_find_module_0);
13521352 }
13531353
1354- function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage): Symbol {
1354+ function resolveExternalModuleNameWorker(location: Node, moduleReferenceExpression: Expression, moduleNotFoundError: DiagnosticMessage, isForAugmentation = false ): Symbol {
13551355 if (moduleReferenceExpression.kind !== SyntaxKind.StringLiteral) {
13561356 return;
13571357 }
13581358
13591359 const moduleReferenceLiteral = <LiteralExpression>moduleReferenceExpression;
1360- return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral);
1360+ return resolveExternalModule(location, moduleReferenceLiteral.text, moduleNotFoundError, moduleReferenceLiteral, isForAugmentation );
13611361 }
13621362
1363- function resolveExternalModule(location: Node, moduleReference: string, moduleNotFoundError: DiagnosticMessage, errorNode: Node): Symbol {
1363+ function resolveExternalModule(location: Node, moduleReference: string, moduleNotFoundError: DiagnosticMessage, errorNode: Node, isForAugmentation = false ): Symbol {
13641364 // Module names are escaped in our symbol table. However, string literal values aren't.
13651365 // Escape the name in the "require(...)" clause to ensure we find the right symbol.
13661366 const moduleName = escapeIdentifier(moduleReference);
@@ -1403,24 +1403,19 @@ namespace ts {
14031403
14041404 // May be an untyped module. If so, ignore resolutionDiagnostic.
14051405 if (!isRelative && resolvedModule && !extensionIsTypeScript(resolvedModule.extension)) {
1406- if (compilerOptions.noImplicitAny) {
1407- if (moduleNotFoundError) {
1408- error(errorNode,
1409- Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
1410- moduleReference,
1411- resolvedModule.resolvedFileName);
1412- }
1413- return undefined;
1414- }
1415-
1416- // Create a new symbol to represent the untyped module and store it in globals.
1417- // This provides a name to the module. See the test tests/cases/fourslash/untypedModuleImport.ts
1418- const newSymbol = createSymbol(SymbolFlags.ValueModule, quotedName);
1419- // Module symbols are expected to have 'exports', although since this is an untyped module it can be empty.
1420- newSymbol.exports = createMap<Symbol>();
1421- // Cache it so subsequent accesses will return the same module.
1422- globals[quotedName] = newSymbol;
1423- return newSymbol;
1406+ if (isForAugmentation) {
1407+ Debug.assert(!!moduleNotFoundError);
1408+ const diag = Diagnostics.Invalid_module_name_in_augmentation_Module_0_resolves_to_an_untyped_module_at_1_which_cannot_be_augmented;
1409+ error(errorNode, diag, moduleName, resolvedModule.resolvedFileName);
1410+ }
1411+ else if (compilerOptions.noImplicitAny && moduleNotFoundError) {
1412+ error(errorNode,
1413+ Diagnostics.Could_not_find_a_declaration_file_for_module_0_1_implicitly_has_an_any_type,
1414+ moduleReference,
1415+ resolvedModule.resolvedFileName);
1416+ }
1417+ // Failed imports and untyped modules are both treated in an untyped manner; only difference is whether we give a diagnostic first.
1418+ return undefined;
14241419 }
14251420
14261421 if (moduleNotFoundError) {
@@ -3490,7 +3485,7 @@ namespace ts {
34903485 function getTypeOfFuncClassEnumModule(symbol: Symbol): Type {
34913486 const links = getSymbolLinks(symbol);
34923487 if (!links.type) {
3493- if (symbol.flags & SymbolFlags.Module && isUntypedModuleSymbol (symbol)) {
3488+ if (symbol.flags & SymbolFlags.Module && isShorthandAmbientModuleSymbol (symbol)) {
34943489 links.type = anyType;
34953490 }
34963491 else {
@@ -19063,7 +19058,7 @@ namespace ts {
1906319058
1906419059 function moduleExportsSomeValue(moduleReferenceExpression: Expression): boolean {
1906519060 let moduleSymbol = resolveExternalModuleName(moduleReferenceExpression.parent, moduleReferenceExpression);
19066- if (!moduleSymbol || isUntypedModuleSymbol (moduleSymbol)) {
19061+ if (!moduleSymbol || isShorthandAmbientModuleSymbol (moduleSymbol)) {
1906719062 // If the module is not found or is shorthand, assume that it may export a value.
1906819063 return true;
1906919064 }
0 commit comments