diff --git a/lib/6to5/generation/generator.js b/lib/6to5/generation/generator.js index 7f2ce983de..4406a84f46 100644 --- a/lib/6to5/generation/generator.js +++ b/lib/6to5/generation/generator.js @@ -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") }; diff --git a/lib/6to5/generation/generators/flow.js b/lib/6to5/generation/generators/flow.js new file mode 100644 index 0000000000..f96e45bde0 --- /dev/null +++ b/lib/6to5/generation/generators/flow.js @@ -0,0 +1,3 @@ +exports.ClassProperty = function () { + throw new Error("not implemented"); +}; diff --git a/lib/6to5/generation/generators/statements.js b/lib/6to5/generation/generators/statements.js index f96ecb29fa..c6c471b638 100644 --- a/lib/6to5/generation/generators/statements.js +++ b/lib/6to5/generation/generators/statements.js @@ -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); diff --git a/lib/6to5/patch.js b/lib/6to5/patch.js index b50b96718a..f846fdf2f0 100644 --- a/lib/6to5/patch.js +++ b/lib/6to5/patch.js @@ -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") diff --git a/lib/6to5/transformation/transformers/es6-classes.js b/lib/6to5/transformation/transformers/es6-classes.js index 98ac4d59d6..9292aba0ea 100644 --- a/lib/6to5/transformation/transformers/es6-classes.js +++ b/lib/6to5/transformation/transformers/es6-classes.js @@ -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); } }); diff --git a/lib/6to5/transformation/transformers/es7-abstract-references.js b/lib/6to5/transformation/transformers/es7-abstract-references.js index 266aa9d09b..73d86adb7f 100644 --- a/lib/6to5/transformation/transformers/es7-abstract-references.js +++ b/lib/6to5/transformation/transformers/es7-abstract-references.js @@ -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"), [])); + })); +}; diff --git a/lib/6to5/types/visitor-keys.json b/lib/6to5/types/visitor-keys.json index fc78a716e5..b90e63942f 100644 --- a/lib/6to5/types/visitor-keys.json +++ b/lib/6to5/types/visitor-keys.json @@ -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"], diff --git a/test/fixtures/generation/types/PrivateDeclaration/actual.js b/test/fixtures/generation/types/PrivateDeclaration/actual.js new file mode 100644 index 0000000000..c9c6b03ef6 --- /dev/null +++ b/test/fixtures/generation/types/PrivateDeclaration/actual.js @@ -0,0 +1,7 @@ +private A; +private B, C; + +class Test { + private A; + private B, C; +} diff --git a/test/fixtures/generation/types/PrivateDeclaration/expected.js b/test/fixtures/generation/types/PrivateDeclaration/expected.js new file mode 100644 index 0000000000..c9c6b03ef6 --- /dev/null +++ b/test/fixtures/generation/types/PrivateDeclaration/expected.js @@ -0,0 +1,7 @@ +private A; +private B, C; + +class Test { + private A; + private B, C; +} diff --git a/test/fixtures/transformation/es7-abstract-references/private/actual.js b/test/fixtures/transformation/es7-abstract-references/private/actual.js new file mode 100644 index 0000000000..9643ff19c2 --- /dev/null +++ b/test/fixtures/transformation/es7-abstract-references/private/actual.js @@ -0,0 +1,12 @@ +private A; +private B, C; + +class D { + private E; + private F, G; +} + +var H = class { + private I; + private J, K; +}; diff --git a/test/fixtures/transformation/es7-abstract-references/private/expected.js b/test/fixtures/transformation/es7-abstract-references/private/expected.js new file mode 100644 index 0000000000..91d4b390d1 --- /dev/null +++ b/test/fixtures/transformation/es7-abstract-references/private/expected.js @@ -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; +})();