Skip to content

Preserve trailing double spaces in JSDoc - #18594

Merged
fisker merged 20 commits into
prettier:mainfrom
seiyab:preserve-jsdoc-trailing-double-spaces
Jan 12, 2026
Merged

fisker merged 20 commits into
prettier:mainfrom
seiyab:preserve-jsdoc-trailing-double-spaces

Conversation

@seiyab

@seiyab seiyab commented Jan 9, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Closes: #6793

I'm not sure whether this is approved as feature by members. Feel free to reject this PR.

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.

@pkg-pr-new

pkg-pr-new Bot commented Jan 9, 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/[email protected]

commit: bc4eb17

@netlify

netlify Bot commented Jan 9, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for prettier ready!

Name Link
🔨 Latest commit bc4eb17
🔍 Latest deploy log https://app.netlify.com/projects/prettier/deploys/69651cc173e324000827a64f
😎 Deploy Preview https://deploy-preview-18594--prettier.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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

Comment thread src/language-js/print/comment.js Outdated
return [" ", line.trimStart()];
}
const content = [" ", line.trim()];
if (isJSDoc && line.endsWith(" ")) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There's another choice: ignore after tag (e.g * @type {any} - blah blah ).

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.

Let's not make things complicated; we can ignore the content.

Comment thread tests/format/js/comments/jsdoc.js Outdated
*
* @type {any}
*/
let b;

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.

Since the character is invisible, please use snippets to test https://github.com/prettier/prettier/blob/main/CONTRIBUTING.md#tests, and assert output directly.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I moved them to snippets.

Comment on lines +32 to +44
{
name: "Remove on blank line",
code: outdent`
/**
*${" ".repeat(2)}
*/
`,
output: outdent`
/**
*
*/\n
`,
},

@fisker fisker Jan 10, 2026 •

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.

How should this work? 0 or 2 or 3 spaces?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

There are more patterns safe to remove trailing double spaces in JSDoc.

/**
 * ```  
 *    ^^ spaces here aren't significant
 * ```
 *
 * >  
 * > ^^ spaces here aren't significant
 */

The best change is not obvious since exact behavior will require md parser finally. I'm considering:

  • It's worth to handle simple & practical edge case.
  • For complex edge cases, both false removal and false preservation won't be so harmful as long as they are rare.
    • For false removal, it's same as Prettier stable so not introdured by this change.
    • For false preservation, it's new bug introduced by this change. But users can remove them by themselves and Prettier won't revive them. It should rarely happen, though.

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.

Nah, too much, let's pretend it's not markdown. Only make sure we preserve trailing space. User can use a jsdoc plugin.

@fisker

fisker commented Jan 10, 2026

Copy link
Copy Markdown
Member

@seiyab I rewrote the tests to read more easily, and I have a question #18594 (comment), can you check how it works on blank line in markdown?

* This is a JSDoc comment with trailing spaces.
*/
let x;
```

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.

Let's use the API to show the difference

await require('prettier').format('/**\n * With 2 spaces   \n */', {parser: 'babel'});

# Prettier stable
// -> '/**\n * With 2 spaces  \n */\n'

# Prettier main
// -> '/**\n * With 2 spaces\n */\n'

seiyab and others added 3 commits January 11, 2026 14:41
Update code block syntax from JavaScript to Bash in JSDoc entry.
@fisker fisker changed the title Preserve jsdoc trailing double spaces Preserve trailing double spaces in JSDoc Jan 12, 2026
@fisker
fisker merged commit 303391a into prettier:main Jan 12, 2026
leaysgur added a commit to oxc-project/oxc that referenced this pull request Sep 25, 2026
Prettier keeps a trailing double space on the middle lines of a `/**`
comment (prettier/prettier#18594, in the pinned 3.9.6): it is a Markdown
hard line break that doc tooling renders (prettier/prettier#6793). We
printed every aligned line through `trim()`, so the spaces were dropped:

```tsx
/**
 * JSDoc with new line using double space  
 * It's on another line
 */
```

Match Prettier's `printIndentableBlockComment`: in a `/**` comment (not
`/***`), a middle line whose source ends with two or more spaces keeps
exactly two after its trimmed content. A bare `*` line, a single
trailing space, the opening line, and non-JSDoc block comments are
trimmed as before. The check is textual rather than `comment.is_jsdoc()`
because the parser also tags `/***` as JSDoc and Prettier's test is
textual. The printer never trims text at a line end, so writing the two
spaces before the next hard line break is enough.

Fixture `js/comments/jsdoc-trailing-double-space.js` mirrors Prettier's
`tests/format/js/comments/trailing-spaces` cases plus a plain `/*`
block; its output is byte-identical to Prettier 3.9.8. One line added to
`js/crlf/block-comment.js` pins the CRLF output. `cargo test -p
oxc_formatter`, clippy and fmt are clean; I did not run the
`tasks/coverage` idempotency suites locally.

Written with help from an AI assistant (Claude); reviewed and tested by
me.

Closes #26854

---------

Co-authored-by: leaysgur <[email protected]>
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.

JSDoc trailing whitespaces are deleted

2 participants