diff --git a/crates/oxc_formatter/src/parentheses/expression.rs b/crates/oxc_formatter/src/parentheses/expression.rs index 4d3c9af9f6713..7c7a01dca2fb1 100644 --- a/crates/oxc_formatter/src/parentheses/expression.rs +++ b/crates/oxc_formatter/src/parentheses/expression.rs @@ -416,6 +416,7 @@ impl<'a> NeedsParentheses<'a> for AstNode<'a, PrivateInExpression<'a>> { } is_class_extends(self.span, self.parent) + || matches!(self.parent, AstNodes::UnaryExpression(_)) } } diff --git a/crates/oxc_formatter/tests/fixtures/js/private-fields/parenthesis.js b/crates/oxc_formatter/tests/fixtures/js/private-fields/parenthesis.js new file mode 100644 index 0000000000000..838381a09f102 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/private-fields/parenthesis.js @@ -0,0 +1,8 @@ +class F { + #target = null + next () { + !(#target in this); + -(#target in this); + +(#target in this); + } +} diff --git a/crates/oxc_formatter/tests/fixtures/js/private-fields/parenthesis.js.snap b/crates/oxc_formatter/tests/fixtures/js/private-fields/parenthesis.js.snap new file mode 100644 index 0000000000000..e4b2129ccc4be --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/js/private-fields/parenthesis.js.snap @@ -0,0 +1,24 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +class F { + #target = null + next () { + !(#target in this); + -(#target in this); + +(#target in this); + } +} + +==================== Output ==================== +class F { + #target = null; + next() { + !(#target in this); + -(#target in this); + +(#target in this); + } +} + +===================== End =====================