add basic support for class property initializers - #619
This commit is contained in:
parent
c38edbbb42
commit
56a953df64
@ -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.
|
||||
*
|
||||
|
||||
@ -99,6 +99,7 @@
|
||||
"key": null,
|
||||
"value": null,
|
||||
"computed": false,
|
||||
"static": false,
|
||||
"kind": null
|
||||
},
|
||||
|
||||
|
||||
@ -73,7 +73,7 @@
|
||||
"AnyTypeAnnotation": [],
|
||||
"ArrayTypeAnnotation": [],
|
||||
"BooleanTypeAnnotation": [],
|
||||
"ClassProperty": ["key"],
|
||||
"ClassProperty": ["key", "value"],
|
||||
"DeclareClass": [],
|
||||
"DeclareFunction": [],
|
||||
"DeclareModule": [],
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
"test": "make test"
|
||||
},
|
||||
"dependencies": {
|
||||
"acorn-6to5": "0.11.1-28",
|
||||
"acorn-6to5": "0.11.1-29",
|
||||
"ast-types": "~0.6.1",
|
||||
"chalk": "^0.5.1",
|
||||
"chokidar": "0.12.6",
|
||||
|
||||
7
test/fixtures/transformation/es6-classes-playground-property-initializers/instance/exec.js
vendored
Normal file
7
test/fixtures/transformation/es6-classes-playground-property-initializers/instance/exec.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
class MyClass {
|
||||
myProp = { someValue: 42 };
|
||||
}
|
||||
|
||||
var myClass = new MyClass;
|
||||
assert.ok(myClass.myProp !== MyClass.prototype.myProp);
|
||||
assert.equal(myClass.myProp.someValue, 42);
|
||||
3
test/fixtures/transformation/es6-classes-playground-property-initializers/options.json
vendored
Normal file
3
test/fixtures/transformation/es6-classes-playground-property-initializers/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"playground": true
|
||||
}
|
||||
5
test/fixtures/transformation/es6-classes-playground-property-initializers/static/exec.js
vendored
Normal file
5
test/fixtures/transformation/es6-classes-playground-property-initializers/static/exec.js
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
class MyClass {
|
||||
static myProp = { someValue: 42 };
|
||||
}
|
||||
|
||||
assert.equal(MyClass.myProp.someValue, 42);
|
||||
Loading…
x
Reference in New Issue
Block a user