Skip to content

Commit

Permalink
Properly skip "default" exports in export * from "module".
Browse files Browse the repository at this point in the history
benjamn authored and taylorzane committed Feb 26, 2019
1 parent 87f5444 commit b7ad1db
Showing 6 changed files with 22 additions and 24 deletions.
12 changes: 9 additions & 3 deletions lib/runtime/index.js
Original file line number Diff line number Diff line change
@@ -112,11 +112,17 @@ function runSetters(valueToPassThrough) {
}

// Returns a function that takes a namespace object and copies the
// properties of the namespace to module.exports, which is useful for
// implementing `export * from "module"` syntax.
// properties of the namespace to module.exports, excluding any "default"
// property, which is useful for implementing `export * from "module"`.
function moduleMakeNsSetter() {
var module = this;
// Discussion of why the "default" property is skipped:
// https://github.com/tc39/ecma262/issues/948
return function (namespace) {
utils.assign(module.exports, namespace);
Object.keys(namespace).forEach(function (key) {
if (key !== "default") {
utils.copyKey(key, module.exports, namespace);
}
});
};
}
20 changes: 0 additions & 20 deletions lib/runtime/utils.js
Original file line number Diff line number Diff line change
@@ -15,26 +15,6 @@ var useGetOwnPropDesc =
typeof Object.getOwnPropertyDescriptor === "function";
var hasOwn = Object.prototype.hasOwnProperty;

exports.assign = function (target) {
var argCount = arguments.length;
var sourcesByKey = Object.create(null);

for (var i = 0; i < argCount; ++i) {
var source = arguments[i];
if (isObjectLike(source)) {
Object.keys(source).forEach(function (key) {
sourcesByKey[key] = source;
});
}
}

Object.keys(sourcesByKey).forEach(function (key) {
copyKey(key, target, sourcesByKey[key]);
});

return target;
};

function copyKey(key, target, source) {
if (useGetOwnPropDesc) {
var desc = Object.getOwnPropertyDescriptor(source, key);
2 changes: 1 addition & 1 deletion test/export/all.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "../misc/abc";
export default "default";
export * from "../misc/abc";
1 change: 1 addition & 0 deletions test/export/def.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const d = "d";
export const e = "e";
export const f = "f";
export default "default";
5 changes: 5 additions & 0 deletions test/misc-tests.js
Original file line number Diff line number Diff line change
@@ -19,6 +19,11 @@ describe("spec compliance", () => {
import value from "./export/cycle-a";
assert.equal(value, true);
});

it("should not export the default binding of namespace exports", () => {
import * as ns from "./misc/export-all.js";
ns.check();
});
});

describe("built-in modules", () => {
6 changes: 6 additions & 0 deletions test/misc/export-all.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import assert from "assert"
import * as ns from "../export/all-multiple.js"

export function check() {
assert.ok(! ("default" in ns))
}

0 comments on commit b7ad1db

Please sign in to comment.