diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 82269b4a05..479e233ee6 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -29,6 +29,7 @@ File.helpers = [ "object-without-properties", "has-own", "slice", + "bind", "define-property", "async-to-generator", "interop-require-wildcard", diff --git a/lib/6to5/transformation/templates/bind.js b/lib/6to5/transformation/templates/bind.js new file mode 100644 index 0000000000..abe1c0668a --- /dev/null +++ b/lib/6to5/transformation/templates/bind.js @@ -0,0 +1 @@ +Function.prototype.bind diff --git a/lib/6to5/transformation/transformers/es6-spread.js b/lib/6to5/transformation/transformers/es6-spread.js index 214a765f03..2f544fe989 100644 --- a/lib/6to5/transformation/transformers/es6-spread.js +++ b/lib/6to5/transformation/transformers/es6-spread.js @@ -1,4 +1,5 @@ var t = require("../../types"); +var _ = require("lodash"); var getSpreadLiteral = function (spread, file) { return file.toArray(spread.argument); @@ -98,7 +99,14 @@ exports.NewExpression = function (node, parent, file) { var args = node.arguments; if (!hasSpread(args)) return; + var nativeType = t.isIdentifier(node.callee) && _.contains(t.NATIVE_TYPES_NAMES, node.callee.name); + var nodes = build(args, file); + + if (nativeType) { + nodes.unshift(t.arrayExpression([t.literal(null)])); + } + var first = nodes.shift(); if (nodes.length) { @@ -107,5 +115,15 @@ exports.NewExpression = function (node, parent, file) { args = first; } - return t.callExpression(file.addHelper("apply-constructor"), [node.callee, args]); + if (nativeType) { + return t.newExpression( + t.callExpression( + t.memberExpression(file.addHelper("bind"), t.identifier("apply")), + [node.callee, args] + ), + [] + ); + } else { + return t.callExpression(file.addHelper("apply-constructor"), [node.callee, args]); + } };