diff --git a/lib/6to5/generator.js b/lib/6to5/generator.js index a5241e4926..bca53b5dc4 100644 --- a/lib/6to5/generator.js +++ b/lib/6to5/generator.js @@ -3,6 +3,8 @@ module.exports = function (ast, opts) { return gen.generate(ast, opts); }; +module.exports.CodeGenerator = CodeGenerator; + var assert = require("assert"); var _ = require("lodash"); @@ -98,18 +100,11 @@ CodeGenerator.prototype.MemberExpression = function (node, print) { return code; }; -CodeGenerator.prototype.Path = function (node, print) { - return "." + print(node.body); -}; - CodeGenerator.prototype.Identifier = function (node) { return node.name; }; -CodeGenerator.prototype.SpreadElement = -CodeGenerator.prototype.SpreadElementPattern = -CodeGenerator.prototype.SpreadProperty = -CodeGenerator.prototype.SpreadPropertyPattern = function (node, print) { +CodeGenerator.prototype.SpreadElement = function (node, print) { return "..." + print(node.argument); }; @@ -120,7 +115,7 @@ CodeGenerator.prototype.FunctionExpression = function (node, print) { code += "function"; if (node.generator) code += "*"; if (node.id) code += " " + print(node.id); - code += "(" + node.params.map(print).join(", ") + ")"; + code += this._params(node, print); code += " " + print(node.body); return code; }; @@ -131,17 +126,13 @@ CodeGenerator.prototype.ArrowFunctionExpression = function (node, print) { if (node.params.length === 1) { code += print(node.params[0]); } else { - code += "(" + node.params.map(this.buildPrint(node)).join(", ") + ")"; + code += this._params(node, print); } code += " => "; code += print(node.body); return code; }; -CodeGenerator.prototype.MethodDefinition = function () { - throw new Error("MethodDefinition"); -}; - CodeGenerator.prototype.YieldExpression = function (node, print) { var code = "yield"; if (node.delegate) code += "*"; @@ -149,23 +140,6 @@ CodeGenerator.prototype.YieldExpression = function (node, print) { return code; }; -CodeGenerator.prototype.AwaitExpression = function (node, print) { - var code = "await"; - if (node.all) code += "*"; - if (node.argument) code += print(node.argument); - return code; -}; - -CodeGenerator.prototype.ModuleDeclaration = function (node, print) { - var code = "module " + print(node.id); - if (node.source) { - code += " from " + print(node.source); - } else { - code += print(node.body); - } - return code; -}; - CodeGenerator.prototype.ImportSpecifier = CodeGenerator.prototype.ExportSpecifier = function (node, print) { var code = print(node.id); @@ -222,37 +196,27 @@ CodeGenerator.prototype.CallExpression = function (node, print) { CodeGenerator.prototype.ObjectExpression = CodeGenerator.prototype.ObjectPattern = function (node, print) { - var allowBreak = false; - var indent = this.indent; - var parts = [len > 0 ? "{\n" : "{"]; - var len = node.properties.length; + var props = node.properties; + var len = props.length; + if (!len) return "{}"; - _.each(node.properties, function (prop, i) { - var lines = indent(print(prop)); + var indent = this.indent; + var parts = ["{\n"]; - var multiLine = lines.length > 1; - if (multiLine && allowBreak) { - // Similar to the logic for BlockStatement. - parts.push("\n"); - } - - parts.push(lines); + _.each(props, function (prop, i) { + var part = indent(print(prop)); if (i < len - 1) { - // Add an extra line break if the previous object property - // had a multi-line value. - parts.push(multiLine ? ",\n\n" : ",\n"); - allowBreak = !multiLine; + // not the last item + part += ",\n"; } + + parts.push(part); }); - parts.push(len > 0 ? "\n}" : "}"); + parts.push("\n}"); - return parts.join("\n"); -}; - -CodeGenerator.prototype.PropertyPattern = function (node, print) { - return print(node.key) + ": " + print(node.pattern); + return parts.join(""); }; CodeGenerator.prototype.Property = function (node, print) { @@ -318,10 +282,6 @@ CodeGenerator.prototype.Literal = function (node) { } }; -CodeGenerator.prototype.ModuleSpecifier = function (node) { - return "\"" + node.value + "\""; -}; - CodeGenerator.prototype.UnaryExpression = function (node, print) { var code = node.operator; if (/[a-z]$/.test(node.operator)) code += " "; @@ -523,6 +483,38 @@ CodeGenerator.prototype.ClassDeclaration = function (node, print) { return parts.join(""); }; +CodeGenerator.prototype.ComprehensionBlock = function (node, print) { + throw new Error("ComprehensionBlock"); +}; + +CodeGenerator.prototype.ComprehensionExpression = function (node, print) { + throw new Error("ComprehensionExpression"); +}; + +CodeGenerator.prototype.ForOfStatement = function (node, print) { + throw new Error("ForOfStatement"); +}; + +CodeGenerator.prototype.ImportBatchSpecifier = function (node, print) { + throw new Error("ImportBatchSpecifier"); +}; + +CodeGenerator.prototype.ParenthesizedExpression = function (node, print) { + throw new Error("ParenthesizedExpression"); +}; + +CodeGenerator.prototype.TaggedTemplateExpression = function (node, print) { + throw new Error("TaggedTemplateExpression"); +}; + +CodeGenerator.prototype.TemplateElement = function (node, print) { + throw new Error("TemplateElement"); +}; + +CodeGenerator.prototype.TemplateLiteral = function (node, print) { + throw new Error("TemplateLiteral"); +}; + CodeGenerator.prototype.ClassBody = function (node, print) { if (node.body.length === 0) { return "{}"; @@ -535,6 +527,10 @@ CodeGenerator.prototype.ClassBody = function (node, print) { ].join(""); }; +CodeGenerator.prototype._params = function (node, print) { + return "(" + node.params.map(print).join(", ") + ")"; +}; + CodeGenerator.prototype._method = function (kind, key, value, print) { var parts = []; @@ -553,7 +549,7 @@ CodeGenerator.prototype._method = function (kind, key, value, print) { parts.push( print(key), - "(" + value.params.map(print).join(", ") + ")", + this._params(value, print), print(value.body) ); @@ -620,10 +616,6 @@ CodeGenerator.prototype.XJSClosingElement = function (node) { return ""; }; -CodeGenerator.prototype.XJSText = function (node) { - return node.value; -}; - CodeGenerator.prototype.XJSEmptyExpression = function () { return ""; };