Skip to content

Commit

Permalink
Fix babel#4840: Alias class prototype for methods in loose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverdon committed Mar 28, 2017
1 parent 0c2ac9d commit 4dcb7e0
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 8 deletions.
21 changes: 20 additions & 1 deletion packages/babel-plugin-transform-es2015-classes/src/loose.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,33 @@ export default class LooseClassTransformer extends VanillaTransformer {
constructor() {
super(...arguments);
this.isLoose = true;
this.aliasInserted = false;
this.methodAlias = "_proto";
}

_insertProtoAliasOnce() {
if (!this.aliasInserted) {
const classProto = t.memberExpression(this.classRef, t.identifier("prototype"));
const protoDeclaration = t.variableDeclaration("var", [
t.variableDeclarator(t.identifier(this.methodAlias), classProto),
]);

this.body.push(protoDeclaration);
this.aliasInserted = true;
return true;
}
return false;
}

_processMethod(node, scope) {
if (!node.decorators) {
// use assignments instead of define properties for loose classes

let classRef = this.classRef;
if (!node.static) classRef = t.memberExpression(classRef, t.identifier("prototype"));
if (!node.static) {
this._insertProtoAliasOnce();
classRef = t.identifier(this.methodAlias);
}
const methodName = t.memberExpression(classRef, node.key, node.computed || t.isLiteral(node.key));

let func = t.functionExpression(null, node.params, node.body, node.generator, node.async);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
var x = function () {
x.prototype.f = function f() {
var _proto = x.prototype;

_proto.f = function f() {
1;
2;
3;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var Foo = function () {
babelHelpers.classCallCheck(this, Foo);
}

Foo.prototype["bar"] = function bar() {};
var _proto = Foo.prototype;

_proto["bar"] = function bar() {};

return Foo;
}();
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ var C = function () {
babelHelpers.classCallCheck(this, C);
}

C.prototype.m = function m(x: number): string {
var _proto = C.prototype;

_proto.m = function m(x: number): string {
return 'a';
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Test {
a() {}
static b() {}
c() {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var Test = function () {
function Test() {
babelHelpers.classCallCheck(this, Test);
}

var _proto = Test.prototype;

_proto.a = function a() {};

Test.b = function b() {};

_proto.c = function c() {};

return Test;
}();
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ var Example = function () {
_classCallCheck(this, Example);
}

Example.prototype.test1 = async function test1() {
var _proto = Example.prototype;

_proto.test1 = async function test1() {
await Promise.resolve(2);
};

Example.prototype.test2 = regeneratorRuntime.mark(function test2() {
_proto.test2 = regeneratorRuntime.mark(function test2() {
return regeneratorRuntime.wrap(function test2$(_context) {
while (1) {
switch (_context.prev = _context.next) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ var C = function () {
babelHelpers.classCallCheck(this, C);
}

C.prototype.m = function m(x /*: number*/) /*: string*/ {
var _proto = C.prototype;

_proto.m = function m(x /*: number*/) /*: string*/ {
return 'a';
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ var C = function () {
babelHelpers.classCallCheck(this, C);
}

C.prototype.m = function m(x) {
var _proto = C.prototype;

_proto.m = function m(x) {
return 'a';
};

Expand Down

0 comments on commit 4dcb7e0

Please sign in to comment.