Merge pull request #5588 from Microsoft/reexport-default-export

Reexport default export
This commit is contained in:
Nathan Shively-Sanders 2015-11-11 11:29:31 -08:00
commit c1ea85fa72
5 changed files with 113 additions and 22 deletions

View file

@ -486,9 +486,19 @@ namespace ts {
if (location.kind === SyntaxKind.SourceFile ||
(location.kind === SyntaxKind.ModuleDeclaration && (<ModuleDeclaration>location).name.kind === SyntaxKind.StringLiteral)) {
// It's an external module. Because of module/namespace merging, a module's exports are in scope,
// yet we never want to treat an export specifier as putting a member in scope. Therefore,
// if the name we find is purely an export specifier, it is not actually considered in scope.
// It's an external module. First see if the module has an export default and if the local
// name of that export default matches.
if (result = moduleExports["default"]) {
const localSymbol = getLocalSymbolForExportDefault(result);
if (localSymbol && (result.flags & meaning) && localSymbol.name === name) {
break loop;
}
result = undefined;
}
// Because of module/namespace merging, a module's exports are in scope,
// yet we never want to treat an export specifier as putting a member in scope.
// Therefore, if the name we find is purely an export specifier, it is not actually considered in scope.
// Two things to note about this:
// 1. We have to check this without calling getSymbol. The problem with calling getSymbol
// on an export specifier is that it might find the export specifier itself, and try to
@ -502,13 +512,6 @@ namespace ts {
getDeclarationOfKind(moduleExports[name], SyntaxKind.ExportSpecifier)) {
break;
}
result = moduleExports["default"];
const localSymbol = getLocalSymbolForExportDefault(result);
if (result && localSymbol && (result.flags & meaning) && localSymbol.name === name) {
break loop;
}
result = undefined;
}
if (result = getSymbol(moduleExports, name, meaning & SymbolFlags.ModuleMember)) {

View file

@ -0,0 +1,27 @@
//// [tests/cases/conformance/es6/modules/reExportDefaultExport.ts] ////
//// [m1.ts]
export default function f() {
}
export {f};
//// [m2.ts]
import foo from "./m1";
import {f} from "./m1";
f();
foo();
//// [m1.js]
function f() {
}
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = f;
exports.f = f;
//// [m2.js]
var m1_1 = require("./m1");
var m1_2 = require("./m1");
m1_2.f();
m1_1.default();

View file

@ -0,0 +1,22 @@
=== tests/cases/conformance/es6/modules/m1.ts ===
export default function f() {
>f : Symbol(f, Decl(m1.ts, 0, 0))
}
export {f};
>f : Symbol(f, Decl(m1.ts, 3, 8))
=== tests/cases/conformance/es6/modules/m2.ts ===
import foo from "./m1";
>foo : Symbol(foo, Decl(m2.ts, 0, 6))
import {f} from "./m1";
>f : Symbol(f, Decl(m2.ts, 1, 8))
f();
>f : Symbol(f, Decl(m2.ts, 1, 8))
foo();
>foo : Symbol(foo, Decl(m2.ts, 0, 6))

View file

@ -0,0 +1,24 @@
=== tests/cases/conformance/es6/modules/m1.ts ===
export default function f() {
>f : () => void
}
export {f};
>f : () => void
=== tests/cases/conformance/es6/modules/m2.ts ===
import foo from "./m1";
>foo : () => void
import {f} from "./m1";
>f : () => void
f();
>f() : void
>f : () => void
foo();
>foo() : void
>foo : () => void

View file

@ -0,0 +1,15 @@
// @module: commonjs
// @target: ES5
// @filename: m1.ts
export default function f() {
}
export {f};
// @filename: m2.ts
import foo from "./m1";
import {f} from "./m1";
f();
foo();