Skip to content

Commit

Permalink
fix: adjust await and yield operator precendences
Browse files Browse the repository at this point in the history
Fixes #435

- Set their precedence similar to binary operators.
- Moved async fixtures to precedence and function fixtures.
- Expanded precedence fixture with more await and yield operators.
  • Loading branch information
davidbonnet committed Feb 3, 2021
1 parent 00690ab commit d6166a2
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 27 deletions.
22 changes: 7 additions & 15 deletions src/astring.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,12 @@ export const EXPRESSIONS_PRECEDENCE = {
// Other operations
UpdateExpression: 16,
UnaryExpression: 15,
AwaitExpression: 15,
YieldExpression: 15,
BinaryExpression: 14,
LogicalExpression: 13,
ConditionalExpression: 4,
AssignmentExpression: 3,
AwaitExpression: 2,
YieldExpression: 2,
RestElement: 1,
}

Expand Down Expand Up @@ -143,7 +143,7 @@ function expressionNeedsParenthesis(state, node, parentNode, isRightHand) {
)
}

function formatBinaryExpressionPart(state, node, parentNode, isRightHand) {
function formatExpression(state, node, parentNode, isRightHand) {
/*
Writes into `state` a left-hand or right-hand expression `node`
from a binary expression applying the provided `operator`.
Expand Down Expand Up @@ -568,7 +568,7 @@ export const GENERATOR = {
state.write('export default ')
this[node.declaration.type](node.declaration, state)
if (
state.expressionsPrecedence[node.declaration.type] &&
state.expressionsPrecedence[node.declaration.type] != null &&
node.declaration.type[0] !== 'F'
) {
// All expression nodes except `FunctionExpression`
Expand Down Expand Up @@ -686,15 +686,7 @@ export const GENERATOR = {
},
AwaitExpression(node, state) {
state.write('await ', node)
if (node.argument) {
if (node.argument.type === 'ArrowFunctionExpression') {
state.write('(')
this[node.argument.type](node.argument, state)
state.write(')')
} else {
this[node.argument.type](node.argument, state)
}
}
formatExpression(state, node.argument, node)
},
TemplateLiteral(node, state) {
const { quasis, expressions } = node
Expand Down Expand Up @@ -891,9 +883,9 @@ export const GENERATOR = {
// Avoids confusion in `for` loops initializers
state.write('(')
}
formatBinaryExpressionPart(state, node.left, node, false)
formatExpression(state, node.left, node, false)
state.write(' ' + node.operator + ' ')
formatBinaryExpressionPart(state, node.right, node, true)
formatExpression(state, node.right, node, true)
if (isIn) {
state.write(')')
}
Expand Down
11 changes: 0 additions & 11 deletions src/tests/fixtures/syntax/async.js

This file was deleted.

12 changes: 11 additions & 1 deletion src/tests/fixtures/syntax/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ function* o() {
}
function* p() {
yield 42;
yield 7;
yield 4 + 2;
return "answer";
}
async function a() {}
const b = async function () {};
const c = {
async f() {}
};
const d = async () => {};
async function e() {
await a + await b;
return await f();
}
let q = function* () {};
let r = a => a;
let s = (a, b) => a + b;
Expand Down
10 changes: 10 additions & 0 deletions src/tests/fixtures/syntax/precedence.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ d = !!a;
e = !+-+!a;
f = -+-a++;
g = b + -+-a++;
(async function* () {
await a + b;
await a + await b;
await (a = b);
(await f()).a;
await f().a;
yield 1;
yield 1 + 2;
const c = yield 3;
});

0 comments on commit d6166a2

Please sign in to comment.