add support for experimental private abstract references syntax - closes #291

This commit is contained in:
Sebastian McKenzie 2014-12-14 14:49:25 +11:00
parent d26f441a5c
commit 00cb90541a
11 changed files with 90 additions and 17 deletions

View File

@ -57,6 +57,7 @@ CodeGenerator.generators = {
methods: require("./generators/methods"), methods: require("./generators/methods"),
modules: require("./generators/modules"), modules: require("./generators/modules"),
types: require("./generators/types"), types: require("./generators/types"),
flow: require("./generators/flow"),
base: require("./generators/base"), base: require("./generators/base"),
jsx: require("./generators/jsx") jsx: require("./generators/jsx")
}; };

View File

@ -0,0 +1,3 @@
exports.ClassProperty = function () {
throw new Error("not implemented");
};

View File

@ -164,6 +164,12 @@ exports.VariableDeclaration = function (node, print, parent) {
} }
}; };
exports.PrivateDeclaration = function (node, print) {
this.push("private ");
print.join(node.declarations, { separator: ", " });
this.semicolon();
};
exports.VariableDeclarator = function (node, print) { exports.VariableDeclarator = function (node, print) {
if (node.init) { if (node.init) {
print(node.id); print(node.id);

View File

@ -30,6 +30,11 @@ def("VirtualPropertyExpression")
.field("object", def("Expression")) .field("object", def("Expression"))
.field("property", or(def("Identifier"), def("Expression"))); .field("property", or(def("Identifier"), def("Expression")));
def("PrivateDeclaration")
.bases("Declaration")
.build("declarations")
.field("declarations", [def("Identifier")]);
// Playground // Playground
def("BindMemberExpression") def("BindMemberExpression")
.bases("Expression") .bases("Expression")

View File

@ -4,11 +4,11 @@ var t = require("../../types");
var _ = require("lodash"); var _ = require("lodash");
exports.ClassDeclaration = function (node, parent, file, scope) { exports.ClassDeclaration = function (node, parent, file, scope) {
return new Class(node, file, scope, true).run(); return new Class(node, file, scope, false).run();
}; };
exports.ClassExpression = function (node, parent, file, scope) { exports.ClassExpression = function (node, parent, file, scope) {
return new Class(node, file, scope, false).run(); return new Class(node, file, scope, true).run();
}; };
/** /**
@ -17,11 +17,11 @@ exports.ClassExpression = function (node, parent, file, scope) {
* @param {Node} node * @param {Node} node
* @param {File} file * @param {File} file
* @param {Scope} scope * @param {Scope} scope
* @param {Boolean} isStatement * @param {Boolean} closure
*/ */
function Class(node, file, scope, isStatement) { function Class(node, file, scope, closure) {
this.isStatement = isStatement; this.closure = closure;
this.scope = scope; this.scope = scope;
this.node = node; this.node = node;
this.file = file; this.file = file;
@ -80,9 +80,7 @@ Class.prototype.run = function () {
this.buildBody(); this.buildBody();
if (this.isStatement) { if (this.closure) {
return body;
} else {
if (body.length === 1) { if (body.length === 1) {
// only a constructor so no need for a closure container // only a constructor so no need for a closure container
return constructor; return constructor;
@ -93,6 +91,8 @@ Class.prototype.run = function () {
[] []
); );
} }
} else {
return body;
} }
}; };
@ -109,6 +109,7 @@ Class.prototype.buildBody = function () {
var self = this; var self = this;
_.each(classBody, function (node) { _.each(classBody, function (node) {
if (t.isMethodDefinition(node)) {
self.replaceInstanceSuperReferences(node); self.replaceInstanceSuperReferences(node);
if (node.key.name === "constructor") { if (node.key.name === "constructor") {
@ -116,6 +117,10 @@ Class.prototype.buildBody = function () {
} else { } else {
self.pushMethod(node); self.pushMethod(node);
} }
} else if (t.isPrivateDeclaration(node)) {
self.closure = true;
body.unshift(node);
}
}); });
if (!this.hasConstructor && superName) { if (!this.hasConstructor && superName) {

View File

@ -96,3 +96,9 @@ exports.VirtualPropertyExpression = function (node) {
OBJECT: node.object OBJECT: node.object
}); });
}; };
exports.PrivateDeclaration = function (node) {
return t.variableDeclaration("const", node.declarations.map(function (id) {
return t.variableDeclarator(id, t.newExpression(t.identifier("WeakMap"), []));
}));
};

View File

@ -5,6 +5,7 @@
"AssignmentExpression": ["left", "right"], "AssignmentExpression": ["left", "right"],
"AwaitExpression": ["argument"], "AwaitExpression": ["argument"],
"BinaryExpression": ["left", "right"], "BinaryExpression": ["left", "right"],
"BindFunctionExpression": ["callee", "arguments"],
"BindMemberExpression": ["object", "property", "arguments"], "BindMemberExpression": ["object", "property", "arguments"],
"BlockStatement": ["body"], "BlockStatement": ["body"],
"BreakStatement": ["label"], "BreakStatement": ["label"],
@ -13,6 +14,7 @@
"ClassBody": ["body"], "ClassBody": ["body"],
"ClassDeclaration": ["id", "body", "superClass"], "ClassDeclaration": ["id", "body", "superClass"],
"ClassExpression": ["id", "body", "superClass"], "ClassExpression": ["id", "body", "superClass"],
"ClassProperty": ["key"],
"ComprehensionBlock": ["left", "right", "body"], "ComprehensionBlock": ["left", "right", "body"],
"ComprehensionExpression": ["filter", "blocks", "body"], "ComprehensionExpression": ["filter", "blocks", "body"],
"ConditionalExpression": ["test", "consequent", "alternate"], "ConditionalExpression": ["test", "consequent", "alternate"],
@ -44,7 +46,7 @@
"ObjectExpression": ["properties"], "ObjectExpression": ["properties"],
"ObjectPattern": ["properties"], "ObjectPattern": ["properties"],
"ParenthesizedExpression": ["expression"], "ParenthesizedExpression": ["expression"],
"BindFunctionExpression": ["callee", "arguments"], "PrivateDeclaration": ["declarations"],
"Program": ["body"], "Program": ["body"],
"Property": ["key", "value"], "Property": ["key", "value"],
"ReturnStatement": ["argument"], "ReturnStatement": ["argument"],

View File

@ -0,0 +1,7 @@
private A;
private B, C;
class Test {
private A;
private B, C;
}

View File

@ -0,0 +1,7 @@
private A;
private B, C;
class Test {
private A;
private B, C;
}

View File

@ -0,0 +1,12 @@
private A;
private B, C;
class D {
private E;
private F, G;
}
var H = class {
private I;
private J, K;
};

View File

@ -0,0 +1,19 @@
"use strict";
var A = new WeakMap();
var B = new WeakMap(), C = new WeakMap();
(function () {
var F = new WeakMap(), G = new WeakMap();
var E = new WeakMap();
var D = function D() {};
return D;
})()
var H = (function () {
var J = new WeakMap(), K = new WeakMap();
var I = new WeakMap();
var _class = function () {};
return _class;
})();