add basic support for class property initializers - #619

This commit is contained in:
Sebastian McKenzie
2015-02-11 10:59:44 +11:00
parent c38edbbb42
commit 56a953df64
7 changed files with 42 additions and 2 deletions

View File

@@ -167,6 +167,8 @@ ClassTransformer.prototype.buildBody = function () {
} else if (t.isPrivateDeclaration(node)) {
this.closure = true;
body.unshift(node);
} else if (t.isClassProperty(node)) {
this.pushProperty(node);
}
}
@@ -245,6 +247,28 @@ ClassTransformer.prototype.pushMethod = function (node) {
defineMap.push(mutatorMap, methodName, "enumerable", node.computed, false);
};
/**
* Description
*
* @param {Node} node
*/
ClassTransformer.prototype.pushProperty = function (node) {
if (!node.value) return;
if (node.static) {
var key = t.memberExpression(this.className, node.key);
this.body.push(
t.expressionStatement(t.assignmentExpression("=", key, node.value))
);
} else {
var key = t.memberExpression(t.thisExpression(), node.key);
this.constructor.body.body.unshift(
t.expressionStatement(t.assignmentExpression("=", key, node.value))
);
}
};
/**
* Replace the constructor body of our class.
*

View File

@@ -99,6 +99,7 @@
"key": null,
"value": null,
"computed": false,
"static": false,
"kind": null
},

View File

@@ -73,7 +73,7 @@
"AnyTypeAnnotation": [],
"ArrayTypeAnnotation": [],
"BooleanTypeAnnotation": [],
"ClassProperty": ["key"],
"ClassProperty": ["key", "value"],
"DeclareClass": [],
"DeclareFunction": [],
"DeclareModule": [],