Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #4840: Alias class prototype for methods in loose mode #5560

Merged
merged 2 commits into from Aug 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion packages/babel-plugin-transform-es2015-classes/src/loose.js
Expand Up @@ -5,16 +5,33 @@ import * as t from "babel-types";
export default class LooseClassTransformer extends VanillaTransformer {
constructor() {
super(...arguments);
this._protoAlias = null;
this.isLoose = true;
}

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

this.body.push(protoDeclaration);
}
}

_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"));
this._insertProtoAliasOnce();
classRef = this._protoAlias;
}
const methodName = t.memberExpression(
classRef,
Expand Down
Expand Up @@ -5,7 +5,9 @@ let A = function A() {
let B = function () {
function B() {}

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

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

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

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

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

return Foo;
}();
}();
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;
}();
}();
@@ -0,0 +1,5 @@
class Test {
a() {}
static b() {}
c() {}
}
@@ -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;
}();
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 =
_proto.test2 =
/*#__PURE__*/
regeneratorRuntime.mark(function test2() {
return regeneratorRuntime.wrap(function test2$(_context) {
Expand Down
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;
}();
}();
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;
}();
}();