Skip to content

Commit 0eeb9cb

Browse files
authored
Fix #10083 - allowSyntheticDefaultImports alters getExternalModuleMember (#10096)
1 parent 4a470bd commit 0eeb9cb

15 files changed

+260
-0
lines changed

src/compiler/checker.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,6 +1135,10 @@ namespace ts {
11351135
else {
11361136
symbolFromVariable = getPropertyOfVariable(targetSymbol, name.text);
11371137
}
1138+
// 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
1139+
if (!symbolFromVariable && allowSyntheticDefaultImports && name.text === "default") {
1140+
symbolFromVariable = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol);
1141+
}
11381142
// if symbolFromVariable is export - get its final target
11391143
symbolFromVariable = resolveSymbol(symbolFromVariable);
11401144
const symbolFromModule = getExportOfModule(targetSymbol, name.text);
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
tests/cases/compiler/a.ts(2,5): error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.
2+
tests/cases/compiler/a.ts(3,5): error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.
3+
4+
5+
==== tests/cases/compiler/a.ts (2 errors) ====
6+
import Foo = require("./b");
7+
Foo.default.bar();
8+
~~~~~~~
9+
!!! error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.
10+
Foo.default.default.foo();
11+
~~~~~~~
12+
!!! error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.
13+
==== tests/cases/compiler/b.d.ts (0 errors) ====
14+
export function foo();
15+
16+
export function bar();
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports10.ts] ////
2+
3+
//// [b.d.ts]
4+
export function foo();
5+
6+
export function bar();
7+
8+
//// [a.ts]
9+
import Foo = require("./b");
10+
Foo.default.bar();
11+
Foo.default.default.foo();
12+
13+
//// [a.js]
14+
"use strict";
15+
var Foo = require("./b");
16+
Foo.default.bar();
17+
Foo.default.default.foo();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports7.ts] ////
2+
3+
//// [b.d.ts]
4+
export function foo();
5+
6+
export function bar();
7+
8+
//// [a.ts]
9+
import { default as Foo } from "./b";
10+
Foo.bar();
11+
Foo.foo();
12+
13+
//// [a.js]
14+
System.register(["./b"], function(exports_1, context_1) {
15+
"use strict";
16+
var __moduleName = context_1 && context_1.id;
17+
var b_1;
18+
return {
19+
setters:[
20+
function (b_1_1) {
21+
b_1 = b_1_1;
22+
}],
23+
execute: function() {
24+
b_1["default"].bar();
25+
b_1["default"].foo();
26+
}
27+
}
28+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
export function foo();
3+
>foo : Symbol(foo, Decl(b.d.ts, 0, 0))
4+
5+
export function bar();
6+
>bar : Symbol(bar, Decl(b.d.ts, 0, 22))
7+
8+
=== tests/cases/compiler/a.ts ===
9+
import { default as Foo } from "./b";
10+
>default : Symbol(Foo, Decl(a.ts, 0, 8))
11+
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
12+
13+
Foo.bar();
14+
>Foo.bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))
15+
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
16+
>bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))
17+
18+
Foo.foo();
19+
>Foo.foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))
20+
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
21+
>foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))
22+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
export function foo();
3+
>foo : () => any
4+
5+
export function bar();
6+
>bar : () => any
7+
8+
=== tests/cases/compiler/a.ts ===
9+
import { default as Foo } from "./b";
10+
>default : typeof Foo
11+
>Foo : typeof Foo
12+
13+
Foo.bar();
14+
>Foo.bar() : any
15+
>Foo.bar : () => any
16+
>Foo : typeof Foo
17+
>bar : () => any
18+
19+
Foo.foo();
20+
>Foo.foo() : any
21+
>Foo.foo : () => any
22+
>Foo : typeof Foo
23+
>foo : () => any
24+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/a.ts(1,10): error TS2305: Module '"tests/cases/compiler/b"' has no exported member 'default'.
2+
3+
4+
==== tests/cases/compiler/b.d.ts (0 errors) ====
5+
export function foo();
6+
7+
export function bar();
8+
9+
==== tests/cases/compiler/a.ts (1 errors) ====
10+
import { default as Foo } from "./b";
11+
~~~~~~~
12+
!!! error TS2305: Module '"tests/cases/compiler/b"' has no exported member 'default'.
13+
Foo.bar();
14+
Foo.foo();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports8.ts] ////
2+
3+
//// [b.d.ts]
4+
export function foo();
5+
6+
export function bar();
7+
8+
//// [a.ts]
9+
import { default as Foo } from "./b";
10+
Foo.bar();
11+
Foo.foo();
12+
13+
//// [a.js]
14+
System.register(["./b"], function(exports_1, context_1) {
15+
"use strict";
16+
var __moduleName = context_1 && context_1.id;
17+
var b_1;
18+
return {
19+
setters:[
20+
function (b_1_1) {
21+
b_1 = b_1_1;
22+
}],
23+
execute: function() {
24+
b_1["default"].bar();
25+
b_1["default"].foo();
26+
}
27+
}
28+
});
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports9.ts] ////
2+
3+
//// [b.d.ts]
4+
export function foo();
5+
6+
export function bar();
7+
8+
//// [a.ts]
9+
import { default as Foo } from "./b";
10+
Foo.bar();
11+
Foo.foo();
12+
13+
//// [a.js]
14+
"use strict";
15+
var b_1 = require("./b");
16+
b_1["default"].bar();
17+
b_1["default"].foo();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
export function foo();
3+
>foo : Symbol(foo, Decl(b.d.ts, 0, 0))
4+
5+
export function bar();
6+
>bar : Symbol(bar, Decl(b.d.ts, 0, 22))
7+
8+
=== tests/cases/compiler/a.ts ===
9+
import { default as Foo } from "./b";
10+
>default : Symbol(Foo, Decl(a.ts, 0, 8))
11+
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
12+
13+
Foo.bar();
14+
>Foo.bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))
15+
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
16+
>bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))
17+
18+
Foo.foo();
19+
>Foo.foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))
20+
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
21+
>foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))
22+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
export function foo();
3+
>foo : () => any
4+
5+
export function bar();
6+
>bar : () => any
7+
8+
=== tests/cases/compiler/a.ts ===
9+
import { default as Foo } from "./b";
10+
>default : typeof Foo
11+
>Foo : typeof Foo
12+
13+
Foo.bar();
14+
>Foo.bar() : any
15+
>Foo.bar : () => any
16+
>Foo : typeof Foo
17+
>bar : () => any
18+
19+
Foo.foo();
20+
>Foo.foo() : any
21+
>Foo.foo : () => any
22+
>Foo : typeof Foo
23+
>foo : () => any
24+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @allowSyntheticDefaultImports: true
2+
// @module: commonjs
3+
// @Filename: b.d.ts
4+
export function foo();
5+
6+
export function bar();
7+
8+
// @Filename: a.ts
9+
import Foo = require("./b");
10+
Foo.default.bar();
11+
Foo.default.default.foo();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// @module: system
2+
// @Filename: b.d.ts
3+
export function foo();
4+
5+
export function bar();
6+
7+
// @Filename: a.ts
8+
import { default as Foo } from "./b";
9+
Foo.bar();
10+
Foo.foo();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @allowSyntheticDefaultImports: false
2+
// @module: system
3+
// @Filename: b.d.ts
4+
export function foo();
5+
6+
export function bar();
7+
8+
// @Filename: a.ts
9+
import { default as Foo } from "./b";
10+
Foo.bar();
11+
Foo.foo();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// @allowSyntheticDefaultImports: true
2+
// @module: commonjs
3+
// @Filename: b.d.ts
4+
export function foo();
5+
6+
export function bar();
7+
8+
// @Filename: a.ts
9+
import { default as Foo } from "./b";
10+
Foo.bar();
11+
Foo.foo();

0 commit comments

Comments
 (0)