Skip to content

Fix #10083 - allowSyntheticDefaultImports alters getExternalModuleMember #10096

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 1 commit into from
Aug 2, 2016
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
4 changes: 4 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,10 @@ namespace ts {
else {
symbolFromVariable = getPropertyOfVariable(targetSymbol, name.text);
}
// 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
if (!symbolFromVariable && allowSyntheticDefaultImports && name.text === "default") {
symbolFromVariable = resolveExternalModuleSymbol(moduleSymbol) || resolveSymbol(moduleSymbol);
}
// if symbolFromVariable is export - get its final target
symbolFromVariable = resolveSymbol(symbolFromVariable);
const symbolFromModule = getExportOfModule(targetSymbol, name.text);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
tests/cases/compiler/a.ts(2,5): error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.
tests/cases/compiler/a.ts(3,5): error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.


==== tests/cases/compiler/a.ts (2 errors) ====
import Foo = require("./b");
Foo.default.bar();
~~~~~~~
!!! error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.
Foo.default.default.foo();
~~~~~~~
!!! error TS2339: Property 'default' does not exist on type 'typeof "tests/cases/compiler/b"'.
==== tests/cases/compiler/b.d.ts (0 errors) ====
export function foo();

export function bar();

17 changes: 17 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports10.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/allowSyntheticDefaultImports10.ts] ////

//// [b.d.ts]
export function foo();

export function bar();

//// [a.ts]
import Foo = require("./b");
Foo.default.bar();
Foo.default.default.foo();

//// [a.js]
"use strict";
var Foo = require("./b");
Foo.default.bar();
Foo.default.default.foo();
28 changes: 28 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/allowSyntheticDefaultImports7.ts] ////

//// [b.d.ts]
export function foo();

export function bar();

//// [a.ts]
import { default as Foo } from "./b";
Foo.bar();
Foo.foo();

//// [a.js]
System.register(["./b"], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var b_1;
return {
setters:[
function (b_1_1) {
b_1 = b_1_1;
}],
execute: function() {
b_1["default"].bar();
b_1["default"].foo();
}
}
});
22 changes: 22 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports7.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/b.d.ts ===
export function foo();
>foo : Symbol(foo, Decl(b.d.ts, 0, 0))

export function bar();
>bar : Symbol(bar, Decl(b.d.ts, 0, 22))

=== tests/cases/compiler/a.ts ===
import { default as Foo } from "./b";
>default : Symbol(Foo, Decl(a.ts, 0, 8))
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))

Foo.bar();
>Foo.bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
>bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))

Foo.foo();
>Foo.foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
>foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))

24 changes: 24 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports7.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/b.d.ts ===
export function foo();
>foo : () => any

export function bar();
>bar : () => any

=== tests/cases/compiler/a.ts ===
import { default as Foo } from "./b";
>default : typeof Foo
>Foo : typeof Foo

Foo.bar();
>Foo.bar() : any
>Foo.bar : () => any
>Foo : typeof Foo
>bar : () => any

Foo.foo();
>Foo.foo() : any
>Foo.foo : () => any
>Foo : typeof Foo
>foo : () => any

14 changes: 14 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports8.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
tests/cases/compiler/a.ts(1,10): error TS2305: Module '"tests/cases/compiler/b"' has no exported member 'default'.


==== tests/cases/compiler/b.d.ts (0 errors) ====
export function foo();

export function bar();

==== tests/cases/compiler/a.ts (1 errors) ====
import { default as Foo } from "./b";
~~~~~~~
!!! error TS2305: Module '"tests/cases/compiler/b"' has no exported member 'default'.
Foo.bar();
Foo.foo();
28 changes: 28 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports8.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/allowSyntheticDefaultImports8.ts] ////

//// [b.d.ts]
export function foo();

export function bar();

//// [a.ts]
import { default as Foo } from "./b";
Foo.bar();
Foo.foo();

//// [a.js]
System.register(["./b"], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
var b_1;
return {
setters:[
function (b_1_1) {
b_1 = b_1_1;
}],
execute: function() {
b_1["default"].bar();
b_1["default"].foo();
}
}
});
17 changes: 17 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports9.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/allowSyntheticDefaultImports9.ts] ////

//// [b.d.ts]
export function foo();

export function bar();

//// [a.ts]
import { default as Foo } from "./b";
Foo.bar();
Foo.foo();

//// [a.js]
"use strict";
var b_1 = require("./b");
b_1["default"].bar();
b_1["default"].foo();
22 changes: 22 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports9.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
=== tests/cases/compiler/b.d.ts ===
export function foo();
>foo : Symbol(foo, Decl(b.d.ts, 0, 0))

export function bar();
>bar : Symbol(bar, Decl(b.d.ts, 0, 22))

=== tests/cases/compiler/a.ts ===
import { default as Foo } from "./b";
>default : Symbol(Foo, Decl(a.ts, 0, 8))
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))

Foo.bar();
>Foo.bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
>bar : Symbol(Foo.bar, Decl(b.d.ts, 0, 22))

Foo.foo();
>Foo.foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))
>Foo : Symbol(Foo, Decl(a.ts, 0, 8))
>foo : Symbol(Foo.foo, Decl(b.d.ts, 0, 0))

24 changes: 24 additions & 0 deletions tests/baselines/reference/allowSyntheticDefaultImports9.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
=== tests/cases/compiler/b.d.ts ===
export function foo();
>foo : () => any

export function bar();
>bar : () => any

=== tests/cases/compiler/a.ts ===
import { default as Foo } from "./b";
>default : typeof Foo
>Foo : typeof Foo

Foo.bar();
>Foo.bar() : any
>Foo.bar : () => any
>Foo : typeof Foo
>bar : () => any

Foo.foo();
>Foo.foo() : any
>Foo.foo : () => any
>Foo : typeof Foo
>foo : () => any

11 changes: 11 additions & 0 deletions tests/cases/compiler/allowSyntheticDefaultImports10.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @allowSyntheticDefaultImports: true
// @module: commonjs
// @Filename: b.d.ts
export function foo();

export function bar();

// @Filename: a.ts
import Foo = require("./b");
Foo.default.bar();
Foo.default.default.foo();
10 changes: 10 additions & 0 deletions tests/cases/compiler/allowSyntheticDefaultImports7.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// @module: system
// @Filename: b.d.ts
export function foo();

export function bar();

// @Filename: a.ts
import { default as Foo } from "./b";
Foo.bar();
Foo.foo();
11 changes: 11 additions & 0 deletions tests/cases/compiler/allowSyntheticDefaultImports8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @allowSyntheticDefaultImports: false
// @module: system
// @Filename: b.d.ts
export function foo();

export function bar();

// @Filename: a.ts
import { default as Foo } from "./b";
Foo.bar();
Foo.foo();
11 changes: 11 additions & 0 deletions tests/cases/compiler/allowSyntheticDefaultImports9.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @allowSyntheticDefaultImports: true
// @module: commonjs
// @Filename: b.d.ts
export function foo();

export function bar();

// @Filename: a.ts
import { default as Foo } from "./b";
Foo.bar();
Foo.foo();