From 1a1e6bc3ba87b2dda9e09dd4b002a0ff9860b3ba Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 25 Nov 2014 23:56:38 +1100 Subject: [PATCH] remove arguments-to-array helper --- lib/6to5/file.js | 12 +++------ lib/6to5/templates/arguments-to-array.js | 7 ----- .../transformers/es6-rest-parameters.js | 7 ++--- .../accessing-super-class/expected.js | 26 +++++++------------ .../arrow-functions/expected.js | 12 ++------- .../es6-rest-parameters/multiple/expected.js | 14 +++------- .../es6-rest-parameters/single/expected.js | 14 +++------- .../es6-spread/arguments/expected.js | 12 ++------- .../misc/custom-runtime/expected.js | 2 +- .../transformation/misc/runtime/expected.js | 2 +- 10 files changed, 27 insertions(+), 81 deletions(-) delete mode 100644 lib/6to5/templates/arguments-to-array.js diff --git a/lib/6to5/file.js b/lib/6to5/file.js index 32e8f62384..0b5906f858 100644 --- a/lib/6to5/file.js +++ b/lib/6to5/file.js @@ -22,7 +22,6 @@ File.declarations = [ "tagged-template-literal", "interop-require", "to-array", - "arguments-to-array", "object-spread", "has-own", "slice" @@ -81,14 +80,11 @@ File.normaliseOptions = function (opts) { File.prototype.toArray = function (node) { if (t.isArrayExpression(node)) { return node; + } else if (t.isIdentifier(node) && node.name === "arguments") { + return t.callExpression(t.memberExpression(this.addDeclaration("slice"), t.identifier("call")), [node]); + } else { + return t.callExpression(this.addDeclaration("to-array"), [node]); } - - var templateName = "to-array"; - if (t.isIdentifier(node) && node.name === "arguments") { - templateName = "arguments-to-array"; - } - - return t.callExpression(this.addDeclaration(templateName), [node]); }; File.prototype.getModuleFormatter = function (type) { diff --git a/lib/6to5/templates/arguments-to-array.js b/lib/6to5/templates/arguments-to-array.js deleted file mode 100644 index 129e2b89e5..0000000000 --- a/lib/6to5/templates/arguments-to-array.js +++ /dev/null @@ -1,7 +0,0 @@ -(function (args) { - var target = new Array(args.length); - for (var i = 0; i< args.length; i++) { - target[i] = args[i]; - } - return target; -}) diff --git a/lib/6to5/transformation/transformers/es6-rest-parameters.js b/lib/6to5/transformation/transformers/es6-rest-parameters.js index e50f24bf5f..37a32657dd 100644 --- a/lib/6to5/transformation/transformers/es6-rest-parameters.js +++ b/lib/6to5/transformation/transformers/es6-rest-parameters.js @@ -8,13 +8,10 @@ exports.Function = function (node, parent, file) { t.ensureBlock(node); - var call = t.callExpression( - file.addDeclaration("arguments-to-array"), - [t.identifier("arguments")] - ); + var call = file.toArray(t.identifier("arguments")); if (node.params.length) { - call = t.callExpression(t.memberExpression(call, t.identifier("slice")), [t.literal(node.params.length)]); + call.arguments.push(t.literal(node.params.length)); } call._ignoreAliasFunctions = true; diff --git a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js index 59bdbf71ad..d49fffb74e 100644 --- a/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js +++ b/test/fixtures/transformation/es6-classes/accessing-super-class/expected.js @@ -1,14 +1,6 @@ "use strict"; -var _argumentsToArray = function (args) { - var target = new Array(args.length); - for (var i = 0; i < args.length; i++) { - target[i] = args[i]; - } - - return target; -}; - +var _slice = Array.prototype.slice; var _classProps = function (child, staticProps, instanceProps) { if (staticProps) Object.defineProperties(child, staticProps); if (instanceProps) Object.defineProperties(child.prototype, instanceProps); @@ -33,11 +25,11 @@ var Test = (function (Foo) { Foo.prototype.test.call(this); foob(Foo); - Foo.call.apply(Foo, [this].concat(_argumentsToArray(arguments))); - Foo.call.apply(Foo, [this, "test"].concat(_argumentsToArray(arguments))); + Foo.call.apply(Foo, [this].concat(_slice.call(arguments))); + Foo.call.apply(Foo, [this, "test"].concat(_slice.call(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_argumentsToArray(arguments))); - Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_argumentsToArray(arguments))); + Foo.prototype.test.call.apply(Foo.prototype, [this].concat(_slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype, [this, "test"].concat(_slice.call(arguments))); }; _extends(Test, Foo); @@ -47,8 +39,8 @@ var Test = (function (Foo) { writable: true, value: function () { Foo.foo.call(this); - Foo.foo.call.apply(Foo.foo, [this].concat(_argumentsToArray(arguments))); - Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_argumentsToArray(arguments))); + Foo.foo.call.apply(Foo.foo, [this].concat(_slice.call(arguments))); + Foo.foo.call.apply(Foo.foo, [this, "test"].concat(_slice.call(arguments))); } } }, { @@ -56,8 +48,8 @@ var Test = (function (Foo) { writable: true, value: function () { Foo.prototype.test.call(this); - Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_argumentsToArray(arguments))); - Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_argumentsToArray(arguments))); + Foo.prototype.test.call.apply(Foo.prototype.test, [this].concat(_slice.call(arguments))); + Foo.prototype.test.call.apply(Foo.prototype.test, [this, "test"].concat(_slice.call(arguments))); } } }); diff --git a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js index 2a41ca5ca2..49c1d11e53 100644 --- a/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/arrow-functions/expected.js @@ -1,14 +1,6 @@ "use strict"; -var _argumentsToArray = function (args) { - var target = new Array(args.length); - for (var i = 0; i < args.length; i++) { - target[i] = args[i]; - } - - return target; -}; - +var _slice = Array.prototype.slice; var concat = function () { - var arrs = _argumentsToArray(arguments); + var arrs = _slice.call(arguments); }; diff --git a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js index c5a26f5041..f2f9ea4fe4 100644 --- a/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/multiple/expected.js @@ -1,18 +1,10 @@ "use strict"; -var _argumentsToArray = function (args) { - var target = new Array(args.length); - for (var i = 0; i < args.length; i++) { - target[i] = args[i]; - } - - return target; -}; - +var _slice = Array.prototype.slice; var t = function (f) { - var items = _argumentsToArray(arguments).slice(1); + var items = _slice.call(arguments, 1); }; function t(f) { - var items = _argumentsToArray(arguments).slice(1); + var items = _slice.call(arguments, 1); } diff --git a/test/fixtures/transformation/es6-rest-parameters/single/expected.js b/test/fixtures/transformation/es6-rest-parameters/single/expected.js index 3746578ccd..306e5f6f68 100644 --- a/test/fixtures/transformation/es6-rest-parameters/single/expected.js +++ b/test/fixtures/transformation/es6-rest-parameters/single/expected.js @@ -1,18 +1,10 @@ "use strict"; -var _argumentsToArray = function (args) { - var target = new Array(args.length); - for (var i = 0; i < args.length; i++) { - target[i] = args[i]; - } - - return target; -}; - +var _slice = Array.prototype.slice; var t = function () { - var items = _argumentsToArray(arguments); + var items = _slice.call(arguments); }; function t() { - var items = _argumentsToArray(arguments); + var items = _slice.call(arguments); } diff --git a/test/fixtures/transformation/es6-spread/arguments/expected.js b/test/fixtures/transformation/es6-spread/arguments/expected.js index 11cafe747c..1ba8c3aeb2 100644 --- a/test/fixtures/transformation/es6-spread/arguments/expected.js +++ b/test/fixtures/transformation/es6-spread/arguments/expected.js @@ -1,16 +1,8 @@ "use strict"; -var _argumentsToArray = function (args) { - var target = new Array(args.length); - for (var i = 0; i < args.length; i++) { - target[i] = args[i]; - } - - return target; -}; - +var _slice = Array.prototype.slice; function foo() { - return bar.apply(null, ["test"].concat(_argumentsToArray(arguments))); + return bar.apply(null, ["test"].concat(_slice.call(arguments))); } function bar(one, two, three) { diff --git a/test/fixtures/transformation/misc/custom-runtime/expected.js b/test/fixtures/transformation/misc/custom-runtime/expected.js index 41092c40d2..94200c84ce 100644 --- a/test/fixtures/transformation/misc/custom-runtime/expected.js +++ b/test/fixtures/transformation/misc/custom-runtime/expected.js @@ -1,5 +1,5 @@ "use strict"; function foo() { - var test = customNamespace.argumentsToArray(arguments); + var test = customNamespace.slice.call(arguments); } diff --git a/test/fixtures/transformation/misc/runtime/expected.js b/test/fixtures/transformation/misc/runtime/expected.js index e7364114e7..1427ac1536 100644 --- a/test/fixtures/transformation/misc/runtime/expected.js +++ b/test/fixtures/transformation/misc/runtime/expected.js @@ -1,5 +1,5 @@ "use strict"; function foo() { - var test = to5Runtime.argumentsToArray(arguments); + var test = to5Runtime.slice.call(arguments); }