From c3e090695ec59cd79536ea892f4351fbb3674489 Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Wed, 6 Sep 2017 22:07:30 -0700 Subject: [PATCH] Do not consider UMD alias symbols as visible within external modules (#18049) * Do not consider UMD alias symbols as visible within external modules in the symbol writer * Minimal repro --- src/compiler/checker.ts | 5 ++++ .../reference/exportAsNamespace.d.types | 2 +- ...mportShouldNotBeElidedInDeclarationEmit.js | 26 +++++++++++++++++++ ...ShouldNotBeElidedInDeclarationEmit.symbols | 23 ++++++++++++++++ ...rtShouldNotBeElidedInDeclarationEmit.types | 24 +++++++++++++++++ .../reference/umd-augmentation-1.types | 2 +- .../reference/umd-augmentation-2.types | 2 +- .../reference/umd-augmentation-3.symbols | 2 +- .../reference/umd-augmentation-3.types | 4 +-- .../reference/umd-augmentation-4.symbols | 2 +- .../reference/umd-augmentation-4.types | 4 +-- tests/baselines/reference/umd1.types | 2 +- tests/baselines/reference/umd3.types | 2 +- tests/baselines/reference/umd4.types | 2 +- .../reference/umdGlobalConflict.types | 2 +- ...mportShouldNotBeElidedInDeclarationEmit.ts | 12 +++++++++ 16 files changed, 103 insertions(+), 13 deletions(-) create mode 100644 tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.js create mode 100644 tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.symbols create mode 100644 tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.types create mode 100644 tests/cases/compiler/importShouldNotBeElidedInDeclarationEmit.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 9fcd0b593f8..e7e66ed5f2d 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2095,6 +2095,10 @@ namespace ts { canQualifySymbol(symbolFromSymbolTable, meaning); } + function isUMDExportSymbol(symbol: Symbol) { + return symbol && symbol.declarations && symbol.declarations[0] && isNamespaceExportDeclaration(symbol.declarations[0]); + } + function trySymbolTable(symbols: SymbolTable) { // If symbol is directly available by its name in the symbol table if (isAccessible(symbols.get(symbol.escapedName))) { @@ -2106,6 +2110,7 @@ namespace ts { if (symbolFromSymbolTable.flags & SymbolFlags.Alias && symbolFromSymbolTable.escapedName !== "export=" && !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier) + && !(isUMDExportSymbol(symbolFromSymbolTable) && isExternalModule(getSourceFileOfNode(enclosingDeclaration))) // If `!useOnlyExternalAliasing`, we can use any type of alias to get the name && (!useOnlyExternalAliasing || some(symbolFromSymbolTable.declarations, isExternalModuleImportEqualsDeclaration))) { diff --git a/tests/baselines/reference/exportAsNamespace.d.types b/tests/baselines/reference/exportAsNamespace.d.types index 706857bf8ed..4952cb8863b 100644 --- a/tests/baselines/reference/exportAsNamespace.d.types +++ b/tests/baselines/reference/exportAsNamespace.d.types @@ -5,5 +5,5 @@ export var X; >X : any export as namespace N ->N : typeof N +>N : typeof "tests/cases/compiler/exportAsNamespace" diff --git a/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.js b/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.js new file mode 100644 index 00000000000..a1316dbdd46 --- /dev/null +++ b/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.js @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/importShouldNotBeElidedInDeclarationEmit.ts] //// + +//// [umd.d.ts] +export as namespace UMD; + +export type Thing = { + a: number; +} + +export declare function makeThing(): Thing; +//// [index.ts] +import { makeThing } from "umd"; +export const thing = makeThing(); + + +//// [index.js] +"use strict"; +exports.__esModule = true; +var umd_1 = require("umd"); +exports.thing = umd_1.makeThing(); + + +//// [index.d.ts] +export declare const thing: { + a: number; +}; diff --git a/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.symbols b/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.symbols new file mode 100644 index 00000000000..4ac0f4928b0 --- /dev/null +++ b/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.symbols @@ -0,0 +1,23 @@ +=== tests/cases/compiler/node_modules/umd.d.ts === +export as namespace UMD; +>UMD : Symbol(UMD, Decl(umd.d.ts, 0, 0)) + +export type Thing = { +>Thing : Symbol(Thing, Decl(umd.d.ts, 0, 24)) + + a: number; +>a : Symbol(a, Decl(umd.d.ts, 2, 21)) +} + +export declare function makeThing(): Thing; +>makeThing : Symbol(makeThing, Decl(umd.d.ts, 4, 1)) +>Thing : Symbol(Thing, Decl(umd.d.ts, 0, 24)) + +=== tests/cases/compiler/index.ts === +import { makeThing } from "umd"; +>makeThing : Symbol(makeThing, Decl(index.ts, 0, 8)) + +export const thing = makeThing(); +>thing : Symbol(thing, Decl(index.ts, 1, 12)) +>makeThing : Symbol(makeThing, Decl(index.ts, 0, 8)) + diff --git a/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.types b/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.types new file mode 100644 index 00000000000..2531654f80b --- /dev/null +++ b/tests/baselines/reference/importShouldNotBeElidedInDeclarationEmit.types @@ -0,0 +1,24 @@ +=== tests/cases/compiler/node_modules/umd.d.ts === +export as namespace UMD; +>UMD : typeof "tests/cases/compiler/node_modules/umd" + +export type Thing = { +>Thing : Thing + + a: number; +>a : number +} + +export declare function makeThing(): Thing; +>makeThing : () => Thing +>Thing : Thing + +=== tests/cases/compiler/index.ts === +import { makeThing } from "umd"; +>makeThing : () => { a: number; } + +export const thing = makeThing(); +>thing : { a: number; } +>makeThing() : { a: number; } +>makeThing : () => { a: number; } + diff --git a/tests/baselines/reference/umd-augmentation-1.types b/tests/baselines/reference/umd-augmentation-1.types index 5324e58f68e..9d89e0c4537 100644 --- a/tests/baselines/reference/umd-augmentation-1.types +++ b/tests/baselines/reference/umd-augmentation-1.types @@ -47,7 +47,7 @@ var t = p.x; === tests/cases/conformance/externalModules/node_modules/math2d/index.d.ts === export as namespace Math2d; ->Math2d : typeof Math2d +>Math2d : typeof "tests/cases/conformance/externalModules/node_modules/math2d/index" export interface Point { >Point : Point diff --git a/tests/baselines/reference/umd-augmentation-2.types b/tests/baselines/reference/umd-augmentation-2.types index 4ead8d611ca..b8d04ecd643 100644 --- a/tests/baselines/reference/umd-augmentation-2.types +++ b/tests/baselines/reference/umd-augmentation-2.types @@ -45,7 +45,7 @@ var t = p.x; === tests/cases/conformance/externalModules/node_modules/math2d/index.d.ts === export as namespace Math2d; ->Math2d : typeof Math2d +>Math2d : typeof "tests/cases/conformance/externalModules/node_modules/math2d/index" export interface Point { >Point : Point diff --git a/tests/baselines/reference/umd-augmentation-3.symbols b/tests/baselines/reference/umd-augmentation-3.symbols index acb2f471faf..7cbe3ac803b 100644 --- a/tests/baselines/reference/umd-augmentation-3.symbols +++ b/tests/baselines/reference/umd-augmentation-3.symbols @@ -44,7 +44,7 @@ export = M2D; >M2D : Symbol(M2D, Decl(index.d.ts, 2, 13)) declare namespace M2D { ->M2D : Symbol(Math2d, Decl(index.d.ts, 2, 13), Decl(math2d-augment.d.ts, 0, 33)) +>M2D : Symbol(M2D, Decl(index.d.ts, 2, 13), Decl(math2d-augment.d.ts, 0, 33)) interface Point { >Point : Symbol(Point, Decl(index.d.ts, 4, 23)) diff --git a/tests/baselines/reference/umd-augmentation-3.types b/tests/baselines/reference/umd-augmentation-3.types index 4802d159e18..5efafd780a0 100644 --- a/tests/baselines/reference/umd-augmentation-3.types +++ b/tests/baselines/reference/umd-augmentation-3.types @@ -47,13 +47,13 @@ var t = p.x; === tests/cases/conformance/externalModules/node_modules/math2d/index.d.ts === export as namespace Math2d; ->Math2d : typeof Math2d +>Math2d : typeof M2D export = M2D; >M2D : typeof M2D declare namespace M2D { ->M2D : typeof Math2d +>M2D : typeof M2D interface Point { >Point : Point diff --git a/tests/baselines/reference/umd-augmentation-4.symbols b/tests/baselines/reference/umd-augmentation-4.symbols index 12696ab51f7..eabb2e15898 100644 --- a/tests/baselines/reference/umd-augmentation-4.symbols +++ b/tests/baselines/reference/umd-augmentation-4.symbols @@ -42,7 +42,7 @@ export = M2D; >M2D : Symbol(M2D, Decl(index.d.ts, 2, 13)) declare namespace M2D { ->M2D : Symbol(Math2d, Decl(index.d.ts, 2, 13), Decl(math2d-augment.d.ts, 0, 33)) +>M2D : Symbol(M2D, Decl(index.d.ts, 2, 13), Decl(math2d-augment.d.ts, 0, 33)) interface Point { >Point : Symbol(Point, Decl(index.d.ts, 4, 23)) diff --git a/tests/baselines/reference/umd-augmentation-4.types b/tests/baselines/reference/umd-augmentation-4.types index 324de384183..f71928f5afc 100644 --- a/tests/baselines/reference/umd-augmentation-4.types +++ b/tests/baselines/reference/umd-augmentation-4.types @@ -45,13 +45,13 @@ var t = p.x; === tests/cases/conformance/externalModules/node_modules/math2d/index.d.ts === export as namespace Math2d; ->Math2d : typeof Math2d +>Math2d : typeof M2D export = M2D; >M2D : typeof M2D declare namespace M2D { ->M2D : typeof Math2d +>M2D : typeof M2D interface Point { >Point : Point diff --git a/tests/baselines/reference/umd1.types b/tests/baselines/reference/umd1.types index 9ccc0d2cd8e..b84aaf3ad70 100644 --- a/tests/baselines/reference/umd1.types +++ b/tests/baselines/reference/umd1.types @@ -30,5 +30,5 @@ export interface Thing { n: typeof x } >x : number export as namespace Foo; ->Foo : typeof Foo +>Foo : typeof "tests/cases/conformance/externalModules/foo" diff --git a/tests/baselines/reference/umd3.types b/tests/baselines/reference/umd3.types index ab7978545ba..ef54806a174 100644 --- a/tests/baselines/reference/umd3.types +++ b/tests/baselines/reference/umd3.types @@ -32,5 +32,5 @@ export interface Thing { n: typeof x } >x : number export as namespace Foo; ->Foo : typeof Foo +>Foo : typeof "tests/cases/conformance/externalModules/foo" diff --git a/tests/baselines/reference/umd4.types b/tests/baselines/reference/umd4.types index c9144af7a18..3b6ebfa6ca7 100644 --- a/tests/baselines/reference/umd4.types +++ b/tests/baselines/reference/umd4.types @@ -32,5 +32,5 @@ export interface Thing { n: typeof x } >x : number export as namespace Foo; ->Foo : typeof Foo +>Foo : typeof "tests/cases/conformance/externalModules/foo" diff --git a/tests/baselines/reference/umdGlobalConflict.types b/tests/baselines/reference/umdGlobalConflict.types index d23d42e702a..0bb26b47918 100644 --- a/tests/baselines/reference/umdGlobalConflict.types +++ b/tests/baselines/reference/umdGlobalConflict.types @@ -1,6 +1,6 @@ === tests/cases/compiler/v1/index.d.ts === export as namespace Alpha; ->Alpha : typeof Alpha +>Alpha : typeof "tests/cases/compiler/v1/index" export var x: string; >x : string diff --git a/tests/cases/compiler/importShouldNotBeElidedInDeclarationEmit.ts b/tests/cases/compiler/importShouldNotBeElidedInDeclarationEmit.ts new file mode 100644 index 00000000000..a6d77a51567 --- /dev/null +++ b/tests/cases/compiler/importShouldNotBeElidedInDeclarationEmit.ts @@ -0,0 +1,12 @@ +// @declaration: true +// @filename: node_modules/umd.d.ts +export as namespace UMD; + +export type Thing = { + a: number; +} + +export declare function makeThing(): Thing; +// @filename: index.ts +import { makeThing } from "umd"; +export const thing = makeThing();