Skip to content

Commit

Permalink
Fix class expressions with heritage in arrow functions
Browse files Browse the repository at this point in the history
Closes #150.
  • Loading branch information
adrianheine committed Oct 16, 2018
1 parent 499df99 commit 455a224
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/program/types/ClassExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ export default class ClassExpression extends Node {
if (this.superClass) {
code.remove(this.start, this.superClass.start);
code.remove(this.superClass.end, this.body.start);
code.appendLeft(this.start, `/*@__PURE__*/(function (${superName}) {\n${i1}`);
code.appendRight(this.start, `/*@__PURE__*/(function (${superName}) {\n${i1}`);
} else {
code.overwrite(this.start, this.body.start, `/*@__PURE__*/(function () {\n${i1}`);
}

this.body.transpile(code, transforms, true, superName);

const outro = `\n\n${i1}return ${this.name};\n${i0}}(`;

let superClass = '';
if (this.superClass) {
code.appendLeft(this.end, outro);
code.move(this.superClass.start, this.superClass.end, this.end);
code.prependRight(this.end, '))');
} else {
code.appendLeft(this.end, `\n\n${i1}return ${this.name};\n${i0}}())`);
superClass = code.slice(this.superClass.start, this.superClass.end);
code.remove(this.superClass.start, this.superClass.end);
}
code.appendLeft(this.end, `\n\n${i1}return ${this.name};\n${i0}}(${superClass}))`);
} else {
this.body.transpile(code, transforms, false);
}
Expand Down
22 changes: 22 additions & 0 deletions test/samples/classes.js
Original file line number Diff line number Diff line change
Expand Up @@ -1226,6 +1226,28 @@ module.exports = [
X.prototype.foo = function foo$1 () { return foo };
X.prototype.bar = function bar () {};
`
},

{
description: "transpiles class with super class in arrow function (#150)",

input: `
const f = (b) => class a extends b {};
`,

output: `
var f = function (b) { return /*@__PURE__*/(function (b) {
function a () {
b.apply(this, arguments);
}if ( b ) a.__proto__ = b;
a.prototype = Object.create( b && b.prototype );
a.prototype.constructor = a;
return a;
}(b)); };
`
}

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

0 comments on commit 455a224

Please sign in to comment.