From 40d4bb181548a4e8751ef1ced6959c46ecdebcc3 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 16 Nov 2025 20:37:39 +0800 Subject: [PATCH] Remove redundant parentheses around JSX element (#18243) --- changelog_unreleased/javascript/18243.md | 27 +++++++++ src/language-js/print/jsx.js | 1 + .../__snapshots__/format.test.js.snap | 59 +++++++++++++++++++ tests/format/jsx/parentheses/argument.js | 23 ++++++++ tests/format/jsx/parentheses/format.test.js | 15 +++++ 5 files changed, 125 insertions(+) create mode 100644 changelog_unreleased/javascript/18243.md create mode 100644 tests/format/jsx/parentheses/__snapshots__/format.test.js.snap create mode 100644 tests/format/jsx/parentheses/argument.js create mode 100644 tests/format/jsx/parentheses/format.test.js diff --git a/changelog_unreleased/javascript/18243.md b/changelog_unreleased/javascript/18243.md new file mode 100644 index 000000000..e690584b8 --- /dev/null +++ b/changelog_unreleased/javascript/18243.md @@ -0,0 +1,27 @@ +#### Remove redundant parentheses around JSX element (#18243 by @fisker) + + +```jsx +// Input +new A( +
+
+
+) + +// Prettier stable +new A( + ( +
+
+
+ ), +); + +// Prettier main +new A( +
+
+
, +); +``` diff --git a/src/language-js/print/jsx.js b/src/language-js/print/jsx.js index 60d36b7ba..b3f5644d5 100644 --- a/src/language-js/print/jsx.js +++ b/src/language-js/print/jsx.js @@ -490,6 +490,7 @@ const isNoWrapParent = createTypeCheckFunction([ "JSXExpressionContainer", "JSXFragment", "ExpressionStatement", + "NewExpression", "CallExpression", "OptionalCallExpression", "ConditionalExpression", diff --git a/tests/format/jsx/parentheses/__snapshots__/format.test.js.snap b/tests/format/jsx/parentheses/__snapshots__/format.test.js.snap new file mode 100644 index 000000000..086d54606 --- /dev/null +++ b/tests/format/jsx/parentheses/__snapshots__/format.test.js.snap @@ -0,0 +1,59 @@ +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing + +exports[`argument.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "babel-flow", "babel-ts", "__babel_estree", "typescript", "flow", "meriyah", "acorn", "espree", "espree", "hermes", "oxc", "oxc-ts"] +printWidth: 80 + | printWidth +=====================================input====================================== +new A( +
+
+
+) + +A( +
+
+
+) + +new A( + <> +
+ +) + +A( + <> +
+ +) + +=====================================output===================================== +new A( +
+
+
, +); + +A( +
+
+
, +); + +new A( + <> +
+ , +); + +A( + <> +
+ , +); + +================================================================================ +`; diff --git a/tests/format/jsx/parentheses/argument.js b/tests/format/jsx/parentheses/argument.js new file mode 100644 index 000000000..0cd8c1276 --- /dev/null +++ b/tests/format/jsx/parentheses/argument.js @@ -0,0 +1,23 @@ +new A( +
+
+
+) + +A( +
+
+
+) + +new A( + <> +
+ +) + +A( + <> +
+ +) diff --git a/tests/format/jsx/parentheses/format.test.js b/tests/format/jsx/parentheses/format.test.js new file mode 100644 index 000000000..a6dfd8599 --- /dev/null +++ b/tests/format/jsx/parentheses/format.test.js @@ -0,0 +1,15 @@ +runFormatTest(import.meta, [ + "babel", + "babel-flow", + "babel-ts", + "__babel_estree", + "typescript", + "flow", + "meriyah", + "acorn", + "espree", + "espree", + "hermes", + "oxc", + "oxc-ts", +]);