From 6bc99db278c8acf5b0c12eb37527cb5e4a429c84 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Mon, 21 Sep 2026 23:20:48 +0800 Subject: [PATCH] Prevent comment after head of member chain move around (#20125) Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- changelog_unreleased/javascript/20125.md | 15 +++++++++++ src/language-js/print/member-chain.js | 11 ++++---- .../__snapshots__/format.test.js.snap | 27 +++++++++++++++++++ tests/format/js/method-chain/issue-18523.js | 8 ++++++ 4 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 changelog_unreleased/javascript/20125.md create mode 100644 tests/format/js/method-chain/issue-18523.js diff --git a/changelog_unreleased/javascript/20125.md b/changelog_unreleased/javascript/20125.md new file mode 100644 index 000000000..80231abd1 --- /dev/null +++ b/changelog_unreleased/javascript/20125.md @@ -0,0 +1,15 @@ +#### Prevent comment after head of member chain move around (#20125 by @fisker) + + +```jsx +// Input +x // comment + .foo() + +// Prettier stable +x.foo(); // comment + +// Prettier main +x // comment + .foo(); +``` diff --git a/src/language-js/print/member-chain.js b/src/language-js/print/member-chain.js index 284025500..d43be4f57 100644 --- a/src/language-js/print/member-chain.js +++ b/src/language-js/print/member-chain.js @@ -293,11 +293,12 @@ function printMemberChain(path, options, print) { if (groups[0].length === 1) { const firstNode = groups[0][0].node; return ( - firstNode.type === "ThisExpression" || - (firstNode.type === "Identifier" && - (isFactory(firstNode.name) || - (isExpressionStatement && isShort(firstNode.name)) || - hasComputed)) + (firstNode.type === "ThisExpression" || + (firstNode.type === "Identifier" && + (isFactory(firstNode.name) || + (isExpressionStatement && isShort(firstNode.name)) || + hasComputed))) && + !hasComment(firstNode, CommentCheckFlags.Trailing) ); } diff --git a/tests/format/js/method-chain/__snapshots__/format.test.js.snap b/tests/format/js/method-chain/__snapshots__/format.test.js.snap index e920577c9..4132e915e 100644 --- a/tests/format/js/method-chain/__snapshots__/format.test.js.snap +++ b/tests/format/js/method-chain/__snapshots__/format.test.js.snap @@ -1281,6 +1281,33 @@ s.get(u)?.trigger({ ================================================================================ `; +exports[`issue-18523.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +a // + .bla() + +abc // + .bla() + +this // + .bla() + +=====================================output===================================== +a // + .bla(); + +abc // + .bla(); + +this // + .bla(); + +================================================================================ +`; + exports[`logical.js format 1`] = ` ====================================options===================================== parsers: ["babel", "flow", "typescript"] diff --git a/tests/format/js/method-chain/issue-18523.js b/tests/format/js/method-chain/issue-18523.js new file mode 100644 index 000000000..b341df910 --- /dev/null +++ b/tests/format/js/method-chain/issue-18523.js @@ -0,0 +1,8 @@ +a // + .bla() + +abc // + .bla() + +this // + .bla()