From ae65a41e2a49e4ff03b9de3177c622cef4e40a07 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Thu, 8 Sep 2016 14:05:04 -0700 Subject: [PATCH] A shorthand ambient module should be considered as possibly exporting a value. --- src/compiler/checker.ts | 10 +++++----- src/compiler/utilities.ts | 6 +++++- tests/baselines/reference/ambientShorthand_reExport.js | 4 ++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 565d01bf024..d08274247e7 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -1043,7 +1043,7 @@ namespace ts { const moduleSymbol = resolveExternalModuleName(node, (node.parent).moduleSpecifier); if (moduleSymbol) { - const exportDefaultSymbol = isShorthandAmbientModule(moduleSymbol.valueDeclaration) ? + const exportDefaultSymbol = isShorthandAmbientModuleSymbol(moduleSymbol) ? moduleSymbol : moduleSymbol.exports["export="] ? getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") : @@ -1119,7 +1119,7 @@ namespace ts { if (targetSymbol) { const name = specifier.propertyName || specifier.name; if (name.text) { - if (isShorthandAmbientModule(moduleSymbol.valueDeclaration)) { + if (isShorthandAmbientModuleSymbol(moduleSymbol)) { return moduleSymbol; } @@ -3381,7 +3381,7 @@ namespace ts { function getTypeOfFuncClassEnumModule(symbol: Symbol): Type { const links = getSymbolLinks(symbol); if (!links.type) { - if (symbol.valueDeclaration.kind === SyntaxKind.ModuleDeclaration && isShorthandAmbientModule(symbol.valueDeclaration)) { + if (symbol.valueDeclaration.kind === SyntaxKind.ModuleDeclaration && isShorthandAmbientModuleSymbol(symbol)) { links.type = anyType; } else { @@ -18365,8 +18365,8 @@ namespace ts { function moduleExportsSomeValue(moduleReferenceExpression: Expression): boolean { let moduleSymbol = resolveExternalModuleName(moduleReferenceExpression.parent, moduleReferenceExpression); - if (!moduleSymbol) { - // module not found - be conservative + if (!moduleSymbol || isShorthandAmbientModuleSymbol(moduleSymbol)) { + // If the module is not found or is shorthand, assume that it may export a value. return true; } diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 9ea74abbf76..e27a336c580 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -414,7 +414,11 @@ namespace ts { ((node).name.kind === SyntaxKind.StringLiteral || isGlobalScopeAugmentation(node)); } - export function isShorthandAmbientModule(node: Node): boolean { + export function isShorthandAmbientModuleSymbol(moduleSymbol: Symbol): boolean { + return isShorthandAmbientModule(moduleSymbol.valueDeclaration); + } + + function isShorthandAmbientModule(node: Node): boolean { // The only kind of module that can be missing a body is a shorthand ambient module. return node.kind === SyntaxKind.ModuleDeclaration && (!(node).body); } diff --git a/tests/baselines/reference/ambientShorthand_reExport.js b/tests/baselines/reference/ambientShorthand_reExport.js index d0433e5d478..bdfc0ee0821 100644 --- a/tests/baselines/reference/ambientShorthand_reExport.js +++ b/tests/baselines/reference/ambientShorthand_reExport.js @@ -22,6 +22,10 @@ var jquery_1 = require("jquery"); exports.x = jquery_1.x; //// [reExportAll.js] "use strict"; +function __export(m) { + for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p]; +} +__export(require("jquery")); //// [reExportUser.js] "use strict"; var reExportX_1 = require("./reExportX");