Fix #951: properly parenthesize ** expressions (#952)

This commit is contained in:
Kevin Gibbons authored and Christopher Chedeau committed 2017-03-08 13:11:53 -08:00
1 parent f5ede513eb
commit dc2fa2cdd0
5 files changed
+41 -15

No files matched your search

+7 -13
View File
@@ -232,19 +232,6 @@ FPp.needsParens = function(assumeExpressionContext) {
return true;
}
// The left-hand side of the ** exponentiation operator must always
// be parenthesized unless it's an ident or literal
if (
parent.type === "BinaryExpression" &&
parent.operator === "**" &&
parent.left === node &&
node.type !== "Identifier" &&
node.type !== "Literal" &&
node.type !== "NumericLiteral"
) {
return true;
}
if (
parent.type === "ArrowFunctionExpression" && parent.body === node && startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ false)
|| parent.type === "ExpressionStatement" && startsWithNoLookaheadToken(node, /* forbidFunctionAndClass */ true)
@@ -285,6 +272,9 @@ FPp.needsParens = function(assumeExpressionContext) {
case "CallExpression":
return name === "callee" && parent.callee == node;
case "BinaryExpression":
return parent.operator === "**" && name === "left";
default:
return false;
}
@@ -332,6 +322,10 @@ FPp.needsParens = function(assumeExpressionContext) {
return true;
}
if (no === "**" && po === "**") {
return name === "left";
}
if (pp === np && name === "right") {
assert.strictEqual(parent.right, node);
return true;
+3 -1
View File
@@ -2783,10 +2783,12 @@ function printBinaryishExpressions(path, parts, print, options, isNested) {
// is where the rest of the expression will exist. Binary
// expressions on the right side mean they have a difference
// precedence level and should be treated as a separate group, so
// print them normally.
// print them normally. (This doesn't hold for the `**` operator,
// which is unique in that it is right-associative.)
if (
util.getPrecedence(node.left.operator) ===
util.getPrecedence(node.operator)
&& node.operator !== "**"
) {
// Flatten them out by recursively calling this function. The
// printed values will all be appended to `parts`.
+2 -1
View File
@@ -294,7 +294,8 @@ var PRECEDENCE = {};
["<", ">", "<=", ">=", "in", "instanceof"],
[">>", "<<", ">>>"],
["+", "-"],
["*", "/", "%", "**"]
["*", "/", "%"],
["**"]
].forEach(function(tier, i) {
tier.forEach(function(op) {
PRECEDENCE[op] = i;
@@ -19,6 +19,27 @@ function f() {
"
`;
exports[`exp.js 1`] = `
"a ** b ** c;
(a ** b) ** c;
a.b ** c;
(-a) ** b;
a ** -b;
-(a**b);
(a * b) ** c;
a ** (b * c);
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a ** b ** c;
(a ** b) ** c;
a.b ** c;
(-a) ** b;
a ** -b;
-(a ** b);
(a * b) ** c;
a ** (b * c);
"
`;
exports[`inline-object-array.js 1`] = `
"prevState = prevState || {
catalogs: [],
+8
View File
@@ -0,0 +1,8 @@
a ** b ** c;
(a ** b) ** c;
a.b ** c;
(-a) ** b;
a ** -b;
-(a**b);
(a * b) ** c;
a ** (b * c);