Visitar URL original
Keep comment after destructuring pattern out of the pattern by lazerg · Pull Request #19905 · prettier/prettier · GitHub
Skip to content

Keep comment after destructuring pattern out of the pattern - #19905

Merged
fisker merged 10 commits into
prettier:mainfrom
lazerg:fix/issue-19903-comment-in-empty-pattern
Aug 23, 2026
Merged

fisker merged 10 commits into
prettier:mainfrom
lazerg:fix/issue-19903-comment-in-empty-pattern

Conversation

@lazerg

@lazerg lazerg commented Aug 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Fixes #19903.

A pattern with a type annotation ends after the annotation, so a comment between the closing bracket and the : still has the pattern as its enclosing node and was printed inside the brackets. It now stays outside unless it really is before the closing bracket.

Checklist

  • I’ve added tests to confirm my change works.
  • (If changing the API or CLI) I’ve documented the changes I’ve made (in the docs/ directory).
  • (If the change is user-facing) I’ve added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md.
  • I’ve read the contributing guidelines.
  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting.

@netlify

netlify Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Name Link
🔨 Latest commit d63869c
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6a88c02c7d701d0008938e90
😎 Deploy Preview https://deploy-preview-19905--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new

pkg-pr-new Bot commented Aug 20, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

yarn add https://pkg.pr.new/@prettier/plugin-hermes@19905.tgz
yarn add https://pkg.pr.new/@prettier/plugin-oxc@19905.tgz
yarn add https://pkg.pr.new/@prettier/plugin-yuku@19905.tgz
yarn add https://pkg.pr.new/prettier@19905.tgz

commit: d63869c

@fisker

fisker commented Aug 20, 2026

Copy link
Copy Markdown
Member

Prettier PR #19905
Playground link

--parser typescript

Input:

const {foo}
  // bar
  : Foo = expr;

Output:

const {
  foo,
  // bar
}: Foo = expr;

@lazerg

lazerg commented Aug 20, 2026

Copy link
Copy Markdown
Contributor Author

Good catch, fixed in c484fb0. The check now runs before the preceding-node branch, so a comment after the closing bracket stays out of a non-empty pattern too. I also added the handler to the end-of-line list, otherwise the next format pass pulled the comment back in.

// const {foo}
// // bar
// : Foo = expr;
addLeadingComment(followingNode, comment);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if we remove addLeadingComment here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Works, thanks. I dropped the addLeadingComment and return false there instead, so the handler is a single condition now. Output is the same and tests/format stays at baseline. Commit 89e9bc6.

One note: the handleEndOfLineComment registration is still needed, for const { // bar followed by }: Foo = expr;. Without it that dangling comment moves outside the braces. I added the case to the fixture.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we can't remove it, otherwise

Input:

const {
  foo2,
} // bar
: Foo = expr;

Output:

const {
  foo2, // bar
}: Foo = expr;

Fix formatting of TypeScript destructuring assignment.
@fisker
fisker merged commit 11d5584 into prettier:main Aug 23, 2026
35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop moving comment into destructuring pattern

2 participants