Skip to content

Commit

Permalink
correct use of super() in static methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed May 14, 2016
1 parent 08d12d6 commit 3ec933e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/program/types/Super.js
Expand Up @@ -28,7 +28,10 @@ export default class Super extends Node {

transpile ( code, transforms ) {
if ( transforms.classes ) {
const expression = this.isCalled ? this.superClassName : `${this.superClassName}.prototype`;
const expression = ( this.isCalled || this.method.static ) ?
this.superClassName :
`${this.superClassName}.prototype`;

code.overwrite( this.start, this.end, expression, true );

const callExpression = this.isCalled ? this.parent : this.parent.parent;
Expand Down
28 changes: 28 additions & 0 deletions test/samples/classes.js
Expand Up @@ -556,6 +556,34 @@ module.exports = [
var a = options.a;
var b = options.b;
};`
},

{
description: 'allows super in static methods',

input: `
class Foo extends Bar {
static baz () {
super.baz();
}
}`,

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.baz = function baz () {
Bar.baz.call(this);
};
return Foo;
}(Bar));`
}

// TODO more tests. e.g. getters and setters. computed method names
Expand Down

0 comments on commit 3ec933e

Please sign in to comment.