diff --git a/lib/6to5/file.js b/lib/6to5/file.js index f222387830..da5b168fa3 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -35,7 +35,8 @@ File.helpers = [ "async-to-generator", "interop-require-wildcard", "typeof", - "exports-wildcard" + "exports-wildcard", + "extends" ]; File.excludeHelpersFromRuntime = [ diff --git a/lib/6to5/transformation/modules/common.js b/lib/6to5/transformation/modules/common.js index 7364d6a3fd..7fc557d6c0 100644 --- a/lib/6to5/transformation/modules/common.js +++ b/lib/6to5/transformation/modules/common.js @@ -83,10 +83,12 @@ CommonJSFormatter.prototype.exportDeclaration = function (node, nodes) { // this export isn't a function so we can't hoist it to the top so we need to set it // at the very end of the file with something like: // - // module.exports = Object.assign(exports["default"], exports) + // module.exports = _extends(exports["default"], exports) // - assign = util.template("common-export-default-assign", true); + assign = util.template("common-export-default-assign", { + EXTENDS_HELPER: this.file.addHelper("extends") + }, true); assign._blockHoist = 0; nodes.push(assign); diff --git a/lib/6to5/transformation/templates/common-export-default-assign.js b/lib/6to5/transformation/templates/common-export-default-assign.js index 17a1f96968..557c221edb 100644 --- a/lib/6to5/transformation/templates/common-export-default-assign.js +++ b/lib/6to5/transformation/templates/common-export-default-assign.js @@ -1 +1 @@ -module.exports = Object.assign(exports["default"], exports); +module.exports = EXTENDS_HELPER(exports["default"], exports); diff --git a/lib/6to5/transformation/templates/extends.js b/lib/6to5/transformation/templates/extends.js new file mode 100644 index 0000000000..b75e9a76a9 --- /dev/null +++ b/lib/6to5/transformation/templates/extends.js @@ -0,0 +1,9 @@ +(function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var key in source) { + target[key] = source[key]; + } + } + return target; +}) diff --git a/lib/6to5/transformation/transformers/es7-object-spread.js b/lib/6to5/transformation/transformers/es7-object-spread.js index 47469a35bc..81156bfb1f 100644 --- a/lib/6to5/transformation/transformers/es7-object-spread.js +++ b/lib/6to5/transformation/transformers/es7-object-spread.js @@ -4,7 +4,7 @@ var t = require("../../types"); exports.experimental = true; -exports.ObjectExpression = function (node) { +exports.ObjectExpression = function (node, parent, file) { var hasSpread = false; var i; var prop; @@ -42,5 +42,5 @@ exports.ObjectExpression = function (node) { args.unshift(t.objectExpression([])); } - return t.callExpression(t.memberExpression(t.identifier("Object"), t.identifier("assign")), args); + return t.callExpression(file.addHelper("extends"), args); }; diff --git a/lib/6to5/transformation/transformers/optional-proto-to-assign.js b/lib/6to5/transformation/transformers/optional-proto-to-assign.js index 5de64efb34..6622193759 100644 --- a/lib/6to5/transformation/transformers/optional-proto-to-assign.js +++ b/lib/6to5/transformation/transformers/optional-proto-to-assign.js @@ -43,7 +43,7 @@ exports.ExpressionStatement = function (node, parent, file) { } }; -exports.ObjectExpression = function (node) { +exports.ObjectExpression = function (node, parent, file) { var proto; for (var i in node.properties) { @@ -58,6 +58,6 @@ exports.ObjectExpression = function (node) { if (proto) { var args = [t.objectExpression([]), proto]; if (node.properties.length) args.push(node); - return t.callExpression(OBJECT_ASSIGN_MEMBER, args); + return t.callExpression(file.addHelper("extends"), args); } }; diff --git a/test/fixtures/transformation/es6-modules-common/exports-default-non-function/expected.js b/test/fixtures/transformation/es6-modules-common/exports-default-non-function/expected.js index 8922e90b48..a8d178e952 100644 --- a/test/fixtures/transformation/es6-modules-common/exports-default-non-function/expected.js +++ b/test/fixtures/transformation/es6-modules-common/exports-default-non-function/expected.js @@ -1,6 +1,17 @@ "use strict"; +var _extends = function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var key in source) { + target[key] = source[key]; + } + } + + return target; +}; + exports.Cachier = Cachier; exports["default"] = new Cachier(); function Cachier(databaseName) {} -module.exports = Object.assign(exports["default"], exports); +module.exports = _extends(exports["default"], exports); diff --git a/test/fixtures/transformation/es7-object-spread/assignment/expected.js b/test/fixtures/transformation/es7-object-spread/assignment/expected.js index bd7dab2734..5671a88356 100644 --- a/test/fixtures/transformation/es7-object-spread/assignment/expected.js +++ b/test/fixtures/transformation/es7-object-spread/assignment/expected.js @@ -1,3 +1,14 @@ "use strict"; -z = Object.assign({ x: x }, y); +var _extends = function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var key in source) { + target[key] = source[key]; + } + } + + return target; +}; + +z = _extends({ x: x }, y); diff --git a/test/fixtures/transformation/es7-object-spread/expression/expected.js b/test/fixtures/transformation/es7-object-spread/expression/expected.js index 856b5c1f8c..6080159f0f 100644 --- a/test/fixtures/transformation/es7-object-spread/expression/expected.js +++ b/test/fixtures/transformation/es7-object-spread/expression/expected.js @@ -1,3 +1,14 @@ "use strict"; -Object.assign({ x: x }, y, { a: a }, b, { c: c }); +var _extends = function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var key in source) { + target[key] = source[key]; + } + } + + return target; +}; + +_extends({ x: x }, y, { a: a }, b, { c: c }); diff --git a/test/fixtures/transformation/es7-object-spread/variable-declaration/expected.js b/test/fixtures/transformation/es7-object-spread/variable-declaration/expected.js index 2522635156..8fee368e87 100644 --- a/test/fixtures/transformation/es7-object-spread/variable-declaration/expected.js +++ b/test/fixtures/transformation/es7-object-spread/variable-declaration/expected.js @@ -1,3 +1,14 @@ "use strict"; -var z = Object.assign({}, x); +var _extends = function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var key in source) { + target[key] = source[key]; + } + } + + return target; +}; + +var z = _extends({}, x); diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js b/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js deleted file mode 100644 index a16dd23cb9..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/actual.js +++ /dev/null @@ -1 +0,0 @@ -var z = { ...x }; diff --git a/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js b/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js deleted file mode 100644 index 940d6e9d13..0000000000 --- a/test/fixtures/transformation/optional-core-aliasing/es7-object-spread-rest/expected.js +++ /dev/null @@ -1,9 +0,0 @@ -"use strict"; - -var _interopRequire = function (obj) { - return obj && (obj["default"] || obj); -}; - -var _core = _interopRequire(require("core-js/library")); - -var z = _core.Object.assign({}, x); diff --git a/test/fixtures/transformation/optional-proto-to-assign/object-literal/expected.js b/test/fixtures/transformation/optional-proto-to-assign/object-literal/expected.js index 31b92ba185..49087a66e7 100644 --- a/test/fixtures/transformation/optional-proto-to-assign/object-literal/expected.js +++ b/test/fixtures/transformation/optional-proto-to-assign/object-literal/expected.js @@ -1,10 +1,21 @@ "use strict"; -var foo = Object.assign({}, bar); +var _extends = function (target) { + for (var i = 1; i < arguments.length; i++) { + var source = arguments[i]; + for (var key in source) { + target[key] = source[key]; + } + } -var foo = Object.assign({}, bar, { + return target; +}; + +var foo = _extends({}, bar); + +var foo = _extends({}, bar, { bar: "foo" }); -var foo = Object.assign({}, bar, { +var foo = _extends({}, bar, { bar: "foo" });