From ee8b9dfb2176e10e9a7b2ead1628d1940806d71a Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Tue, 2 Aug 2016 11:21:18 -0700 Subject: [PATCH] Fix #10083 - allowSyntheticDefaultImports alters getExternalModuleMember --- src/compiler/checker.ts | 4 +++ .../allowSyntheticDefaultImports10.errors.txt | 17 +++++++++++ .../allowSyntheticDefaultImports10.js | 17 +++++++++++ .../allowSyntheticDefaultImports7.js | 28 +++++++++++++++++++ .../allowSyntheticDefaultImports7.symbols | 22 +++++++++++++++ .../allowSyntheticDefaultImports7.types | 24 ++++++++++++++++ .../allowSyntheticDefaultImports8.errors.txt | 14 ++++++++++ .../allowSyntheticDefaultImports8.js | 28 +++++++++++++++++++ .../allowSyntheticDefaultImports9.js | 17 +++++++++++ .../allowSyntheticDefaultImports9.symbols | 22 +++++++++++++++ .../allowSyntheticDefaultImports9.types | 24 ++++++++++++++++ .../allowSyntheticDefaultImports10.ts | 11 ++++++++ .../compiler/allowSyntheticDefaultImports7.ts | 10 +++++++ .../compiler/allowSyntheticDefaultImports8.ts | 11 ++++++++ .../compiler/allowSyntheticDefaultImports9.ts | 11 ++++++++ 15 files changed, 260 insertions(+) create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports10.errors.txt create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports10.js create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports7.js create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports7.symbols create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports7.types create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports8.errors.txt create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports8.js create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports9.js create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports9.symbols create mode 100644 tests/baselines/reference/allowSyntheticDefaultImports9.types create mode 100644 tests/cases/compiler/allowSyntheticDefaultImports10.ts create mode 100644 tests/cases/compiler/allowSyntheticDefaultImports7.ts create mode 100644 tests/cases/compiler/allowSyntheticDefaultImports8.ts create mode 100644 tests/cases/compiler/allowSyntheticDefaultImports9.ts diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 0a45b61660a1f..c1626ff0b7584 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -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); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports10.errors.txt b/tests/baselines/reference/allowSyntheticDefaultImports10.errors.txt new file mode 100644 index 0000000000000..981f89214e2ca --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports10.errors.txt @@ -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(); + \ No newline at end of file diff --git a/tests/baselines/reference/allowSyntheticDefaultImports10.js b/tests/baselines/reference/allowSyntheticDefaultImports10.js new file mode 100644 index 0000000000000..746997c4c8941 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports10.js @@ -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(); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports7.js b/tests/baselines/reference/allowSyntheticDefaultImports7.js new file mode 100644 index 0000000000000..51cc63e6e11e7 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports7.js @@ -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(); + } + } +}); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports7.symbols b/tests/baselines/reference/allowSyntheticDefaultImports7.symbols new file mode 100644 index 0000000000000..51155dd69dfd8 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports7.symbols @@ -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)) + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports7.types b/tests/baselines/reference/allowSyntheticDefaultImports7.types new file mode 100644 index 0000000000000..69ce39e6dc6e6 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports7.types @@ -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 + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports8.errors.txt b/tests/baselines/reference/allowSyntheticDefaultImports8.errors.txt new file mode 100644 index 0000000000000..acb584f8fd9e0 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports8.errors.txt @@ -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(); \ No newline at end of file diff --git a/tests/baselines/reference/allowSyntheticDefaultImports8.js b/tests/baselines/reference/allowSyntheticDefaultImports8.js new file mode 100644 index 0000000000000..3c3c5fa8d28c7 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports8.js @@ -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(); + } + } +}); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports9.js b/tests/baselines/reference/allowSyntheticDefaultImports9.js new file mode 100644 index 0000000000000..15810ccaaf703 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports9.js @@ -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(); diff --git a/tests/baselines/reference/allowSyntheticDefaultImports9.symbols b/tests/baselines/reference/allowSyntheticDefaultImports9.symbols new file mode 100644 index 0000000000000..51155dd69dfd8 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports9.symbols @@ -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)) + diff --git a/tests/baselines/reference/allowSyntheticDefaultImports9.types b/tests/baselines/reference/allowSyntheticDefaultImports9.types new file mode 100644 index 0000000000000..69ce39e6dc6e6 --- /dev/null +++ b/tests/baselines/reference/allowSyntheticDefaultImports9.types @@ -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 + diff --git a/tests/cases/compiler/allowSyntheticDefaultImports10.ts b/tests/cases/compiler/allowSyntheticDefaultImports10.ts new file mode 100644 index 0000000000000..6b50bae96d8fe --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports10.ts @@ -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(); \ No newline at end of file diff --git a/tests/cases/compiler/allowSyntheticDefaultImports7.ts b/tests/cases/compiler/allowSyntheticDefaultImports7.ts new file mode 100644 index 0000000000000..2da05e4678c7f --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports7.ts @@ -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(); \ No newline at end of file diff --git a/tests/cases/compiler/allowSyntheticDefaultImports8.ts b/tests/cases/compiler/allowSyntheticDefaultImports8.ts new file mode 100644 index 0000000000000..3a213ad80235f --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports8.ts @@ -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(); \ No newline at end of file diff --git a/tests/cases/compiler/allowSyntheticDefaultImports9.ts b/tests/cases/compiler/allowSyntheticDefaultImports9.ts new file mode 100644 index 0000000000000..8da66f33adf56 --- /dev/null +++ b/tests/cases/compiler/allowSyntheticDefaultImports9.ts @@ -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(); \ No newline at end of file