Skip to content

super.function(...args) calls get transpiled incorrectly

The following minimal sample

class A {

  foo(a, b) {
    console.log('A.foo(%s, %s)', a, b)
  }

}

class B extends A {

  foo(...args) {
    super.foo(...args)

    console.log('B.foo()')
  }

}

var b = new B();

b.foo('a', 'b')

gets transpiled into

var A = function A () {};

A.prototype.foo = function foo (a, b) {
  console.log('A.foo(%s, %s)', a, b)
};

var B = (function (A) {
  function B () {
    A.apply(this, arguments);
  }

  if ( A ) B.__proto__ = A;
  B.prototype = Object.create( A && A.prototype );
  B.prototype.constructor = B;

  B.prototype.foo = function foo () {
    var args = [], len = arguments.length;
    while ( len-- ) args[ len ] = arguments[ len ];

    (ref = A.prototype).foo.apply.call(this, ref, args)

    console.log('B.foo()')
    var ref;
  };

  return B;
}(A));

var b = new B();

b.foo('a', 'b')

Particularly this line:

    (ref = A.prototype).foo.apply.call(this, ref, args)

is wrong.

Without too much understanding of the generator (program/types/Super.js) I found out that disabling two lines

			if ( callExpression && callExpression.type === 'CallExpression' ) {
				if ( !this.noCall ) { // special case – `super( ...args )`
					// code.insertLeft( callExpression.callee.end, '.call' );
				}

				const thisAlias = this.thisAlias || 'this';

				if ( callExpression.arguments.length ) {
					// code.insertLeft( callExpression.arguments[0].start, `${thisAlias}, ` );
				} else {
					code.insertLeft( callExpression.end - 1, `${thisAlias}` );
				}
			}

solves the problem for this sample.

Hope this helps!