Skip to content

Commit 38215c6

Browse files
committed
Merge pull request #5577 from weswigham/autohoist-default
Add command line flag to allow synthetic default exports
2 parents ebbce6a + fd5f440 commit 38215c6

26 files changed

+435
-3
lines changed

Diff for: src/compiler/checker.ts

+5-1
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 = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System;
4950

5051
const emitResolver = createResolver();
5152

@@ -768,9 +769,12 @@ namespace ts {
768769
const moduleSymbol = resolveExternalModuleName(node, (<ImportDeclaration>node.parent).moduleSpecifier);
769770
if (moduleSymbol) {
770771
const exportDefaultSymbol = resolveSymbol(moduleSymbol.exports["default"]);
771-
if (!exportDefaultSymbol) {
772+
if (!exportDefaultSymbol && !allowSyntheticDefaultImports) {
772773
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
773774
}
775+
else if (!exportDefaultSymbol && allowSyntheticDefaultImports) {
776+
return resolveSymbol(moduleSymbol.exports["export="]) || resolveSymbol(moduleSymbol);
777+
}
774778
return exportDefaultSymbol;
775779
}
776780
}

Diff for: src/compiler/commandLineParser.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,15 @@ namespace ts {
280280
type: "boolean",
281281
description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file
282282
},
283+
{
284+
name: "allowSyntheticDefaultImports",
285+
type: "boolean",
286+
description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking
287+
},
283288
{
284289
name: "allowJs",
285290
type: "boolean",
286-
description: Diagnostics.Allow_javascript_files_to_be_compiled,
291+
description: Diagnostics.Allow_javascript_files_to_be_compiled
287292
}
288293
];
289294

