Skip to content

Commit a32f44f

Browse files
committed
Add command line flag to allow synthetic default exports
1 parent 4cba1b2 commit a32f44f

26 files changed

Lines changed: 424 additions & 1 deletion

src/compiler/checker.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace ts {
4646
const compilerOptions = host.getCompilerOptions();
4747
const languageVersion = compilerOptions.target || ScriptTarget.ES3;
4848
const modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None;
49+
const allowSyntheticDefaultImports = compilerOptions.hasOwnProperty("allowSyntheticDefaultImports") ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
4950

5051
const emitResolver = createResolver();
5152

@@ -730,9 +731,12 @@ namespace ts {
730731
const moduleSymbol = resolveExternalModuleName(node, (<ImportDeclaration>node.parent).moduleSpecifier);
731732
if (moduleSymbol) {
732733
const exportDefaultSymbol = resolveSymbol(moduleSymbol.exports["default"]);
733-
if (!exportDefaultSymbol) {
734+
if (!exportDefaultSymbol && !allowSyntheticDefaultImports) {
734735
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
735736
}
737+
else if (!exportDefaultSymbol && allowSyntheticDefaultImports) {
738+
return resolveSymbol(moduleSymbol.exports["export="]) || resolveSymbol(moduleSymbol);
739+
}
736740
return exportDefaultSymbol;
737741
}
738742
}

src/compiler/commandLineParser.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,11 @@ namespace ts {
279279
name: "forceConsistentCasingInFileNames",
280280
type: "boolean",
281281
description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file
282+
},
283+
{
284+
name: "allowSyntheticDefaultImports",
285+
type: "boolean",
286+
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export
282287
}
283288
];
284289

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,10 @@
21012101
"category": "Message",
21022102
"code": 6010
21032103
},
2104+
"Allow default imports from modules with no default export": {
2105+
"category": "Message",
2106+
"code": 6011
2107+
},
21042108
"Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)": {
21052109
"category": "Message",
21062110
"code": 6015

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,6 +2109,7 @@ namespace ts {
21092109
noImplicitReturns?: boolean;
21102110
noFallthroughCasesInSwitch?: boolean;
21112111
forceConsistentCasingInFileNames?: boolean;
2112+
allowSyntheticDefaultImports?: boolean;
21122113
/* @internal */ stripInternal?: boolean;
21132114

21142115
// Skip checking lib.d.ts to help speed up tests.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports1.ts] ////
2+
3+
//// [a.ts]
4+
import Namespace from "./b";
5+
export var x = new Namespace.Foo();
6+
7+
//// [b.ts]
8+
export class Foo {
9+
member: string;
10+
}
11+
12+
13+
//// [b.js]
14+
var Foo = (function () {
15+
function Foo() {
16+
}
17+
return Foo;
18+
})();
19+
exports.Foo = Foo;
20+
//// [a.js]
21+
var b_1 = require("./b");
22+
exports.x = new b_1["default"].Foo();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/a.ts ===
2+
import Namespace from "./b";
3+
>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6))
4+
5+
export var x = new Namespace.Foo();
6+
>x : Symbol(x, Decl(a.ts, 1, 10))
7+
>Namespace.Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0))
8+
>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6))
9+
>Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0))
10+
11+
=== tests/cases/compiler/b.ts ===
12+
export class Foo {
13+
>Foo : Symbol(Foo, Decl(b.ts, 0, 0))
14+
15+
member: string;
16+
>member : Symbol(member, Decl(b.ts, 0, 18))
17+
}
18+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/a.ts ===
2+
import Namespace from "./b";
3+
>Namespace : typeof Namespace
4+
5+
export var x = new Namespace.Foo();
6+
>x : Namespace.Foo
7+
>new Namespace.Foo() : Namespace.Foo
8+
>Namespace.Foo : typeof Namespace.Foo
9+
>Namespace : typeof Namespace
10+
>Foo : typeof Namespace.Foo
11+
12+
=== tests/cases/compiler/b.ts ===
13+
export class Foo {
14+
>Foo : Foo
15+
16+
member: string;
17+
>member : string
18+
}
19+
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports2.ts] ////
2+
3+
//// [a.ts]
4+
import Namespace from "./b";
5+
export var x = new Namespace.Foo();
6+
7+
//// [b.ts]
8+
export class Foo {
9+
member: string;
10+
}
11+
12+
//// [b.js]
13+
System.register([], function(exports_1) {
14+
var Foo;
15+
return {
16+
setters:[],
17+
execute: function() {
18+
Foo = (function () {
19+
function Foo() {
20+
}
21+
return Foo;
22+
})();
23+
exports_1("Foo", Foo);
24+
}
25+
}
26+
});
27+
//// [a.js]
28+
System.register(["./b"], function(exports_1) {
29+
var b_1;
30+
var x;
31+
return {
32+
setters:[
33+
function (b_1_1) {
34+
b_1 = b_1_1;
35+
}],
36+
execute: function() {
37+
exports_1("x", x = new b_1["default"].Foo());
38+
}
39+
}
40+
});
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+
import Namespace from "./b";
3+
>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6))
4+
5+
export var x = new Namespace.Foo();
6+
>x : Symbol(x, Decl(a.ts, 1, 10))
7+
>Namespace.Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0))
8+
>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6))
9+
>Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0))
10+
11+
=== tests/cases/compiler/b.ts ===
12+
export class Foo {
13+
>Foo : Symbol(Foo, Decl(b.ts, 0, 0))
14+
15+
member: string;
16+
>member : Symbol(member, Decl(b.ts, 0, 18))
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/a.ts ===
2+
import Namespace from "./b";
3+
>Namespace : typeof Namespace
4+
5+
export var x = new Namespace.Foo();
6+
>x : Namespace.Foo
7+
>new Namespace.Foo() : Namespace.Foo
8+
>Namespace.Foo : typeof Namespace.Foo
9+
>Namespace : typeof Namespace
10+
>Foo : typeof Namespace.Foo
11+
12+
=== tests/cases/compiler/b.ts ===
13+
export class Foo {
14+
>Foo : Foo
15+
16+
member: string;
17+
>member : string
18+
}

0 commit comments

Comments
 (0)