Skip to content

Fix repeated blank lines before exported decorators - #20180

Merged
fisker merged 13 commits into
prettier:mainfrom
charan-rathore:fix-export-decorator-blank-lines-20175
Oct 13, 2026
Merged

fisker merged 13 commits into
prettier:mainfrom
charan-rathore:fix-export-decorator-blank-lines-20175

Conversation

@charan-rathore

Copy link
Copy Markdown
Contributor

Description

Fixes #20175. A leading comment between export and a decorator already prints its own separator. The decorator printer added a hardline too, so formatting the result again added another blank line. It now leaves that extra hardline out when the comment sits before the first decorator. This preserves a blank line if one was already in the input and leaves export declarations without that comment unchanged.

The TypeScript fixture covers both issue examples, a pre-existing blank line, multiple decorators, default export, and a comment after a decorator. The snapshot and formatter's two-pass checks confirm the output is stable. Inline block comments remain inline with the decorator, rather than using the multiline layout suggested in the issue.

Instinct assisted with the change and tests.

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). (Not applicable: no API or CLI change.)
  • (If the change is user-facing) I've added my changes to changelog_unreleased/*/XXXX.md file following changelog_unreleased/TEMPLATE.md. (Rename 20175.md and heading to the PR number when known.)
  • I've read the contributing guidelines. (Charan should check this himself.)
  • I did not use AI to generate this PR.
  • (If the above is not checked) I have reviewed the AI-generated content before submitting. (Leave unchecked until Charan personally reviews and can attest.)

Added locStart import and improved comment handling for decorators.
Prettier now maintains comment placement before exported decorators, preventing additional blank lines during formatting.
@agentscanapp

agentscanapp Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Updated changelog entry for issue prettier#20180 to reflect changes regarding comments before exported decorators.
@netlify

netlify Bot commented Sep 29, 2026

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit 056f3cd
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6abb55651dc207000823c173
😎 Deploy Preview https://deploy-preview-20180--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.

@netlify

netlify Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Name Link
🔨 Latest commit 7e5158b
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/6ace0802cc8da6844fa28d65
😎 Deploy Preview https://deploy-preview-20180--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.

@charan-rathore

Copy link
Copy Markdown
Contributor Author

Sorry, this was closed by mistake, reopening.

Comment thread src/language-js/print/decorators.js Outdated
return [
path.key === "declaration" && isExportDeclaration(parent)
? hardline
? hasCommentBeforeDecorator

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.

Does it work if we only check that there is a leading comment?

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.

Yes, it works. A comment placed after a decorator does not attach as a leading comment of the declaration, so the position comparison was redundant. Simplified in ddef773: the check is now just the presence of a leading comment, the unused locStart import is dropped, and the decorator, decorator-comment, and comment format suites all pass unchanged (2,234 tests).

A comment placed after a decorator does not attach as a leading comment
of the declaration, so comparing its position against the first
decorator was redundant. Checking for the presence of a leading
comment covers the same cases.

Signed-off-by: Charan Rathore <[email protected]>
Comment thread src/language-js/print/decorators.js Outdated
// line on each formatting pass.
const hasCommentBeforeDecorator = isNonEmptyArray(
getComments(node, CommentCheckFlags.Leading),
);

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.

hasComment(node, CommentCheckFlags.Leading)

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.

Done in 9b19af1 - now on hasComment(node, CommentCheckFlags.Leading). The decorator and comment format suites still pass unchanged.

@pkg-pr-new

pkg-pr-new Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

yarn add https://pkg.pr.new/@prettier/[email protected]
yarn add https://pkg.pr.new/@prettier/[email protected]
yarn add https://pkg.pr.new/@prettier/[email protected]
yarn add https://pkg.pr.new/[email protected]

commit: 7e5158b

@fisker
fisker merged commit 837f6d4 into prettier:main Oct 13, 2026
32 of 33 checks passed
@charan-rathore
charan-rathore deleted the fix-export-decorator-blank-lines-20175 branch October 13, 2026 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Comment between export and a decorator adds a blank line on every format

2 participants