diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index de68f4f217..d4cdd50180 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -46,6 +46,7 @@ namespace ts { const compilerOptions = host.getCompilerOptions(); const languageVersion = compilerOptions.target || ScriptTarget.ES3; const modulekind = compilerOptions.module ? compilerOptions.module : languageVersion === ScriptTarget.ES6 ? ModuleKind.ES6 : ModuleKind.None; + const allowSyntheticDefaultImports = typeof compilerOptions.allowSyntheticDefaultImports !== "undefined" ? compilerOptions.allowSyntheticDefaultImports : modulekind === ModuleKind.System; const emitResolver = createResolver(); @@ -768,9 +769,12 @@ namespace ts { const moduleSymbol = resolveExternalModuleName(node, (node.parent).moduleSpecifier); if (moduleSymbol) { const exportDefaultSymbol = resolveSymbol(moduleSymbol.exports["default"]); - if (!exportDefaultSymbol) { + if (!exportDefaultSymbol && !allowSyntheticDefaultImports) { error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol)); } + else if (!exportDefaultSymbol && allowSyntheticDefaultImports) { + return resolveSymbol(moduleSymbol.exports["export="]) || resolveSymbol(moduleSymbol); + } return exportDefaultSymbol; } } diff --git a/src/compiler/commandLineParser.ts b/src/compiler/commandLineParser.ts index 96b6bdeef9..034b7022e8 100644 --- a/src/compiler/commandLineParser.ts +++ b/src/compiler/commandLineParser.ts @@ -280,10 +280,15 @@ namespace ts { type: "boolean", description: Diagnostics.Disallow_inconsistently_cased_references_to_the_same_file }, + { + name: "allowSyntheticDefaultImports", + type: "boolean", + description: Diagnostics.Allow_default_imports_from_modules_with_no_default_export_This_does_not_affect_code_emit_just_typechecking + }, { name: "allowJs", type: "boolean", - description: Diagnostics.Allow_javascript_files_to_be_compiled, + description: Diagnostics.Allow_javascript_files_to_be_compiled } ]; @@ -488,7 +493,7 @@ namespace ts { fileNames: getFileNames(), errors }; - + function getFileNames(): string[] { let fileNames: string[] = []; if (hasProperty(json, "files")) { diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 780f95d7f8..6266170637 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -2117,6 +2117,10 @@ "category": "Message", "code": 6010 }, + "Allow default imports from modules with no default export. This does not affect code emit, just typechecking.": { + "category": "Message", + "code": 6011 + }, "Specify ECMAScript target version: 'ES3' (default), 'ES5', or 'ES2015' (experimental)": { "category": "Message", "code": 6015 diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 8af2746eb3..f8b26ddaa6 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -2372,6 +2372,7 @@ namespace ts { noImplicitReturns?: boolean; noFallthroughCasesInSwitch?: boolean; forceConsistentCasingInFileNames?: boolean; + allowSyntheticDefaultImports?: boolean; allowJs?: boolean; /* @internal */ stripInternal?: boolean; diff --git a/tests/baselines/reference/allowSyntheticDefaultImports1.js b/tests/baselines/reference/allowSyntheticDefaultImports1.js new file mode 100644 index 0000000000..699880f930 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports1.js @@ -0,0 +1,24 @@ +//// [tests/cases/compiler/allowSyntheticDefaultImports1.ts] //// + +//// [a.ts] +import Namespace from "./b"; +export var x = new Namespace.Foo(); + +//// [b.ts] +export class Foo { + member: string; +} + + +//// [b.js] +"use strict"; +var Foo = (function () { + function Foo() { + } + return Foo; +})(); +exports.Foo = Foo; +//// [a.js] +"use strict"; +var b_1 = require("./b"); +exports.x = new b_1["default"].Foo(); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports1.symbols b/tests/baselines/reference/allowSyntheticDefaultImports1.symbols new file mode 100644 index 0000000000..d88a79e1bf --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports1.symbols @@ -0,0 +1,18 @@ +=== tests/cases/compiler/a.ts === +import Namespace from "./b"; +>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6)) + +export var x = new Namespace.Foo(); +>x : Symbol(x, Decl(a.ts, 1, 10)) +>Namespace.Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0)) +>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6)) +>Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0)) + +=== tests/cases/compiler/b.ts === +export class Foo { +>Foo : Symbol(Foo, Decl(b.ts, 0, 0)) + + member: string; +>member : Symbol(member, Decl(b.ts, 0, 18)) +} + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports1.types b/tests/baselines/reference/allowSyntheticDefaultImports1.types new file mode 100644 index 0000000000..c2265d7611 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports1.types @@ -0,0 +1,19 @@ +=== tests/cases/compiler/a.ts === +import Namespace from "./b"; +>Namespace : typeof Namespace + +export var x = new Namespace.Foo(); +>x : Namespace.Foo +>new Namespace.Foo() : Namespace.Foo +>Namespace.Foo : typeof Namespace.Foo +>Namespace : typeof Namespace +>Foo : typeof Namespace.Foo + +=== tests/cases/compiler/b.ts === +export class Foo { +>Foo : Foo + + member: string; +>member : string +} + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports2.js b/tests/baselines/reference/allowSyntheticDefaultImports2.js new file mode 100644 index 0000000000..d21750c65f --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports2.js @@ -0,0 +1,42 @@ +//// [tests/cases/compiler/allowSyntheticDefaultImports2.ts] //// + +//// [a.ts] +import Namespace from "./b"; +export var x = new Namespace.Foo(); + +//// [b.ts] +export class Foo { + member: string; +} + +//// [b.js] +System.register([], function(exports_1) { + "use strict"; + var Foo; + return { + setters:[], + execute: function() { + Foo = (function () { + function Foo() { + } + return Foo; + })(); + exports_1("Foo", Foo); + } + } +}); +//// [a.js] +System.register(["./b"], function(exports_1) { + "use strict"; + var b_1; + var x; + return { + setters:[ + function (b_1_1) { + b_1 = b_1_1; + }], + execute: function() { + exports_1("x", x = new b_1["default"].Foo()); + } + } +}); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports2.symbols b/tests/baselines/reference/allowSyntheticDefaultImports2.symbols new file mode 100644 index 0000000000..cea6145fbd --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports2.symbols @@ -0,0 +1,17 @@ +=== tests/cases/compiler/a.ts === +import Namespace from "./b"; +>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6)) + +export var x = new Namespace.Foo(); +>x : Symbol(x, Decl(a.ts, 1, 10)) +>Namespace.Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0)) +>Namespace : Symbol(Namespace, Decl(a.ts, 0, 6)) +>Foo : Symbol(Namespace.Foo, Decl(b.ts, 0, 0)) + +=== tests/cases/compiler/b.ts === +export class Foo { +>Foo : Symbol(Foo, Decl(b.ts, 0, 0)) + + member: string; +>member : Symbol(member, Decl(b.ts, 0, 18)) +} diff --git a/tests/baselines/reference/allowSyntheticDefaultImports2.types b/tests/baselines/reference/allowSyntheticDefaultImports2.types new file mode 100644 index 0000000000..c40fbc7e98 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports2.types @@ -0,0 +1,18 @@ +=== tests/cases/compiler/a.ts === +import Namespace from "./b"; +>Namespace : typeof Namespace + +export var x = new Namespace.Foo(); +>x : Namespace.Foo +>new Namespace.Foo() : Namespace.Foo +>Namespace.Foo : typeof Namespace.Foo +>Namespace : typeof Namespace +>Foo : typeof Namespace.Foo + +=== tests/cases/compiler/b.ts === +export class Foo { +>Foo : Foo + + member: string; +>member : string +} diff --git a/tests/baselines/reference/allowSyntheticDefaultImports3.errors.txt b/tests/baselines/reference/allowSyntheticDefaultImports3.errors.txt new file mode 100644 index 0000000000..551355e5cc --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports3.errors.txt @@ -0,0 +1,14 @@ +tests/cases/compiler/a.ts(1,8): error TS1192: Module '"tests/cases/compiler/b"' has no default export. + + +==== tests/cases/compiler/a.ts (1 errors) ==== + import Namespace from "./b"; + ~~~~~~~~~ +!!! error TS1192: Module '"tests/cases/compiler/b"' has no default export. + export var x = new Namespace.Foo(); + +==== tests/cases/compiler/b.ts (0 errors) ==== + export class Foo { + member: string; + } + \ No newline at end of file diff --git a/tests/baselines/reference/allowSyntheticDefaultImports3.js b/tests/baselines/reference/allowSyntheticDefaultImports3.js new file mode 100644 index 0000000000..8864a28e79 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports3.js @@ -0,0 +1,43 @@ +//// [tests/cases/compiler/allowSyntheticDefaultImports3.ts] //// + +//// [a.ts] +import Namespace from "./b"; +export var x = new Namespace.Foo(); + +//// [b.ts] +export class Foo { + member: string; +} + + +//// [b.js] +System.register([], function(exports_1) { + "use strict"; + var Foo; + return { + setters:[], + execute: function() { + Foo = (function () { + function Foo() { + } + return Foo; + })(); + exports_1("Foo", Foo); + } + } +}); +//// [a.js] +System.register(["./b"], function(exports_1) { + "use strict"; + var b_1; + var x; + return { + setters:[ + function (b_1_1) { + b_1 = b_1_1; + }], + execute: function() { + exports_1("x", x = new b_1["default"].Foo()); + } + } +}); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports4.js b/tests/baselines/reference/allowSyntheticDefaultImports4.js new file mode 100644 index 0000000000..747f59dc90 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports4.js @@ -0,0 +1,17 @@ +//// [tests/cases/compiler/allowSyntheticDefaultImports4.ts] //// + +//// [b.d.ts] +declare class Foo { + member: string; +} +export = Foo; + +//// [a.ts] +import Foo from "./b"; +export var x = new Foo(); + + +//// [a.js] +"use strict"; +var b_1 = require("./b"); +exports.x = new b_1["default"](); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports4.symbols b/tests/baselines/reference/allowSyntheticDefaultImports4.symbols new file mode 100644 index 0000000000..8edad006c2 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports4.symbols @@ -0,0 +1,18 @@ +=== tests/cases/compiler/b.d.ts === +declare class Foo { +>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0)) + + member: string; +>member : Symbol(member, Decl(b.d.ts, 0, 19)) +} +export = Foo; +>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0)) + +=== tests/cases/compiler/a.ts === +import Foo from "./b"; +>Foo : Symbol(Foo, Decl(a.ts, 0, 6)) + +export var x = new Foo(); +>x : Symbol(x, Decl(a.ts, 1, 10)) +>Foo : Symbol(Foo, Decl(a.ts, 0, 6)) + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports4.types b/tests/baselines/reference/allowSyntheticDefaultImports4.types new file mode 100644 index 0000000000..5922abeb43 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports4.types @@ -0,0 +1,19 @@ +=== tests/cases/compiler/b.d.ts === +declare class Foo { +>Foo : Foo + + member: string; +>member : string +} +export = Foo; +>Foo : Foo + +=== tests/cases/compiler/a.ts === +import Foo from "./b"; +>Foo : typeof Foo + +export var x = new Foo(); +>x : Foo +>new Foo() : Foo +>Foo : typeof Foo + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports5.js b/tests/baselines/reference/allowSyntheticDefaultImports5.js new file mode 100644 index 0000000000..c9121512b6 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports5.js @@ -0,0 +1,28 @@ +//// [tests/cases/compiler/allowSyntheticDefaultImports5.ts] //// + +//// [b.d.ts] +declare class Foo { + member: string; +} +export = Foo; + +//// [a.ts] +import Foo from "./b"; +export var x = new Foo(); + + +//// [a.js] +System.register(["./b"], function(exports_1) { + "use strict"; + var b_1; + var x; + return { + setters:[ + function (b_1_1) { + b_1 = b_1_1; + }], + execute: function() { + exports_1("x", x = new b_1["default"]()); + } + } +}); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports5.symbols b/tests/baselines/reference/allowSyntheticDefaultImports5.symbols new file mode 100644 index 0000000000..8edad006c2 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports5.symbols @@ -0,0 +1,18 @@ +=== tests/cases/compiler/b.d.ts === +declare class Foo { +>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0)) + + member: string; +>member : Symbol(member, Decl(b.d.ts, 0, 19)) +} +export = Foo; +>Foo : Symbol(Foo, Decl(b.d.ts, 0, 0)) + +=== tests/cases/compiler/a.ts === +import Foo from "./b"; +>Foo : Symbol(Foo, Decl(a.ts, 0, 6)) + +export var x = new Foo(); +>x : Symbol(x, Decl(a.ts, 1, 10)) +>Foo : Symbol(Foo, Decl(a.ts, 0, 6)) + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports5.types b/tests/baselines/reference/allowSyntheticDefaultImports5.types new file mode 100644 index 0000000000..5922abeb43 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports5.types @@ -0,0 +1,19 @@ +=== tests/cases/compiler/b.d.ts === +declare class Foo { +>Foo : Foo + + member: string; +>member : string +} +export = Foo; +>Foo : Foo + +=== tests/cases/compiler/a.ts === +import Foo from "./b"; +>Foo : typeof Foo + +export var x = new Foo(); +>x : Foo +>new Foo() : Foo +>Foo : typeof Foo + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports6.errors.txt b/tests/baselines/reference/allowSyntheticDefaultImports6.errors.txt new file mode 100644 index 0000000000..80b9cf8d07 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports6.errors.txt @@ -0,0 +1,15 @@ +tests/cases/compiler/a.ts(1,8): error TS1192: Module '"tests/cases/compiler/b"' has no default export. + + +==== tests/cases/compiler/b.d.ts (0 errors) ==== + declare class Foo { + member: string; + } + export = Foo; + +==== tests/cases/compiler/a.ts (1 errors) ==== + import Foo from "./b"; + ~~~ +!!! error TS1192: Module '"tests/cases/compiler/b"' has no default export. + export var x = new Foo(); + \ No newline at end of file diff --git a/tests/baselines/reference/allowSyntheticDefaultImports6.js b/tests/baselines/reference/allowSyntheticDefaultImports6.js new file mode 100644 index 0000000000..64d52e70af --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports6.js @@ -0,0 +1,28 @@ +//// [tests/cases/compiler/allowSyntheticDefaultImports6.ts] //// + +//// [b.d.ts] +declare class Foo { + member: string; +} +export = Foo; + +//// [a.ts] +import Foo from "./b"; +export var x = new Foo(); + + +//// [a.js] +System.register(["./b"], function(exports_1) { + "use strict"; + var b_1; + var x; + return { + setters:[ + function (b_1_1) { + b_1 = b_1_1; + }], + execute: function() { + exports_1("x", x = new b_1["default"]()); + } + } +}); diff --git a/tests/cases/compiler/allowSyntheticDefaultImports1.ts b/tests/cases/compiler/allowSyntheticDefaultImports1.ts new file mode 100644 index 0000000000..4793da7913 --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports1.ts @@ -0,0 +1,10 @@ +// @allowSyntheticDefaultImports: true +// @module: commonjs +// @Filename: a.ts +import Namespace from "./b"; +export var x = new Namespace.Foo(); + +// @Filename: b.ts +export class Foo { + member: string; +} diff --git a/tests/cases/compiler/allowSyntheticDefaultImports2.ts b/tests/cases/compiler/allowSyntheticDefaultImports2.ts new file mode 100644 index 0000000000..8fa004be38 --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports2.ts @@ -0,0 +1,9 @@ +// @module: system +// @Filename: a.ts +import Namespace from "./b"; +export var x = new Namespace.Foo(); + +// @Filename: b.ts +export class Foo { + member: string; +} \ No newline at end of file diff --git a/tests/cases/compiler/allowSyntheticDefaultImports3.ts b/tests/cases/compiler/allowSyntheticDefaultImports3.ts new file mode 100644 index 0000000000..65d11c892c --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports3.ts @@ -0,0 +1,10 @@ +// @allowSyntheticDefaultImports: false +// @module: system +// @Filename: a.ts +import Namespace from "./b"; +export var x = new Namespace.Foo(); + +// @Filename: b.ts +export class Foo { + member: string; +} diff --git a/tests/cases/compiler/allowSyntheticDefaultImports4.ts b/tests/cases/compiler/allowSyntheticDefaultImports4.ts new file mode 100644 index 0000000000..1d26c6277c --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports4.ts @@ -0,0 +1,11 @@ +// @allowSyntheticDefaultImports: true +// @module: commonjs +// @Filename: b.d.ts +declare class Foo { + member: string; +} +export = Foo; + +// @Filename: a.ts +import Foo from "./b"; +export var x = new Foo(); diff --git a/tests/cases/compiler/allowSyntheticDefaultImports5.ts b/tests/cases/compiler/allowSyntheticDefaultImports5.ts new file mode 100644 index 0000000000..30ebba6714 --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports5.ts @@ -0,0 +1,10 @@ +// @module: system +// @Filename: b.d.ts +declare class Foo { + member: string; +} +export = Foo; + +// @Filename: a.ts +import Foo from "./b"; +export var x = new Foo(); diff --git a/tests/cases/compiler/allowSyntheticDefaultImports6.ts b/tests/cases/compiler/allowSyntheticDefaultImports6.ts new file mode 100644 index 0000000000..ff0300e30b --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports6.ts @@ -0,0 +1,11 @@ +// @allowSyntheticDefaultImports: false +// @module: system +// @Filename: b.d.ts +declare class Foo { + member: string; +} +export = Foo; + +// @Filename: a.ts +import Foo from "./b"; +export var x = new Foo();