Skip to content

Commit

Permalink
fix(#74): correct exponentiation precedence
Browse files Browse the repository at this point in the history
Closes #74.
  • Loading branch information
davidbonnet committed Jul 20, 2019
1 parent 6e56162 commit b267927
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 12 deletions.
2 changes: 1 addition & 1 deletion docs/demo/astring.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/demo/astring.min.js.map

Large diffs are not rendered by default.

23 changes: 14 additions & 9 deletions src/astring.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,13 @@ function expressionNeedsParenthesis(node, parentNode, isRightHand) {
const parentNodePrecedence = EXPRESSIONS_PRECEDENCE[parentNode.type]
if (nodePrecedence !== parentNodePrecedence) {
// Different node types
return nodePrecedence < parentNodePrecedence
return (
(!isRightHand &&
nodePrecedence === 15 &&
parentNodePrecedence === 14 &&
parentNode.operator === '**') ||
nodePrecedence < parentNodePrecedence
)
}
if (nodePrecedence !== 13 && nodePrecedence !== 14) {
// Not a `LogicalExpression` or `BinaryExpression`
Expand Down Expand Up @@ -843,17 +849,16 @@ export const baseGenerator = {
this[node.right.type](node.right, state)
},
BinaryExpression: (BinaryExpression = function(node, state) {
if (node.operator === 'in') {
const isIn = node.operator === 'in'
if (isIn) {
// Avoids confusion in `for` loops initializers
state.write('(')
formatBinaryExpressionPart(state, node.left, node, false)
state.write(' ' + node.operator + ' ')
formatBinaryExpressionPart(state, node.right, node, true)
}
formatBinaryExpressionPart(state, node.left, node, false)
state.write(' ' + node.operator + ' ')
formatBinaryExpressionPart(state, node.right, node, true)
if (isIn) {
state.write(')')
} else {
formatBinaryExpressionPart(state, node.left, node, false)
state.write(' ' + node.operator + ' ')
formatBinaryExpressionPart(state, node.right, node, true)
}
}),
LogicalExpression: BinaryExpression,
Expand Down
2 changes: 2 additions & 0 deletions src/tests/fixtures/syntax/precedence.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ var r = (/ab+c/i).exec('abc');
a = b ** 2 * 3;
c = (d ** 2) ** 3;
e = f ** 2 ** 3;
e = (+2) ** 3;
e = 2 ** +3;
f = a + (b = 3);
g = 1 && (() => {});
g = (() => {}) && 1;
8 changes: 7 additions & 1 deletion src/tests/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ test('Syntax check', assert => {
files.forEach(filename => {
const code = readFile(path.join(dirname, filename))
const ast = parse(code, options)
assert.is(generate(ast), code, filename.substring(0, filename.length - 3))
assert.is(
generate(ast),
code,
filename.substring(0, filename.length - 3),
'Generates code with the expected format',
)
})
})

Expand All @@ -60,6 +65,7 @@ test('Tree comparison', assert => {
formattedAst,
ast,
filename.substring(0, filename.length - 3),
'Generates code with the same meaning',
)
})
})
Expand Down

0 comments on commit b267927

Please sign in to comment.