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 Jul 21, 2017
1 parent 0b890ce commit 1897e78
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 14 deletions.
22 changes: 21 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,6 +6,25 @@ export default class LooseClassTransformer extends VanillaTransformer {
constructor() {
super(...arguments);
this.isLoose = true;
this.aliasInserted = false;
this.methodAlias = this.path.scope.generateUidIdentifier("proto");
}

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

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

_processMethod(node, scope) {
Expand All @@ -14,7 +33,8 @@ export default class LooseClassTransformer extends VanillaTransformer {

let classRef = this.classRef;
if (!node.static) {
classRef = t.memberExpression(classRef, t.identifier("prototype"));
this._insertProtoAliasOnce();
classRef = this.methodAlias;
}
const methodName = t.memberExpression(
classRef,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ let A = function A() {
let B = function () {
function B() {}

B.prototype.b = function b() {
var _proto2 = B.prototype;

_proto2.b = function b() {
console.log('b');
};

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
@@ -1,7 +1,9 @@
var Foo = function () {
function 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 @@ -2,9 +2,11 @@
var C = function () {
function C() {}

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

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

return C;
}();
}();
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,13 @@
var Test = function () {
function 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 @@ -3,11 +3,13 @@
var Example = function () {
function 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 All @@ -23,4 +25,4 @@ var Example = function () {
}, test2, this);
});
return Example;
}();
}();
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
var C = function () {
function C() {}

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

_proto.m = function m(x
/*: number*/
)
/*: string*/
Expand All @@ -13,4 +15,4 @@ var C = function () {
};

return C;
}();
}();
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
var C = function () {
function C() {}

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

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

return C;
}();
}();

0 comments on commit 1897e78

Please sign in to comment.