Skip to content

Commit

Permalink
Use a helper for the SameValue check
Browse files Browse the repository at this point in the history
  • Loading branch information
Diogo Franco (Kovensky) committed Dec 12, 2016
1 parent 65680c9 commit 70d0f9a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 8 additions & 0 deletions packages/babel-helpers/src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,14 @@ helpers.interopRequireWildcard = template(`
})
`);

// based on core-js's SameValue implementation
// https://github.com/zloirock/core-js/blob/693767b/modules/_same-value.js
helpers.sameValue = template(`
(function (x, y) {
return x === y ? x !== 0 || 1 / x === 1 / y : x != x && y != y;
})
`);

helpers.specRequireInterop = template(`
(function (obj) {
if (obj && obj.__esModule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,10 @@ const specBuildOwnExports = template(`
const $0 = Object.keys($1)
`);

// The SameValue check between own and other was adapted from core-js's
// Object.is implementation.
// https://github.com/zloirock/core-js/blob/master/modules/_same-value.js
const specBuildNamespaceSpread = template(`
Object.keys(OBJECT).forEach(function (key) {
if (key === "__esModule" || key === "default" || OWN_EXPORTS.indexOf(key) >= 0) return;
if (key in EXPORTS) {
var own = EXPORTS[key], other = OBJECT[key]
if (own === other ? own !== 0 || 1 / own === 1 / other : own != own && other != other) return;
}
if (key in EXPORTS && SAME_VALUE(EXPORTS[key], OBJECT[key])) return;
Object.defineProperty(EXPORTS, key, {
enumerable: true,
get() {
Expand Down Expand Up @@ -645,6 +639,7 @@ export default function () {
exportNode = specBuildNamespaceSpread({
EXPORTS: exportsObj,
OWN_EXPORTS: ownExportsUid,
SAME_VALUE: this.addHelper("sameValue"),
OBJECT: addRequire(path.node.source.value, 3)
});
exportNode._blockHoist = 3;
Expand Down

0 comments on commit 70d0f9a

Please sign in to comment.