From 0b86a2fef896a45a10483a2307f2882d215cac46 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sun, 9 Nov 2014 12:06:56 +1100 Subject: [PATCH] use acorns preserveParens --- lib/6to5/generation/generators/expressions.js | 8 +- .../transformers/array-comprehension.js | 2 +- .../transformation/transformers/classes.js | 7 +- .../transformers/computed-property-names.js | 2 +- .../transformers/template-literals.js | 5 +- lib/6to5/types/visitor-keys.json | 1 + lib/6to5/util.js | 87 ++++++++++--------- 7 files changed, 62 insertions(+), 50 deletions(-) diff --git a/lib/6to5/generation/generators/expressions.js b/lib/6to5/generation/generators/expressions.js index cc84a32c53..52bd5af8b7 100644 --- a/lib/6to5/generation/generators/expressions.js +++ b/lib/6to5/generation/generators/expressions.js @@ -17,6 +17,12 @@ exports.UnaryExpression = function (node, print) { print(node.argument); }; +exports.ParenthesizedExpression = function (node, print) { + this.push("("); + print(node.expression); + this.push(")"); +}; + exports.UpdateExpression = function (node, print) { if (node.prefix) { this.push(node.operator); @@ -35,7 +41,7 @@ exports.ConditionalExpression = function (node, print) { print(node.alternate); }; -exports.NewExpression = function (node, print, parent) { +exports.NewExpression = function (node, print) { this.push("new "); print(node.callee); this.push("("); diff --git a/lib/6to5/transformation/transformers/array-comprehension.js b/lib/6to5/transformation/transformers/array-comprehension.js index 85c4203ad3..eb8ddb6970 100644 --- a/lib/6to5/transformation/transformers/array-comprehension.js +++ b/lib/6to5/transformation/transformers/array-comprehension.js @@ -25,7 +25,7 @@ var multiple = function (node, file) { }); container._aliasFunction = true; - var block = container.callee.body; + var block = container.callee.expression.body; var body = block.body; var returnStatement = body.pop(); diff --git a/lib/6to5/transformation/transformers/classes.js b/lib/6to5/transformation/transformers/classes.js index a563572621..a05d08751b 100644 --- a/lib/6to5/transformation/transformers/classes.js +++ b/lib/6to5/transformation/transformers/classes.js @@ -40,7 +40,7 @@ var buildClass = function (node, file, scope) { CLASS_NAME: className }); - var block = container.callee.body; + var block = container.callee.expression.body; var body = block.body; var constructor = body[0].declarations[0].init; @@ -52,7 +52,7 @@ var buildClass = function (node, file, scope) { body.push(t.expressionStatement(t.callExpression(file.addDeclaration("extends"), [className, superName]))); container.arguments.push(superClassArgument); - container.callee.params.push(superClassCallee); + container.callee.expression.params.push(superClassCallee); } buildClassBody({ @@ -180,8 +180,7 @@ var superIdentifier = function (superName, methodNode, node, parent) { }; var replaceInstanceSuperReferences = function (superName, methodNode) { - var methodName = methodNode.key; - var method = methodNode.value; + var method = methodNode.value; superName = superName || t.identifier("Function"); diff --git a/lib/6to5/transformation/transformers/computed-property-names.js b/lib/6to5/transformation/transformers/computed-property-names.js index 111a4a940d..c66e3e881e 100644 --- a/lib/6to5/transformation/transformers/computed-property-names.js +++ b/lib/6to5/transformation/transformers/computed-property-names.js @@ -26,7 +26,7 @@ exports.ObjectExpression = function (node, parent, file) { OBJECT: node }); - var containerCallee = container.callee; + var containerCallee = container.callee.expression; var containerBody = containerCallee.body.body; containerCallee._aliasFunction = "arrows"; diff --git a/lib/6to5/transformation/transformers/template-literals.js b/lib/6to5/transformation/transformers/template-literals.js index c9b923f277..e881434176 100644 --- a/lib/6to5/transformation/transformers/template-literals.js +++ b/lib/6to5/transformation/transformers/template-literals.js @@ -28,7 +28,10 @@ exports.TemplateLiteral = function (node) { nodes.push(t.literal(elem.value.raw)); var expr = node.expressions.shift(); - if (expr) nodes.push(expr); + if (expr) { + if (t.isBinary(expr)) expr = t.parenthesizedExpression(expr); + nodes.push(expr); + } }); if (nodes.length > 1) { diff --git a/lib/6to5/types/visitor-keys.json b/lib/6to5/types/visitor-keys.json index 7a5ccb1ea0..fe3396a904 100644 --- a/lib/6to5/types/visitor-keys.json +++ b/lib/6to5/types/visitor-keys.json @@ -41,6 +41,7 @@ "NewExpression": ["callee", "arguments"], "ObjectExpression": ["properties"], "ObjectPattern": ["properties"], + "ParenthesizedExpression": ["expression"], "Program": ["body"], "Property": ["key", "value"], "ReturnStatement": ["argument"], diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 796c4df99b..4ecb4baa5b 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -1,3 +1,5 @@ +require("./patch"); + var estraverse = require("estraverse"); var traverse = require("./traverse"); var acorn = require("acorn-6to5"); @@ -7,8 +9,6 @@ var fs = require("fs"); var t = require("./types"); var _ = require("lodash"); -_.extend(estraverse.VisitorKeys, t.VISITOR_KEYS); - exports.inherits = util.inherits; exports.resolve = function (loc) { @@ -136,6 +136,8 @@ exports.template = function (name, nodes, keepExpression) { if (!keepExpression && t.isExpressionStatement(node)) { node = node.expression; + + if (t.isParenthesizedExpression(node)) node = node.expression; } if (inherits) { @@ -190,23 +192,24 @@ exports.parse = function (opts, code, callback) { var tokens = []; var ast = acorn.parse(code, { - ecmaVersion: Infinity, - strictMode: true, - onComment: comments, - locations: true, - onToken: tokens, - ranges: true + preserveParens: true, + ecmaVersion: Infinity, + strictMode: true, + onComment: comments, + locations: true, + onToken: tokens, + ranges: true }); estraverse.attachComments(ast, comments, tokens); ast = { - type: "File", - program: ast - }; + type: "File", + program: ast, - ast.comments = comments; - ast.tokens = tokens; + comments: comments, + tokens: tokens, + }; if (callback) { return callback(ast); @@ -238,36 +241,36 @@ exports.parseNoProperties = function (loc, code) { }; var loadTemplates = function () { - try { - return require("../../templates.json"); - } catch (err) { - if (err.code !== "MODULE_NOT_FOUND") throw err; + var templates = {}; - var templates = {}; - - var templatesLoc = __dirname + "/templates"; - if (!fs.existsSync(templatesLoc)) { - throw new Error("no templates directory - this is most likely the " + - "result of a broken `npm publish`. Please report to " + - "https://githut.com/sebmck/6to5/issues"); - } - - _.each(fs.readdirSync(templatesLoc), function (name) { - if (name[0] === ".") return; - - var key = path.basename(name, path.extname(name)); - var loc = templatesLoc + "/" + name; - var code = fs.readFileSync(loc, "utf8"); - - templates[key] = exports.parseNoProperties(loc, code); - }); - - return templates; + var templatesLoc = __dirname + "/templates"; + if (!fs.existsSync(templatesLoc)) { + throw new Error("no templates directory - this is most likely the " + + "result of a broken `npm publish`. Please report to " + + "https://githut.com/sebmck/6to5/issues"); } + + _.each(fs.readdirSync(templatesLoc), function (name) { + if (name[0] === ".") return; + + var key = path.basename(name, path.extname(name)); + var loc = templatesLoc + "/" + name; + var code = fs.readFileSync(loc, "utf8"); + + templates[key] = exports.parseNoProperties(loc, code); + }); + + return templates; }; -Object.defineProperty(exports, "templates", { - get: function () { - return exports.templates = loadTemplates(); - } -}); +try { + return require("../../templates.json"); +} catch (err) { + if (err.code !== "MODULE_NOT_FOUND") throw err; + + Object.defineProperty(exports, "templates", { + get: function () { + return exports.templates = loadTemplates(); + } + }); +}