@@ -488,7 +493,7 @@ namespace ts {
488493
fileNames: getFileNames(),
489494
errors
490495
};
491-
496+
492497
function getFileNames(): string[] {
493498
let fileNames: string[] = [];
494499
if (hasProperty(json, "files")) {

Diff for: src/compiler/diagnosticMessages.json

+4
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,10 @@
21172117
"category": "Message",
21182118
"code": 6010
21192119
},
2120+
"Allow default imports from modules with no default export. This does not affect code emit, just typechecking.": {
2121+
"category": "Message",
2122+
"code": 6011
2123+
},
21202124
"Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)": {
21212125
"category": "Message",
21222126
"code": 6015

Diff for: src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -2372,6 +2372,7 @@ namespace ts {
23722372
noImplicitReturns?: boolean;
23732373
noFallthroughCasesInSwitch?: boolean;
23742374
forceConsistentCasingInFileNames?: boolean;
2375+
allowSyntheticDefaultImports?: boolean;
23752376
allowJs?: boolean;
23762377
/* @internal */ stripInternal?: boolean;
23772378

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
"use strict";
15+
var Foo = (function () {
16+
function Foo() {
17+
}
18+
return Foo;
19+
})();
20+
exports.Foo = Foo;
21+
//// [a.js]
22+
"use strict";
23+
var b_1 = require("./b");
24+
exports.x = new b_1["default"].Foo();
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+
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
"use strict";
15+
var Foo;
16+
return {
17+
setters:[],
18+
execute: function() {
19+
Foo = (function () {
20+
function Foo() {
21+
}
22+
return Foo;
23+
})();
24+
exports_1("Foo", Foo);
25+
}
26+
}
27+
});
28+
//// [a.js]
29+
System.register(["./b"], function(exports_1) {
30+
"use strict";
31+
var b_1;
32+
var x;
33+
return {
34+
setters:[
35+
function (b_1_1) {
36+
b_1 = b_1_1;
37+
}],
38+
execute: function() {
39+
exports_1("x", x = new b_1["default"].Foo());
40+
}
41+
}
42+
});
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+
}
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/a.ts(1,8): error TS1192: Module '"tests/cases/compiler/b"' has no default export.
2+
3+
4+
==== tests/cases/compiler/a.ts (1 errors) ====
5+
import Namespace from "./b";
6+
~~~~~~~~~
7+
!!! error TS1192: Module '"tests/cases/compiler/b"' has no default export.
8+
export var x = new Namespace.Foo();
9+
10+
==== tests/cases/compiler/b.ts (0 errors) ====
11+
export class Foo {
12+
member: string;
13+
}
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports3.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+
System.register([], function(exports_1) {
15+
"use strict";
16+
var Foo;
17+
return {
18+
setters:[],
19+
execute: function() {
20+
Foo = (function () {
21+
function Foo() {
22+
}
23+
return Foo;
24+
})();
25+
exports_1("Foo", Foo);
26+
}
27+
}
28+
});
29+
//// [a.js]
30+
System.register(["./b"], function(exports_1) {
31+
"use strict";
32+
var b_1;
33+
var x;
34+
return {
35+
setters:[
36+
function (b_1_1) {
37+
b_1 = b_1_1;
38+
}],
39+
execute: function() {
40+
exports_1("x", x = new b_1["default"].Foo());
41+
}
42+
}
43+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports4.ts] ////
2+
3+
//// [b.d.ts]
4+
declare class Foo {
5+
member: string;
6+
}
7+
export = Foo;
8+
9+
//// [a.ts]
10+
import Foo from "./b";
11+
export var x = new Foo();
12+
13+
14+
//// [a.js]
15+
"use strict";
16+
var b_1 = require("./b");
17+
exports.x = new b_1["default"]();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
declare class Foo {
3+
>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0))
4+
5+
member: string;
6+
>member : Symbol(member, Decl(b.d.ts, 0, 19))
7+
}
8+
export = Foo;
9+
>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0))
10+
11+
=== tests/cases/compiler/a.ts ===
12+
import Foo from "./b";
13+
>Foo : Symbol(Foo, Decl(a.ts, 0, 6))
14+
15+
export var x = new Foo();
16+
>x : Symbol(x, Decl(a.ts, 1, 10))
17+
>Foo : Symbol(Foo, Decl(a.ts, 0, 6))
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
declare class Foo {
3+
>Foo : Foo
4+
5+
member: string;
6+
>member : string
7+
}
8+
export = Foo;
9+
>Foo : Foo
10+
11+
=== tests/cases/compiler/a.ts ===
12+
import Foo from "./b";
13+
>Foo : typeof Foo
14+
15+
export var x = new Foo();
16+
>x : Foo
17+
>new Foo() : Foo
18+
>Foo : typeof Foo
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//// [tests/cases/compiler/allowSyntheticDefaultImports5.ts] ////
2+
3+
//// [b.d.ts]
4+
declare class Foo {
5+
member: string;
6+
}
7+
export = Foo;
8+
9+
//// [a.ts]
10+
import Foo from "./b";
11+
export var x = new Foo();
12+
13+
14+
//// [a.js]
15+
System.register(["./b"], function(exports_1) {
16+
"use strict";
17+
var b_1;
18+
var x;
19+
return {
20+
setters:[
21+
function (b_1_1) {
22+
b_1 = b_1_1;
23+
}],
24+
execute: function() {
25+
exports_1("x", x = new b_1["default"]());
26+
}
27+
}
28+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
declare class Foo {
3+
>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0))
4+
5+
member: string;
6+
>member : Symbol(member, Decl(b.d.ts, 0, 19))
7+
}
8+
export = Foo;
9+
>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0))
10+
11+
=== tests/cases/compiler/a.ts ===
12+
import Foo from "./b";
13+
>Foo : Symbol(Foo, Decl(a.ts, 0, 6))
14+
15+
export var x = new Foo();
16+
>x : Symbol(x, Decl(a.ts, 1, 10))
17+
>Foo : Symbol(Foo, Decl(a.ts, 0, 6))
18+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
=== tests/cases/compiler/b.d.ts ===
2+
declare class Foo {
3+
>Foo : Foo
4+
5+
member: string;
6+
>member : string
7+
}
8+
export = Foo;
9+
>Foo : Foo
10+
11+
=== tests/cases/compiler/a.ts ===
12+
import Foo from "./b";
13+
>Foo : typeof Foo
14+
15+
export var x = new Foo();
16+
>x : Foo
17+
>new Foo() : Foo
18+
>Foo : typeof Foo
19+

0 commit comments

Comments
 (0)