Skip to content

Commit

Permalink
Support custom expression precedence
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett committed Jan 4, 2021
1 parent 6021bf0 commit db7c2fd
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions src/astring.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const OPERATOR_PRECEDENCE = {
// Enables parenthesis regardless of precedence
const NEEDS_PARENTHESES = 17

const EXPRESSIONS_PRECEDENCE = {
export const EXPRESSIONS_PRECEDENCE = {
// Definitions
ArrayExpression: 20,
TaggedTemplateExpression: 20,
Expand Down Expand Up @@ -106,12 +106,12 @@ function formatSequence(state, nodes) {
state.write(')')
}

function expressionNeedsParenthesis(node, parentNode, isRightHand) {
const nodePrecedence = EXPRESSIONS_PRECEDENCE[node.type]
function expressionNeedsParenthesis(state, node, parentNode, isRightHand) {
const nodePrecedence = state.expressionPrecedence[node.type]
if (nodePrecedence === NEEDS_PARENTHESES) {
return true
}
const parentNodePrecedence = EXPRESSIONS_PRECEDENCE[parentNode.type]
const parentNodePrecedence = state.expressionPrecedence[parentNode.type]
if (nodePrecedence !== parentNodePrecedence) {
// Different node types
return (
Expand Down Expand Up @@ -150,7 +150,7 @@ function formatBinaryExpressionPart(state, node, parentNode, isRightHand) {
The `isRightHand` parameter should be `true` if the `node` is a right-hand argument.
*/
const { generator } = state
if (expressionNeedsParenthesis(node, parentNode, isRightHand)) {
if (expressionNeedsParenthesis(state, node, parentNode, isRightHand)) {
state.write('(')
generator[node.type](node, state)
state.write(')')
Expand Down Expand Up @@ -302,7 +302,7 @@ export const baseGenerator = {
state.write(';')
},
ExpressionStatement(node, state) {
const precedence = EXPRESSIONS_PRECEDENCE[node.expression.type]
const precedence = state.expressionPrecedence[node.expression.type]
if (
precedence === NEEDS_PARENTHESES ||
(precedence === 3 && node.expression.left.type[0] === 'O')
Expand Down Expand Up @@ -565,7 +565,7 @@ export const baseGenerator = {
state.write('export default ')
this[node.declaration.type](node.declaration, state)
if (
EXPRESSIONS_PRECEDENCE[node.declaration.type] &&
state.expressionPrecedence[node.declaration.type] &&
node.declaration.type[0] !== 'F'
) {
// All expression nodes except `FunctionExpression`
Expand Down Expand Up @@ -888,7 +888,7 @@ export const baseGenerator = {
LogicalExpression: BinaryExpression,
ConditionalExpression(node, state) {
if (
EXPRESSIONS_PRECEDENCE[node.test.type] >
state.expressionPrecedence[node.test.type] >
EXPRESSIONS_PRECEDENCE.ConditionalExpression
) {
this[node.test.type](node.test, state)
Expand All @@ -905,7 +905,7 @@ export const baseGenerator = {
NewExpression(node, state) {
state.write('new ')
if (
EXPRESSIONS_PRECEDENCE[node.callee.type] <
state.expressionPrecedence[node.callee.type] <
EXPRESSIONS_PRECEDENCE.CallExpression ||
hasCallExpression(node.callee)
) {
Expand All @@ -919,7 +919,7 @@ export const baseGenerator = {
},
CallExpression(node, state) {
if (
EXPRESSIONS_PRECEDENCE[node.callee.type] <
state.expressionPrecedence[node.callee.type] <
EXPRESSIONS_PRECEDENCE.CallExpression
) {
state.write('(')
Expand All @@ -938,7 +938,7 @@ export const baseGenerator = {
},
MemberExpression(node, state) {
if (
EXPRESSIONS_PRECEDENCE[node.object.type] <
state.expressionPrecedence[node.object.type] <
EXPRESSIONS_PRECEDENCE.MemberExpression
) {
state.write('(')
Expand Down Expand Up @@ -1001,6 +1001,10 @@ class State {
this.output = ''
}
this.generator = setup.generator != null ? setup.generator : baseGenerator
this.expressionPrecedence =
setup.expressionPrecedence != null
? setup.expressionPrecedence
: EXPRESSIONS_PRECEDENCE
// Formating setup
this.indent = setup.indent != null ? setup.indent : ' '
this.lineEnd = setup.lineEnd != null ? setup.lineEnd : '\n'
Expand Down

0 comments on commit db7c2fd

Please sign in to comment.