From 4ea425ac7b0852a7a70da229f3d20cea958f5a80 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Mon, 22 Dec 2014 21:36:33 +1100 Subject: [PATCH] remove ParenthesizedExpression --- lib/6to5/generation/generator.js | 2 +- lib/6to5/generation/generators/expressions.js | 13 +++++-------- lib/6to5/generation/node/parentheses.js | 7 ------- .../transformers/es6-computed-property-names.js | 2 +- .../transformers/es6-template-literals.js | 5 +---- .../transformers/es7-array-comprehension.js | 2 +- lib/6to5/traverse/index.js | 1 - lib/6to5/types/builder-keys.json | 1 - lib/6to5/types/index.js | 3 +-- lib/6to5/types/visitor-keys.json | 1 - lib/6to5/util.js | 11 +---------- .../comments/return-no-argument/expected.js | 2 +- .../edgecase/bitwise-precedence/expected.js | 2 +- .../generation/edgecase/floating-point/expected.js | 2 +- .../generation/edgecase/for-in-no-in/expected.js | 6 +++--- .../generation/edgecase/new-precedence/actual.js | 8 ++++---- .../generation/edgecase/new-precedence/expected.js | 10 +++++----- .../edgecase/variable-declaration/expected.js | 4 ++-- .../types/ParenthesizedExpression/actual.js | 2 -- .../types/ParenthesizedExpression/expected.js | 2 -- .../es7-object-spread/expression/expected.js | 2 +- 21 files changed, 29 insertions(+), 59 deletions(-) delete mode 100644 test/fixtures/generation/types/ParenthesizedExpression/actual.js delete mode 100644 test/fixtures/generation/types/ParenthesizedExpression/expected.js diff --git a/lib/6to5/generation/generator.js b/lib/6to5/generation/generator.js index 4406a84f46..b12f235b55 100644 --- a/lib/6to5/generation/generator.js +++ b/lib/6to5/generation/generator.js @@ -156,7 +156,7 @@ CodeGenerator.prototype.print = function (node, parent, opts) { // only compute if this node needs parens if our parent has been changed // since acorn would've wrapped us in a ParanthesizedExpression - var needsParens = parent !== node._parent && n.needsParens(node, parent); + var needsParens = n.needsParens(node, parent); if (needsParens) this.push("("); this[node.type](node, this.buildPrint(node), parent); diff --git a/lib/6to5/generation/generators/expressions.js b/lib/6to5/generation/generators/expressions.js index 65d0be8710..b5a583d6bb 100644 --- a/lib/6to5/generation/generators/expressions.js +++ b/lib/6to5/generation/generators/expressions.js @@ -18,12 +18,6 @@ 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); @@ -98,8 +92,11 @@ exports.AssignmentExpression = function (node, print) { print(node.right); }; +var SCIENTIFIC_NOTATION = /e/i; + exports.MemberExpression = function (node, print) { - print(node.object); + var obj = node.object; + print(obj); if (node.computed) { this.push("["); @@ -107,7 +104,7 @@ exports.MemberExpression = function (node, print) { this.push("]"); } else { // 5..toFixed(2); - if (t.isLiteral(node.object) && util.isInteger(node.object.value)) { + if (t.isLiteral(obj) && util.isInteger(obj.value) && !SCIENTIFIC_NOTATION.test(obj.value.toString())) { this.push("."); } diff --git a/lib/6to5/generation/node/parentheses.js b/lib/6to5/generation/node/parentheses.js index 544da11d7a..bdea11d6d2 100644 --- a/lib/6to5/generation/node/parentheses.js +++ b/lib/6to5/generation/node/parentheses.js @@ -92,13 +92,6 @@ exports.YieldExpression = function (node, parent) { t.isYieldExpression(parent); }; -exports.Literal = function (node, parent) { - // (1).valueOf() - if (_.isNumber(node.value) && t.isMemberExpression(parent) && parent.object === node) { - return true; - } -}; - exports.ClassExpression = function (node, parent) { return t.isExpressionStatement(parent); }; diff --git a/lib/6to5/transformation/transformers/es6-computed-property-names.js b/lib/6to5/transformation/transformers/es6-computed-property-names.js index 91242d4947..3b434abb14 100644 --- a/lib/6to5/transformation/transformers/es6-computed-property-names.js +++ b/lib/6to5/transformation/transformers/es6-computed-property-names.js @@ -25,7 +25,7 @@ exports.ObjectExpression = function (node, parent, file) { OBJECT: node }); - var containerCallee = container.callee.expression; + var containerCallee = container.callee; var containerBody = containerCallee.body.body; containerCallee._aliasFunction = true; diff --git a/lib/6to5/transformation/transformers/es6-template-literals.js b/lib/6to5/transformation/transformers/es6-template-literals.js index 9ef874a0be..dd29d7a0f5 100644 --- a/lib/6to5/transformation/transformers/es6-template-literals.js +++ b/lib/6to5/transformation/transformers/es6-template-literals.js @@ -36,10 +36,7 @@ exports.TemplateLiteral = function (node) { nodes.push(t.literal(elem.value.cooked)); var expr = node.expressions.shift(); - if (expr) { - if (t.isBinary(expr)) expr = t.parenthesizedExpression(expr); - nodes.push(expr); - } + if (expr) nodes.push(expr); } if (nodes.length > 1) { diff --git a/lib/6to5/transformation/transformers/es7-array-comprehension.js b/lib/6to5/transformation/transformers/es7-array-comprehension.js index daf7a37720..ded4f5a28f 100644 --- a/lib/6to5/transformation/transformers/es7-array-comprehension.js +++ b/lib/6to5/transformation/transformers/es7-array-comprehension.js @@ -33,7 +33,7 @@ var multiple = function (node, file) { }); container.callee.expression._aliasFunction = true; - var block = container.callee.expression.body; + var block = container.callee.body; var body = block.body; var returnStatement = body.pop(); diff --git a/lib/6to5/traverse/index.js b/lib/6to5/traverse/index.js index add0a38df1..aea63764e0 100644 --- a/lib/6to5/traverse/index.js +++ b/lib/6to5/traverse/index.js @@ -96,7 +96,6 @@ traverse.removeProperties = function (tree) { delete node._declarations; delete node.extendedRange; delete node._scopeInfo; - delete node._parent; delete node._scope; delete node.tokens; delete node.range; diff --git a/lib/6to5/types/builder-keys.json b/lib/6to5/types/builder-keys.json index 997476ba00..beca94c0e4 100644 --- a/lib/6to5/types/builder-keys.json +++ b/lib/6to5/types/builder-keys.json @@ -16,7 +16,6 @@ "MemberExpression": ["object", "property", "computed"], "NewExpression": ["callee", "arguments"], "ObjectExpression": ["properties"], - "ParenthesizedExpression": ["expression"], "Program": ["body"], "Property": ["kind", "key", "value", "computed"], "ReturnStatement": ["argument"], diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 39c85d52bc..97be33b034 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -116,7 +116,7 @@ t.shallowEqual = function (actual, expected) { // t.isDynamic = function (node) { - if (t.isParenthesizedExpression(node) || t.isExpressionStatement(node)) { + if (t.isExpressionStatement(node)) { return t.isDynamic(node.expression); } else if (t.isIdentifier(node) || t.isLiteral(node) || t.isThisExpression(node)) { return false; @@ -262,7 +262,6 @@ t.getIds.nodes = { VariableDeclarator: "id", FunctionDeclaration: "id", ClassDeclaration: "id", - ParenthesizedExpression: "expression", MemeberExpression: "object", SpreadElement: "argument", Property: "value" diff --git a/lib/6to5/types/visitor-keys.json b/lib/6to5/types/visitor-keys.json index b90e63942f..32292aa669 100644 --- a/lib/6to5/types/visitor-keys.json +++ b/lib/6to5/types/visitor-keys.json @@ -45,7 +45,6 @@ "NewExpression": ["callee", "arguments"], "ObjectExpression": ["properties"], "ObjectPattern": ["properties"], - "ParenthesizedExpression": ["expression"], "PrivateDeclaration": ["declarations"], "Program": ["body"], "Property": ["key", "value"], diff --git a/lib/6to5/util.js b/lib/6to5/util.js index f6fa5fe0f2..1478340ae8 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -161,8 +161,6 @@ exports.template = function (name, nodes, keepExpression) { if (!keepExpression && t.isExpressionStatement(node)) { node = node.expression; - - if (t.isParenthesizedExpression(node)) node = node.expression; } return node; @@ -209,13 +207,7 @@ exports.repeat = function (width, cha) { exports.normaliseAst = function (ast, comments, tokens) { if (ast && ast.type === "Program") { - ast = t.file(ast, comments || [], tokens || []); - - traverse(ast, function (node, parent) { - node._parent = parent; - }); - - return ast; + return t.file(ast, comments || [], tokens || []); } else { throw new Error("Not a valid ast?"); } @@ -228,7 +220,6 @@ exports.parse = function (opts, code, callback) { var ast = acorn.parse(code, { allowReturnOutsideFunction: true, - preserveParens: true, ecmaVersion: opts.experimental ? 7 : 6, playground: opts.playground, strictMode: true, diff --git a/test/fixtures/generation/comments/return-no-argument/expected.js b/test/fixtures/generation/comments/return-no-argument/expected.js index c8f39a07a5..0dc96b3a8c 100644 --- a/test/fixtures/generation/comments/return-no-argument/expected.js +++ b/test/fixtures/generation/comments/return-no-argument/expected.js @@ -1,3 +1,3 @@ (function () { return; // comment -}()); +})(); diff --git a/test/fixtures/generation/edgecase/bitwise-precedence/expected.js b/test/fixtures/generation/edgecase/bitwise-precedence/expected.js index 7c6969bfc8..ffcf91adde 100644 --- a/test/fixtures/generation/edgecase/bitwise-precedence/expected.js +++ b/test/fixtures/generation/edgecase/bitwise-precedence/expected.js @@ -1,3 +1,3 @@ x | y ^ z; -x | (y ^ z); +x | y ^ z; (x | y) ^ z; diff --git a/test/fixtures/generation/edgecase/floating-point/expected.js b/test/fixtures/generation/edgecase/floating-point/expected.js index b38eebf513..f0c3126dbe 100644 --- a/test/fixtures/generation/edgecase/floating-point/expected.js +++ b/test/fixtures/generation/edgecase/floating-point/expected.js @@ -1,2 +1,2 @@ 1.1.valueOf(); -(1e+300).valueOf(); +1e+300.valueOf(); diff --git a/test/fixtures/generation/edgecase/for-in-no-in/expected.js b/test/fixtures/generation/edgecase/for-in-no-in/expected.js index f84b95ae6b..029b11e515 100644 --- a/test/fixtures/generation/edgecase/for-in-no-in/expected.js +++ b/test/fixtures/generation/edgecase/for-in-no-in/expected.js @@ -1,12 +1,12 @@ for (var i = (1 in []) in []); -for (var i = 1 in [] in []); +for (var i = 1 in ([] in [])); for (var i = (10 * 10 in []) in []); for (var i = (10 + 10 in []) in []); for (var i = 10 + (10 in []) in []); -for (var i = 10 + 10 in [] in []); +for (var i = 10 + 10 in ([] in [])); for (var i = (1 in []);;); for ((1 in []);;); for (1 * (1 in []);;); for (1 * (1 + 1 in []);;); -for (1 * ((1 + 1) in []);;); +for (1 * (1 + 1 in []);;); for (1 * (1 + (1 in []));;); diff --git a/test/fixtures/generation/edgecase/new-precedence/actual.js b/test/fixtures/generation/edgecase/new-precedence/actual.js index 56044a7b99..7100c69c3d 100644 --- a/test/fixtures/generation/edgecase/new-precedence/actual.js +++ b/test/fixtures/generation/edgecase/new-precedence/actual.js @@ -1,9 +1,9 @@ new (a().b)(); new a().b(); new (a()).b(); -new (a()); -new new a(a); -new (new a)(a); +new (a())(); +new new a(a)(); +new (new a())(a); (new a()).test; (new a().test); -(new (a().test)); +(new (a().test)()); diff --git a/test/fixtures/generation/edgecase/new-precedence/expected.js b/test/fixtures/generation/edgecase/new-precedence/expected.js index 7100c69c3d..646afe94da 100644 --- a/test/fixtures/generation/edgecase/new-precedence/expected.js +++ b/test/fixtures/generation/edgecase/new-precedence/expected.js @@ -1,9 +1,9 @@ new (a().b)(); new a().b(); -new (a()).b(); +new (a().b)(); new (a())(); new new a(a)(); -new (new a())(a); -(new a()).test; -(new a().test); -(new (a().test)()); +new new a()(a); +new a().test; +new a().test; +new (a().test)(); diff --git a/test/fixtures/generation/edgecase/variable-declaration/expected.js b/test/fixtures/generation/edgecase/variable-declaration/expected.js index 12407627ec..0af0dd6ac0 100644 --- a/test/fixtures/generation/edgecase/variable-declaration/expected.js +++ b/test/fixtures/generation/edgecase/variable-declaration/expected.js @@ -1,4 +1,4 @@ -var fact5 = function fact(n) { +var fact5 = (function fact(n) { if (n <= 1) return 1; return n * fact(n - 1); -}(5); +})(5); diff --git a/test/fixtures/generation/types/ParenthesizedExpression/actual.js b/test/fixtures/generation/types/ParenthesizedExpression/actual.js deleted file mode 100644 index 5d7b0fd7c0..0000000000 --- a/test/fixtures/generation/types/ParenthesizedExpression/actual.js +++ /dev/null @@ -1,2 +0,0 @@ -(foo()); -(5 * 6); diff --git a/test/fixtures/generation/types/ParenthesizedExpression/expected.js b/test/fixtures/generation/types/ParenthesizedExpression/expected.js deleted file mode 100644 index 5d7b0fd7c0..0000000000 --- a/test/fixtures/generation/types/ParenthesizedExpression/expected.js +++ /dev/null @@ -1,2 +0,0 @@ -(foo()); -(5 * 6); diff --git a/test/fixtures/transformation/es7-object-spread/expression/expected.js b/test/fixtures/transformation/es7-object-spread/expression/expected.js index ae48d06c13..856b5c1f8c 100644 --- a/test/fixtures/transformation/es7-object-spread/expression/expected.js +++ b/test/fixtures/transformation/es7-object-spread/expression/expected.js @@ -1,3 +1,3 @@ "use strict"; -(Object.assign({ x: x }, y, { a: a }, b, { c: c })); +Object.assign({ x: x }, y, { a: a }, b, { c: c });