add support for experimental private abstract references syntax - closes #291
This commit is contained in:
parent
d26f441a5c
commit
00cb90541a
@ -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")
|
||||
};
|
||||
|
||||
3
lib/6to5/generation/generators/flow.js
Normal file
3
lib/6to5/generation/generators/flow.js
Normal file
@ -0,0 +1,3 @@
|
||||
exports.ClassProperty = function () {
|
||||
throw new Error("not implemented");
|
||||
};
|
||||
@ -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);
|
||||
|
||||
@ -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")
|
||||
|
||||
@ -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,11 +17,11 @@ 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;
|
||||
function Class(node, file, scope, closure) {
|
||||
this.closure = closure;
|
||||
this.scope = scope;
|
||||
this.node = node;
|
||||
this.file = file;
|
||||
@ -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,6 +109,7 @@ Class.prototype.buildBody = function () {
|
||||
var self = this;
|
||||
|
||||
_.each(classBody, function (node) {
|
||||
if (t.isMethodDefinition(node)) {
|
||||
self.replaceInstanceSuperReferences(node);
|
||||
|
||||
if (node.key.name === "constructor") {
|
||||
@ -116,6 +117,10 @@ Class.prototype.buildBody = function () {
|
||||
} else {
|
||||
self.pushMethod(node);
|
||||
}
|
||||
} else if (t.isPrivateDeclaration(node)) {
|
||||
self.closure = true;
|
||||
body.unshift(node);
|
||||
}
|
||||
});
|
||||
|
||||
if (!this.hasConstructor && superName) {
|
||||
|
||||
@ -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"), []));
|
||||
}));
|
||||
};
|
||||
|
||||
@ -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"],
|
||||
|
||||
7
test/fixtures/generation/types/PrivateDeclaration/actual.js
vendored
Normal file
7
test/fixtures/generation/types/PrivateDeclaration/actual.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
private A;
|
||||
private B, C;
|
||||
|
||||
class Test {
|
||||
private A;
|
||||
private B, C;
|
||||
}
|
||||
7
test/fixtures/generation/types/PrivateDeclaration/expected.js
vendored
Normal file
7
test/fixtures/generation/types/PrivateDeclaration/expected.js
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
private A;
|
||||
private B, C;
|
||||
|
||||
class Test {
|
||||
private A;
|
||||
private B, C;
|
||||
}
|
||||
12
test/fixtures/transformation/es7-abstract-references/private/actual.js
vendored
Normal file
12
test/fixtures/transformation/es7-abstract-references/private/actual.js
vendored
Normal 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;
|
||||
};
|
||||
19
test/fixtures/transformation/es7-abstract-references/private/expected.js
vendored
Normal file
19
test/fixtures/transformation/es7-abstract-references/private/expected.js
vendored
Normal 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;
|
||||
})();
|
||||
Loading…
x
Reference in New Issue
Block a user