From 56a953df641a3b032e1bac66a6e408598ea5be79 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 11 Feb 2015 10:59:44 +1100 Subject: [PATCH] add basic support for class property initializers - #619 --- .../transformers/es6/classes.js | 24 +++++++++++++++++++ lib/6to5/types/builder-keys.json | 1 + lib/6to5/types/visitor-keys.json | 2 +- package.json | 2 +- .../instance/exec.js | 7 ++++++ .../options.json | 3 +++ .../static/exec.js | 5 ++++ 7 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/transformation/es6-classes-playground-property-initializers/instance/exec.js create mode 100644 test/fixtures/transformation/es6-classes-playground-property-initializers/options.json create mode 100644 test/fixtures/transformation/es6-classes-playground-property-initializers/static/exec.js diff --git a/lib/6to5/transformation/transformers/es6/classes.js b/lib/6to5/transformation/transformers/es6/classes.js index b6a12e2293..220b237231 100644 --- a/lib/6to5/transformation/transformers/es6/classes.js +++ b/lib/6to5/transformation/transformers/es6/classes.js @@ -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. * diff --git a/lib/6to5/types/builder-keys.json b/lib/6to5/types/builder-keys.json index 52aa00845e..556ac40d2a 100644 --- a/lib/6to5/types/builder-keys.json +++ b/lib/6to5/types/builder-keys.json @@ -99,6 +99,7 @@ "key": null, "value": null, "computed": false, + "static": false, "kind": null }, diff --git a/lib/6to5/types/visitor-keys.json b/lib/6to5/types/visitor-keys.json index 8eb679cf2c..669a88f680 100644 --- a/lib/6to5/types/visitor-keys.json +++ b/lib/6to5/types/visitor-keys.json @@ -73,7 +73,7 @@ "AnyTypeAnnotation": [], "ArrayTypeAnnotation": [], "BooleanTypeAnnotation": [], - "ClassProperty": ["key"], + "ClassProperty": ["key", "value"], "DeclareClass": [], "DeclareFunction": [], "DeclareModule": [], diff --git a/package.json b/package.json index 71c0b4ba93..e1faf41b51 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/fixtures/transformation/es6-classes-playground-property-initializers/instance/exec.js b/test/fixtures/transformation/es6-classes-playground-property-initializers/instance/exec.js new file mode 100644 index 0000000000..80711e14fb --- /dev/null +++ b/test/fixtures/transformation/es6-classes-playground-property-initializers/instance/exec.js @@ -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); diff --git a/test/fixtures/transformation/es6-classes-playground-property-initializers/options.json b/test/fixtures/transformation/es6-classes-playground-property-initializers/options.json new file mode 100644 index 0000000000..e68df25688 --- /dev/null +++ b/test/fixtures/transformation/es6-classes-playground-property-initializers/options.json @@ -0,0 +1,3 @@ +{ + "playground": true +} diff --git a/test/fixtures/transformation/es6-classes-playground-property-initializers/static/exec.js b/test/fixtures/transformation/es6-classes-playground-property-initializers/static/exec.js new file mode 100644 index 0000000000..b6f0cf4dd0 --- /dev/null +++ b/test/fixtures/transformation/es6-classes-playground-property-initializers/static/exec.js @@ -0,0 +1,5 @@ +class MyClass { + static myProp = { someValue: 42 }; +} + +assert.equal(MyClass.myProp.someValue, 42);