Skip to content

Commit ca66241

Browse files
authored
Dont consider export specifiers visible in their local scope (#26884)
1 parent f1370ec commit ca66241

22 files changed

+224
-75
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2748,7 +2748,11 @@ namespace ts {
27482748
&& symbolFromSymbolTable.escapedName !== InternalSymbolName.Default
27492749
&& !(isUMDExportSymbol(symbolFromSymbolTable) && enclosingDeclaration && isExternalModule(getSourceFileOfNode(enclosingDeclaration)))
27502750
// If `!useOnlyExternalAliasing`, we can use any type of alias to get the name
2751-
&& (!useOnlyExternalAliasing || some(symbolFromSymbolTable.declarations, isExternalModuleImportEqualsDeclaration))) {
2751+
&& (!useOnlyExternalAliasing || some(symbolFromSymbolTable.declarations, isExternalModuleImportEqualsDeclaration))
2752+
// While exports are generally considered to be in scope, export-specifier declared symbols are _not_
2753+
// See similar comment in `resolveName` for details
2754+
&& (ignoreQualification || !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier))
2755+
) {
27522756

27532757
const resolvedImportedSymbol = resolveAlias(symbolFromSymbolTable);
27542758
if (isAccessible(symbolFromSymbolTable, resolvedImportedSymbol, ignoreQualification)) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//// [tests/cases/compiler/declarationEmitExportAliasVisibiilityMarking.ts] ////
2+
3+
//// [Types.ts]
4+
type Suit = 'Hearts' | 'Spades' | 'Clubs' | 'Diamonds';
5+
type Rank = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 'Jack' | 'Queen' | 'King';
6+
export { Suit, Rank };
7+
8+
//// [Card.ts]
9+
import { Suit, Rank } from './Types';
10+
export default (suit: Suit, rank: Rank) => ({suit, rank});
11+
12+
//// [index.ts]
13+
export let lazyCard = () => import('./Card').then(a => a.default);
14+
export { Suit, Rank } from './Types';
15+
16+
17+
//// [Types.js]
18+
"use strict";
19+
exports.__esModule = true;
20+
//// [Card.js]
21+
"use strict";
22+
exports.__esModule = true;
23+
exports["default"] = (function (suit, rank) { return ({ suit: suit, rank: rank }); });
24+
//// [index.js]
25+
"use strict";
26+
exports.__esModule = true;
27+
exports.lazyCard = function () { return Promise.resolve().then(function () { return require('./Card'); }).then(function (a) { return a["default"]; }); };
28+
29+
30+
//// [Types.d.ts]
31+
declare type Suit = 'Hearts' | 'Spades' | 'Clubs' | 'Diamonds';
32+
declare type Rank = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 'Jack' | 'Queen' | 'King';
33+
export { Suit, Rank };
34+
//// [Card.d.ts]
35+
import { Suit, Rank } from './Types';
36+
declare const _default: (suit: Suit, rank: Rank) => {
37+
suit: Suit;
38+
rank: Rank;
39+
};
40+
export default _default;
41+
//// [index.d.ts]
42+
export declare let lazyCard: () => Promise<(suit: import("./Types").Suit, rank: import("./Types").Rank) => {
43+
suit: import("./Types").Suit;
44+
rank: import("./Types").Rank;
45+
}>;
46+
export { Suit, Rank } from './Types';
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
=== tests/cases/compiler/Types.ts ===
2+
type Suit = 'Hearts' | 'Spades' | 'Clubs' | 'Diamonds';
3+
>Suit : Symbol(Suit, Decl(Types.ts, 0, 0))
4+
5+
type Rank = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 'Jack' | 'Queen' | 'King';
6+
>Rank : Symbol(Rank, Decl(Types.ts, 0, 55))
7+
8+
export { Suit, Rank };
9+
>Suit : Symbol(Suit, Decl(Types.ts, 2, 8))
10+
>Rank : Symbol(Rank, Decl(Types.ts, 2, 14))
11+
12+
=== tests/cases/compiler/Card.ts ===
13+
import { Suit, Rank } from './Types';
14+
>Suit : Symbol(Suit, Decl(Card.ts, 0, 8))
15+
>Rank : Symbol(Rank, Decl(Card.ts, 0, 14))
16+
17+
export default (suit: Suit, rank: Rank) => ({suit, rank});
18+
>suit : Symbol(suit, Decl(Card.ts, 1, 16))
19+
>Suit : Symbol(Suit, Decl(Card.ts, 0, 8))
20+
>rank : Symbol(rank, Decl(Card.ts, 1, 27))
21+
>Rank : Symbol(Rank, Decl(Card.ts, 0, 14))
22+
>suit : Symbol(suit, Decl(Card.ts, 1, 45))
23+
>rank : Symbol(rank, Decl(Card.ts, 1, 50))
24+
25+
=== tests/cases/compiler/index.ts ===
26+
export let lazyCard = () => import('./Card').then(a => a.default);
27+
>lazyCard : Symbol(lazyCard, Decl(index.ts, 0, 10))
28+
>import('./Card').then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
29+
>'./Card' : Symbol("tests/cases/compiler/Card", Decl(Card.ts, 0, 0))
30+
>then : Symbol(Promise.then, Decl(lib.es5.d.ts, --, --))
31+
>a : Symbol(a, Decl(index.ts, 0, 50))
32+
>a.default : Symbol(default, Decl(Card.ts, 0, 37))
33+
>a : Symbol(a, Decl(index.ts, 0, 50))
34+
>default : Symbol(default, Decl(Card.ts, 0, 37))
35+
36+
export { Suit, Rank } from './Types';
37+
>Suit : Symbol(Suit, Decl(index.ts, 1, 8))
38+
>Rank : Symbol(Rank, Decl(index.ts, 1, 14))
39+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
=== tests/cases/compiler/Types.ts ===
2+
type Suit = 'Hearts' | 'Spades' | 'Clubs' | 'Diamonds';
3+
>Suit : Suit
4+
5+
type Rank = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 'Jack' | 'Queen' | 'King';
6+
>Rank : Rank
7+
8+
export { Suit, Rank };
9+
>Suit : any
10+
>Rank : any
11+
12+
=== tests/cases/compiler/Card.ts ===
13+
import { Suit, Rank } from './Types';
14+
>Suit : any
15+
>Rank : any
16+
17+
export default (suit: Suit, rank: Rank) => ({suit, rank});
18+
>(suit: Suit, rank: Rank) => ({suit, rank}) : (suit: Suit, rank: Rank) => { suit: Suit; rank: Rank; }
19+
>suit : Suit
20+
>rank : Rank
21+
>({suit, rank}) : { suit: Suit; rank: Rank; }
22+
>{suit, rank} : { suit: Suit; rank: Rank; }
23+
>suit : Suit
24+
>rank : Rank
25+
26+
=== tests/cases/compiler/index.ts ===
27+
export let lazyCard = () => import('./Card').then(a => a.default);
28+
>lazyCard : () => Promise<(suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }>
29+
>() => import('./Card').then(a => a.default) : () => Promise<(suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }>
30+
>import('./Card').then(a => a.default) : Promise<(suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }>
31+
>import('./Card').then : <TResult1 = typeof import("tests/cases/compiler/Card"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/compiler/Card")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
32+
>import('./Card') : Promise<typeof import("tests/cases/compiler/Card")>
33+
>'./Card' : "./Card"
34+
>then : <TResult1 = typeof import("tests/cases/compiler/Card"), TResult2 = never>(onfulfilled?: (value: typeof import("tests/cases/compiler/Card")) => TResult1 | PromiseLike<TResult1>, onrejected?: (reason: any) => TResult2 | PromiseLike<TResult2>) => Promise<TResult1 | TResult2>
35+
>a => a.default : (a: typeof import("tests/cases/compiler/Card")) => (suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }
36+
>a : typeof import("tests/cases/compiler/Card")
37+
>a.default : (suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }
38+
>a : typeof import("tests/cases/compiler/Card")
39+
>default : (suit: import("tests/cases/compiler/Types").Suit, rank: import("tests/cases/compiler/Types").Rank) => { suit: import("tests/cases/compiler/Types").Suit; rank: import("tests/cases/compiler/Types").Rank; }
40+
41+
export { Suit, Rank } from './Types';
42+
>Suit : any
43+
>Rank : any
44+

tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ export module uninstantiated {
2020

2121
=== tests/cases/compiler/client.ts ===
2222
export { c } from "server";
23-
>c : typeof c
23+
>c : typeof import("tests/cases/compiler/server").c
2424

2525
export { c as c2 } from "server";
26-
>c : typeof c
27-
>c2 : typeof c
26+
>c : typeof import("tests/cases/compiler/server").c
27+
>c2 : typeof import("tests/cases/compiler/server").c
2828

2929
export { i, m as instantiatedModule } from "server";
3030
>i : any
31-
>m : typeof instantiatedModule
32-
>instantiatedModule : typeof instantiatedModule
31+
>m : typeof import("tests/cases/compiler/server").m
32+
>instantiatedModule : typeof import("tests/cases/compiler/server").m
3333

3434
export { uninstantiated } from "server";
3535
>uninstantiated : any

tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.types

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ export module uninstantiated {
2020

2121
=== tests/cases/compiler/client.ts ===
2222
export { c } from "./server";
23-
>c : typeof c
23+
>c : typeof import("tests/cases/compiler/server").c
2424

2525
export { c as c2 } from "./server";
26-
>c : typeof c
27-
>c2 : typeof c
26+
>c : typeof import("tests/cases/compiler/server").c
27+
>c2 : typeof import("tests/cases/compiler/server").c
2828

2929
export { i, m as instantiatedModule } from "./server";
3030
>i : any
31-
>m : typeof instantiatedModule
32-
>instantiatedModule : typeof instantiatedModule
31+
>m : typeof import("tests/cases/compiler/server").m
32+
>instantiatedModule : typeof import("tests/cases/compiler/server").m
3333

3434
export { uninstantiated } from "./server";
3535
>uninstantiated : any

tests/baselines/reference/exportSpecifierForAGlobal.errors.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
tests/cases/compiler/b.ts(1,9): error TS2661: Cannot export 'X'. Only local declarations can be exported from a module.
2-
tests/cases/compiler/b.ts(2,17): error TS4060: Return type of exported function has or is using private name 'X'.
32

43

54
==== tests/cases/compiler/a.d.ts (0 errors) ====
65
declare class X { }
76

8-
==== tests/cases/compiler/b.ts (2 errors) ====
7+
==== tests/cases/compiler/b.ts (1 errors) ====
98
export {X};
109
~
1110
!!! error TS2661: Cannot export 'X'. Only local declarations can be exported from a module.
1211
export function f() {
13-
~
14-
!!! error TS4060: Return type of exported function has or is using private name 'X'.
1512
var x: X;
1613
return x;
1714
}

tests/baselines/reference/exportSpecifierForAGlobal.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ function f() {
1919
return x;
2020
}
2121
exports.f = f;
22+
23+
24+
//// [b.d.ts]
25+
export { X };
26+
export declare function f(): X;

tests/baselines/reference/exportsAndImports1-amd.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ export { v, f, C, I, E, D, M, N, T, a };
6161
export { v, f, C, I, E, D, M, N, T, a } from "./t1";
6262
>v : number
6363
>f : () => void
64-
>C : typeof C
64+
>C : typeof import("tests/cases/conformance/es6/modules/t1").C
6565
>I : any
66-
>E : typeof E
67-
>D : typeof D
68-
>M : typeof M
66+
>E : typeof import("tests/cases/conformance/es6/modules/t1").E
67+
>D : typeof import("tests/cases/conformance/es6/modules/t1").D
68+
>M : typeof import("tests/cases/conformance/es6/modules/t1").M
6969
>N : any
7070
>T : any
7171
>a : any

tests/baselines/reference/exportsAndImports1-es6.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ export { v, f, C, I, E, D, M, N, T, a };
6161
export { v, f, C, I, E, D, M, N, T, a } from "./t1";
6262
>v : number
6363
>f : () => void
64-
>C : typeof C
64+
>C : typeof import("tests/cases/conformance/es6/modules/t1").C
6565
>I : any
66-
>E : typeof E
67-
>D : typeof D
68-
>M : typeof M
66+
>E : typeof import("tests/cases/conformance/es6/modules/t1").E
67+
>D : typeof import("tests/cases/conformance/es6/modules/t1").D
68+
>M : typeof import("tests/cases/conformance/es6/modules/t1").M
6969
>N : any
7070
>T : any
7171
>a : any

tests/baselines/reference/exportsAndImports1.types

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,11 @@ export { v, f, C, I, E, D, M, N, T, a };
6161
export { v, f, C, I, E, D, M, N, T, a } from "./t1";
6262
>v : number
6363
>f : () => void
64-
>C : typeof C
64+
>C : typeof import("tests/cases/conformance/es6/modules/t1").C
6565
>I : any
66-
>E : typeof E
67-
>D : typeof D
68-
>M : typeof M
66+
>E : typeof import("tests/cases/conformance/es6/modules/t1").E
67+
>D : typeof import("tests/cases/conformance/es6/modules/t1").D
68+
>M : typeof import("tests/cases/conformance/es6/modules/t1").M
6969
>N : any
7070
>T : any
7171
>a : any

tests/baselines/reference/exportsAndImports3-amd.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ export enum E {
1515
>E : Symbol(E, Decl(t1.ts, 5, 1))
1616

1717
A, B, C
18-
>A : Symbol(E1.A, Decl(t1.ts, 6, 15))
19-
>B : Symbol(E1.B, Decl(t1.ts, 7, 6))
20-
>C : Symbol(E1.C, Decl(t1.ts, 7, 9))
18+
>A : Symbol(E.A, Decl(t1.ts, 6, 15))
19+
>B : Symbol(E.B, Decl(t1.ts, 7, 6))
20+
>C : Symbol(E.C, Decl(t1.ts, 7, 9))
2121
}
2222
export const enum D {
2323
>D : Symbol(D, Decl(t1.ts, 8, 1))
2424

2525
A, B, C
26-
>A : Symbol(D1.A, Decl(t1.ts, 9, 21))
27-
>B : Symbol(D1.B, Decl(t1.ts, 10, 6))
28-
>C : Symbol(D1.C, Decl(t1.ts, 10, 9))
26+
>A : Symbol(D.A, Decl(t1.ts, 9, 21))
27+
>B : Symbol(D.B, Decl(t1.ts, 10, 6))
28+
>C : Symbol(D.C, Decl(t1.ts, 10, 9))
2929
}
3030
export module M {
3131
>M : Symbol(M, Decl(t1.ts, 11, 1))

tests/baselines/reference/exportsAndImports3-amd.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ export { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N,
7373
>v : number
7474
>f1 : () => void
7575
>f : () => void
76-
>C1 : typeof C
77-
>C : typeof C
76+
>C1 : typeof import("tests/cases/conformance/es6/modules/t1").C
77+
>C : typeof import("tests/cases/conformance/es6/modules/t1").C
7878
>I1 : any
7979
>I : any
80-
>E1 : typeof E
81-
>E : typeof E
82-
>D1 : typeof D
83-
>D : typeof D
84-
>M1 : typeof M
85-
>M : typeof M
80+
>E1 : typeof import("tests/cases/conformance/es6/modules/t1").E
81+
>E : typeof import("tests/cases/conformance/es6/modules/t1").E
82+
>D1 : typeof import("tests/cases/conformance/es6/modules/t1").D
83+
>D : typeof import("tests/cases/conformance/es6/modules/t1").D
84+
>M1 : typeof import("tests/cases/conformance/es6/modules/t1").M
85+
>M : typeof import("tests/cases/conformance/es6/modules/t1").M
8686
>N1 : any
8787
>N : any
8888
>T1 : any

tests/baselines/reference/exportsAndImports3-es6.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ export enum E {
1515
>E : Symbol(E, Decl(t1.ts, 5, 1))
1616

1717
A, B, C
18-
>A : Symbol(E1.A, Decl(t1.ts, 6, 15))
19-
>B : Symbol(E1.B, Decl(t1.ts, 7, 6))
20-
>C : Symbol(E1.C, Decl(t1.ts, 7, 9))
18+
>A : Symbol(E.A, Decl(t1.ts, 6, 15))
19+
>B : Symbol(E.B, Decl(t1.ts, 7, 6))
20+
>C : Symbol(E.C, Decl(t1.ts, 7, 9))
2121
}
2222
export const enum D {
2323
>D : Symbol(D, Decl(t1.ts, 8, 1))
2424

2525
A, B, C
26-
>A : Symbol(D1.A, Decl(t1.ts, 9, 21))
27-
>B : Symbol(D1.B, Decl(t1.ts, 10, 6))
28-
>C : Symbol(D1.C, Decl(t1.ts, 10, 9))
26+
>A : Symbol(D.A, Decl(t1.ts, 9, 21))
27+
>B : Symbol(D.B, Decl(t1.ts, 10, 6))
28+
>C : Symbol(D.C, Decl(t1.ts, 10, 9))
2929
}
3030
export module M {
3131
>M : Symbol(M, Decl(t1.ts, 11, 1))

tests/baselines/reference/exportsAndImports3-es6.types

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,16 @@ export { v1 as v, f1 as f, C1 as C, I1 as I, E1 as E, D1 as D, M1 as M, N1 as N,
7373
>v : number
7474
>f1 : () => void
7575
>f : () => void
76-
>C1 : typeof C
77-
>C : typeof C
76+
>C1 : typeof import("tests/cases/conformance/es6/modules/t1").C
77+
>C : typeof import("tests/cases/conformance/es6/modules/t1").C
7878
>I1 : any
7979
>I : any
80-
>E1 : typeof E
81-
>E : typeof E
82-
>D1 : typeof D
83-
>D : typeof D
84-
>M1 : typeof M
85-
>M : typeof M
80+
>E1 : typeof import("tests/cases/conformance/es6/modules/t1").E
81+
>E : typeof import("tests/cases/conformance/es6/modules/t1").E
82+
>D1 : typeof import("tests/cases/conformance/es6/modules/t1").D
83+
>D : typeof import("tests/cases/conformance/es6/modules/t1").D
84+
>M1 : typeof import("tests/cases/conformance/es6/modules/t1").M
85+
>M : typeof import("tests/cases/conformance/es6/modules/t1").M
8686
>N1 : any
8787
>N : any
8888
>T1 : any

tests/baselines/reference/exportsAndImports3.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ export enum E {
1515
>E : Symbol(E, Decl(t1.ts, 5, 1))
1616

1717
A, B, C
18-
>A : Symbol(E1.A, Decl(t1.ts, 6, 15))
19-
>B : Symbol(E1.B, Decl(t1.ts, 7, 6))
20-
>C : Symbol(E1.C, Decl(t1.ts, 7, 9))
18+
>A : Symbol(E.A, Decl(t1.ts, 6, 15))
19+
>B : Symbol(E.B, Decl(t1.ts, 7, 6))
20+
>C : Symbol(E.C, Decl(t1.ts, 7, 9))
2121
}
2222
export const enum D {
2323
>D : Symbol(D, Decl(t1.ts, 8, 1))
2424

2525
A, B, C
26-
>A : Symbol(D1.A, Decl(t1.ts, 9, 21))
27-
>B : Symbol(D1.B, Decl(t1.ts, 10, 6))
28-
>C : Symbol(D1.C, Decl(t1.ts, 10, 9))
26+
>A : Symbol(D.A, Decl(t1.ts, 9, 21))
27+
>B : Symbol(D.B, Decl(t1.ts, 10, 6))
28+
>C : Symbol(D.C, Decl(t1.ts, 10, 9))
2929
}
3030
export module M {
3131
>M : Symbol(M, Decl(t1.ts, 11, 1))

0 commit comments

Comments
 (0)