Skip to content

Do not consider UMD alias symbols as visible within external modules #18049

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand All @@ -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))) {

Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/exportAsNamespace.d.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ export var X;
>X : any

export as namespace N
>N : typeof N
>N : typeof "tests/cases/compiler/exportAsNamespace"

Original file line number Diff line number Diff line change
@@ -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;
};
Original file line number Diff line number Diff line change
@@ -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))

Original file line number Diff line number Diff line change
@@ -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; }

2 changes: 1 addition & 1 deletion tests/baselines/reference/umd-augmentation-1.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/umd-augmentation-2.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/umd-augmentation-3.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/umd-augmentation-3.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/umd-augmentation-4.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/umd-augmentation-4.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/umd1.types
Original file line number Diff line number Diff line change
Expand Up @@ -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"

2 changes: 1 addition & 1 deletion tests/baselines/reference/umd3.types
Original file line number Diff line number Diff line change
Expand Up @@ -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"

2 changes: 1 addition & 1 deletion tests/baselines/reference/umd4.types
Original file line number Diff line number Diff line change
Expand Up @@ -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"

2 changes: 1 addition & 1 deletion tests/baselines/reference/umdGlobalConflict.types
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/cases/compiler/importShouldNotBeElidedInDeclarationEmit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// @declaration: true
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a minimal test that reproduces the error -- looking at this test I would have no idea what it's trying to test..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should always strive for the smallest reproducible steps for a failure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now minimal

// @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();