Commit 3eedc6b8 by Oliver Buchtala Committed by Marijn Haverbeke

Transpile super function calls with spread arguments correctly.

Fixes #129
parent cbc17ad5
Pipeline #5380338 passed with stage
in 2 minutes 25 seconds
......@@ -35,7 +35,18 @@ export default class CallExpression extends Node {
}
if ( hasSpreadElements ) {
if ( this.callee.type === 'MemberExpression' ) {
// we need to handle super() and super.method() differently
// due to its instance
let _super = null;
if ( this.callee.type === 'Super' ) {
_super = this.callee;
}
else if ( this.callee.type === 'MemberExpression' && this.callee.object.type === 'Super' ) {
_super = this.callee.object;
}
if ( !_super && this.callee.type === 'MemberExpression' ) {
if ( this.callee.object.type === 'Identifier' ) {
context = this.callee.object.name;
} else {
......@@ -56,12 +67,8 @@ export default class CallExpression extends Node {
code.insertLeft( this.callee.end, '.apply' );
// we need to handle `super()` different, because `SuperClass.call.apply`
// isn't very helpful
const isSuper = this.callee.type === 'Super';
if ( isSuper ) {
this.callee.noCall = true; // bit hacky...
if ( _super ) {
_super.noCall = true; // bit hacky...
if ( this.arguments.length > 1 ) {
if ( firstArgument.type !== 'SpreadElement' ) {
......
......@@ -168,6 +168,65 @@ module.exports = [
},
{
description: 'transpiles a subclass with super calls with spread arguments',
input: `
class Foo extends Bar {
baz ( ...args ) {
super.baz(...args);
}
boz ( x, y, ...z ) {
super.boz(x, y, ...z);
}
fab ( x, ...y ) {
super.qux(...x, ...y);
}
fob ( x, y, ...z ) {
((x, y, z) => super.qux(x, ...y, ...z))(x, y, z);
}
}`,
output: `
var Foo = (function (Bar) {
function Foo () {
Bar.apply(this, arguments);
}
if ( Bar ) Foo.__proto__ = Bar;
Foo.prototype = Object.create( Bar && Bar.prototype );
Foo.prototype.constructor = Foo;
Foo.prototype.baz = function baz () {
var args = [], len = arguments.length;
while ( len-- ) args[ len ] = arguments[ len ];
Bar.prototype.baz.apply(this, args);
};
Foo.prototype.boz = function boz ( x, y ) {
var z = [], len = arguments.length - 2;
while ( len-- > 0 ) z[ len ] = arguments[ len + 2 ];
Bar.prototype.boz.apply(this, [ x, y ].concat( z ));
};
Foo.prototype.fab = function fab ( x ) {
var y = [], len = arguments.length - 1;
while ( len-- > 0 ) y[ len ] = arguments[ len + 1 ];
Bar.prototype.qux.apply(this, x.concat( y ));
};
Foo.prototype.fob = function fob ( x, y ) {
var this$1 = this;
var z = [], len = arguments.length - 2;
while ( len-- > 0 ) z[ len ] = arguments[ len + 2 ];
(function (x, y, z) { return Bar.prototype.qux.apply(this$1, [ x ].concat( y, z )); })(x, y, z);
};
return Foo;
}(Bar));`
},
{
description: 'transpiles export default class',
options: { transforms: { moduleExport: false } },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment