From 2daeda78401ab2b850ed472d8c04f44daf5e2746 Mon Sep 17 00:00:00 2001 From: Devon Govett Date: Sun, 3 Jan 2021 19:51:14 -0500 Subject: [PATCH] Parenthesize unary expressions of prefix operators --- src/astring.js | 8 +++----- src/tests/fixtures/syntax/precedence.js | 7 ++++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/astring.js b/src/astring.js index 2d1e6717..ff5b62cb 100644 --- a/src/astring.js +++ b/src/astring.js @@ -830,15 +830,13 @@ export const baseGenerator = { UnaryExpression(node, state) { if (node.prefix) { state.write(node.operator) - if ( - node.operator.length > 1 || - node.argument.type === 'UnaryExpression' - ) { + if (node.operator.length > 1) { state.write(' ') } if ( EXPRESSIONS_PRECEDENCE[node.argument.type] < - EXPRESSIONS_PRECEDENCE.UnaryExpression + EXPRESSIONS_PRECEDENCE.UnaryExpression || + node.argument.prefix ) { state.write('(') this[node.argument.type](node.argument, state) diff --git a/src/tests/fixtures/syntax/precedence.js b/src/tests/fixtures/syntax/precedence.js index 2d6d7fbd..c2d9eb9d 100644 --- a/src/tests/fixtures/syntax/precedence.js +++ b/src/tests/fixtures/syntax/precedence.js @@ -14,7 +14,7 @@ a = !false; b = !x instanceof Number; c = !(x instanceof Number); d = typeof a === 'boolean'; -e = ! typeof a === 'boolean'; +e = !(typeof a) === 'boolean'; f = !(typeof a === 'boolean'); a = (1.1).toString(); b = new A().toString(); @@ -29,5 +29,6 @@ e = 2 ** +3; f = a + (b = 3); g = 1 && (() => {}); g = (() => {}) && 1; -g = (1, + +2); -g = (1, + +(2 + 3)); +g = (1, +(+2)); +g = (1, +(+(2 + 3))); +a = -(--i);