Skip to content

Much better spread operator support

username-removed-383729 requested to merge gl-40 into master

This fixes #40 (closed) (by turning ...arguments into Array.apply(null, arguments). It also fixes a number of other spread operator cases that were previously overlooked.

Examples below, with Babel es2015-loose equivalents for comparison – slight differences but I can't envisage any scenarios in which the behaviour would differ. Correct me if I'm wrong!

// input
[ ...foo ];
[ ...foo, ...bar ];
[ ...foo, ...bar, ...baz ];
[ ...foo, bar ];
[ foo, ...bar ];

[ ...arguments ];
[ ...foo, ...arguments ];
[ foo, ...arguments ];
[ ...arguments, ...foo ];
[ ...arguments, foo ];

Math.max( ...values );
Math.max( ...theseValues, ...thoseValues );
Math.max( ...arguments );
Math.max( ...arguments, ...values );
Math.max( ...values, ...arguments );

// Bublé output
[].concat( foo );
foo.concat( bar );
foo.concat( bar, baz );
foo.concat( [bar] );
[ foo ].concat( bar );

( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) );
foo.concat( ( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) ) );
[ foo ].concat( ( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) ) );
( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) ).concat( foo );
( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) ).concat( [foo] );

Math.max.apply( Math, values );
Math.max.apply( Math, theseValues.concat( thoseValues ) );
Math.max.apply( Math, arguments );
Math.max.apply( Math, ( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) ).concat( values ) );
Math.max.apply( Math, values.concat( ( arguments.length === 1 ? [ arguments[0] ] : Array.apply( null, arguments ) ) ) );

// Babel output
[].concat(foo);
[].concat(foo, bar);
[].concat(foo, bar, baz);
[].concat(foo, [bar]);
[foo].concat(bar);

[].concat(Array.prototype.slice.call(arguments));
[].concat(foo, Array.prototype.slice.call(arguments));
[foo].concat(Array.prototype.slice.call(arguments));
[].concat(Array.prototype.slice.call(arguments), foo);
[].concat(Array.prototype.slice.call(arguments), [foo]);

Math.max.apply(Math, values);
Math.max.apply(Math, theseValues.concat(thoseValues));
Math.max.apply(Math, arguments);
Math.max.apply(Math, Array.prototype.slice.call(arguments).concat(values));
Math.max.apply(Math, values.concat(Array.prototype.slice.call(arguments)));

Next task would be to add a safeSpreadRest transform that does the same as es2015 rather than es2015-loose.

Merge request reports