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"),
modules: require("./generators/modules"),
types: require("./generators/types"),
flow: require("./generators/flow"),
base: require("./generators/base"),
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) {
if (node.init) {
print(node.id);

View File

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

View File

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

View File

@ -96,3 +96,9 @@ exports.VirtualPropertyExpression = function (node) {
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"],
"AwaitExpression": ["argument"],
"BinaryExpression": ["left", "right"],
"BindFunctionExpression": ["callee", "arguments"],
"BindMemberExpression": ["object", "property", "arguments"],
"BlockStatement": ["body"],
"BreakStatement": ["label"],
@ -13,6 +14,7 @@
"ClassBody": ["body"],
"ClassDeclaration": ["id", "body", "superClass"],
"ClassExpression": ["id", "body", "superClass"],
"ClassProperty": ["key"],
"ComprehensionBlock": ["left", "right", "body"],
"ComprehensionExpression": ["filter", "blocks", "body"],
"ConditionalExpression": ["test", "consequent", "alternate"],
@ -44,7 +46,7 @@
"ObjectExpression": ["properties"],
"ObjectPattern": ["properties"],
"ParenthesizedExpression": ["expression"],
"BindFunctionExpression": ["callee", "arguments"],
"PrivateDeclaration": ["declarations"],
"Program": ["body"],
"Property": ["key", "value"],
"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;
})();