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

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

* Cleanup
This commit is contained in:
Oliver Don
2017-08-27 02:15:45 +01:00
committed by Justin Ridgewell
parent d8b4073536
commit 960151c876
10 changed files with 62 additions and 13 deletions

View File

@@ -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,