Skip to content

Commit 046b55e

Browse files
committed
allowSyntheticDefaultImports resolves to modules instead of variables
Fixes #10429 by improving the fix in #10096
1 parent 4a58e68 commit 046b55e

File tree

5 files changed

+109
-5
lines changed

5 files changed

+109
-5
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,13 +1126,13 @@ namespace ts {
11261126
else {
11271127
symbolFromVariable = getPropertyOfVariable(targetSymbol, name.text);
11281128
}
1129-
// If the export member we're looking for is default, and there is no real default but allowSyntheticDefaultImports is on, return the entire module as the default
1130-
if (!symbolFromVariable && allowSyntheticDefaultImports && name.text === "default") {
1131-
symbolFromVariable = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol);
1132-
}
11331129
// if symbolFromVariable is export - get its final target
11341130
symbolFromVariable = resolveSymbol(symbolFromVariable);
1135-
const symbolFromModule = getExportOfModule(targetSymbol, name.text);
1131+
let symbolFromModule = getExportOfModule(targetSymbol, name.text);
1132+
// If the export member we're looking for is default, and there is no real default but allowSyntheticDefaultImports is on, return the entire module as the default
1133+
if (!symbolFromModule && allowSyntheticDefaultImports && name.text === "default") {
1134+
symbolFromModule = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol);
1135+
}
11361136
const symbol = symbolFromModule && symbolFromVariable ?
11371137
combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) :
11381138
symbolFromModule || symbolFromVariable;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//// [tests/cases/compiler/tsxDefaultImports.ts] ////
2+
3+
//// [a.ts]
4+
5+
enum SomeEnum {
6+
one,
7+
}
8+
export default class SomeClass {
9+
public static E = SomeEnum;
10+
}
11+
12+
//// [b.ts]
13+
import {default as Def} from "./a"
14+
let a = Def.E.one;
15+
16+
17+
//// [a.js]
18+
"use strict";
19+
var SomeEnum;
20+
(function (SomeEnum) {
21+
SomeEnum[SomeEnum["one"] = 0] = "one";
22+
})(SomeEnum || (SomeEnum = {}));
23+
var SomeClass = (function () {
24+
function SomeClass() {
25+
}
26+
SomeClass.E = SomeEnum;
27+
return SomeClass;
28+
}());
29+
exports.__esModule = true;
30+
exports["default"] = SomeClass;
31+
//// [b.js]
32+
"use strict";
33+
var a_1 = require("./a");
34+
var a = a_1["default"].E.one;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/a.ts ===
2+
3+
enum SomeEnum {
4+
>SomeEnum : Symbol(SomeEnum, Decl(a.ts, 0, 0))
5+
6+
one,
7+
>one : Symbol(SomeEnum.one, Decl(a.ts, 1, 15))
8+
}
9+
export default class SomeClass {
10+
>SomeClass : Symbol(SomeClass, Decl(a.ts, 3, 1))
11+
12+
public static E = SomeEnum;
13+
>E : Symbol(SomeClass.E, Decl(a.ts, 4, 32))
14+
>SomeEnum : Symbol(SomeEnum, Decl(a.ts, 0, 0))
15+
}
16+
17+
=== tests/cases/compiler/b.ts ===
18+
import {default as Def} from "./a"
19+
>default : Symbol(Def, Decl(b.ts, 0, 8))
20+
>Def : Symbol(Def, Decl(b.ts, 0, 8))
21+
22+
let a = Def.E.one;
23+
>a : Symbol(a, Decl(b.ts, 1, 3))
24+
>Def.E.one : Symbol(SomeEnum.one, Decl(a.ts, 1, 15))
25+
>Def.E : Symbol(Def.E, Decl(a.ts, 4, 32))
26+
>Def : Symbol(Def, Decl(b.ts, 0, 8))
27+
>E : Symbol(Def.E, Decl(a.ts, 4, 32))
28+
>one : Symbol(SomeEnum.one, Decl(a.ts, 1, 15))
29+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/a.ts ===
2+
3+
enum SomeEnum {
4+
>SomeEnum : SomeEnum
5+
6+
one,
7+
>one : SomeEnum
8+
}
9+
export default class SomeClass {
10+
>SomeClass : SomeClass
11+
12+
public static E = SomeEnum;
13+
>E : typeof SomeEnum
14+
>SomeEnum : typeof SomeEnum
15+
}
16+
17+
=== tests/cases/compiler/b.ts ===
18+
import {default as Def} from "./a"
19+
>default : typeof Def
20+
>Def : typeof Def
21+
22+
let a = Def.E.one;
23+
>a : SomeEnum
24+
>Def.E.one : SomeEnum
25+
>Def.E : typeof SomeEnum
26+
>Def : typeof Def
27+
>E : typeof SomeEnum
28+
>one : SomeEnum
29+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @Filename: a.ts
2+
3+
enum SomeEnum {
4+
one,
5+
}
6+
export default class SomeClass {
7+
public static E = SomeEnum;
8+
}
9+
10+
// @Filename: b.ts
11+
import {default as Def} from "./a"
12+
let a = Def.E.one;

0 commit comments

Comments
 (0)