Skip to content

Commit 8fbddb1

Browse files
committed
fix(formatter/jsdoc): more alignment with original plugin (#27039)
- Handle non-leading JSDoc and also legal - Keep trailing 2 spaces (for hard break) - Remove useless spaces when they can hug
1 parent 50be18e commit 8fbddb1

8 files changed

Lines changed: 79 additions & 19 deletions

File tree

‎crates/oxc_formatter/src/formatter/jsdoc/mdast_serialize/detect.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ use super::super::markers::{ListMarker, list_marker};
1212
/// Author opt-outs (`\- ` escape, blank line before an intended list) are pinned in
1313
/// `descriptions/043-list-interrupting-continuation`.
1414
pub(super) fn needs_mdast_parsing(text: &str) -> bool {
15+
// Hard line break by 2 trailing spaces
16+
if text.contains(" \n") {
17+
return true;
18+
}
1519
let bytes = text.as_bytes();
1620
let len = bytes.len();
1721
let mut i = 0;

‎crates/oxc_formatter/src/formatter/jsdoc/tag_formatters.rs‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,10 +473,13 @@ impl JsdocFormatter<'_, '_> {
473473
}
474474

475475
// Split description into first line and rest (avoids collecting all lines)
476-
let (first_text_line, rest_of_desc) = match desc_raw.split_once('\n') {
477-
Some((first, rest)) => (first.trim(), Some(rest)),
478-
None => (desc_raw.trim(), None),
476+
let (first_line, rest_of_desc) = match desc_raw.split_once('\n') {
477+
Some((first, rest)) => (first, Some(rest)),
478+
None => (desc_raw, None),
479479
};
480+
let first_text_line = first_line.trim();
481+
// Keep a Markdown hard line break, which `trim()` drops
482+
let first_hard_break = rest_of_desc.is_some() && first_line.ends_with(" ");
480483

481484
// If the description starts with a code fence, output the tag line alone
482485
// and treat the entire description as structural content with a blank line separator
@@ -626,6 +629,9 @@ impl JsdocFormatter<'_, '_> {
626629
let mut s = if has_remaining {
627630
let mut s = String::with_capacity(first_text.len() + 1 + remaining_desc.len());
628631
s.push_str(&first_text);
632+
if first_hard_break {
633+
s.push_str(" ");
634+
}
629635
s.push('\n');
630636
s.push_str(&remaining_desc);
631637
s
@@ -1130,8 +1136,11 @@ impl JsdocFormatter<'_, '_> {
11301136
}
11311137

11321138
let fits_on_one_line = prefix_len + str_width(&desc_text) <= self.wrap_width;
1133-
if fits_on_one_line || skip_wrapping {
1134-
// Fits on one line, or tag skips description formatting (no wrapping).
1139+
// Other descriptions go through wrapping, which collapses line breaks and spaces
1140+
let is_plain_one_liner =
1141+
fits_on_one_line && !desc_text.contains('\n') && !desc_text.contains(" ");
1142+
if is_plain_one_liner || skip_wrapping {
1143+
// Fits on one line as is, or tag skips description formatting (no wrapping).
11351144
// Tags in TAGS_PEV_FORMATE_DESCRIPTION (e.g. @see) and unknown tags
11361145
// keep their description on one line regardless of length.
11371146
// When desc has embedded newlines (e.g. @module Name\n\nMore text),

‎crates/oxc_formatter/src/formatter/trivia.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,7 @@ impl<'a> Format<'a, JsFormatContext<'a>> for FormatCommentText<'_> {
521521
}
522522

523523
// JSDoc formatting: if enabled, try to format JSDoc comments
524-
let formatted_jsdoc = if comment.is_jsdoc()
525-
&& !comment.is_legal()
524+
let formatted_jsdoc = if is_jsdoc_comment(comment)
526525
&& let Some(jsdoc_options) = &f.options().jsdoc
527526
{
528527
let source: &str = &f.source_text();
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* JSDoc with new line using double space
3+
* It is on another line
4+
*
5+
* @param a first
6+
* Continued
7+
*/
8+
function f(a) {}
9+
10+
/***
11+
* triple
12+
* Next
13+
*/
14+
function g() {}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* JSDoc with new line using double space\
3+
* It is on another line
4+
*
5+
* @param a First\
6+
* Continued
7+
*/
8+
function f(a) {}
9+
10+
/**
11+
* Triple\
12+
* Next
13+
*/
14+
function g() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/** @license MIT
2+
* some text */
3+
const a = 1;
4+
5+
/** @author MIT
6+
* some text */
7+
const b = 1;
8+
9+
const c = 1; /** trailing doc
10+
* next */
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/** @license MIT some text */
2+
const a = 1;
3+
4+
/** @author MIT Some text */
5+
const b = 1;
6+
7+
const c = 1; /** Trailing doc next */

‎crates/oxc_jsdoc/src/parser/jsdoc_parts.rs‎

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,23 @@ impl<'a> JSDocCommentPart<'a> {
105105
result.push('\n');
106106
}
107107
let trimmed = line.trim();
108-
if let Some(rest) = trimmed.strip_prefix('*') {
109-
// Strip `*` as a comment continuation prefix UNLESS it looks like
110-
// markdown emphasis (`*word*`). Emphasis has `*` followed by an
111-
// alphanumeric char; continuation prefixes have `*` followed by
112-
// space, backtick, punctuation, or nothing.
113-
let is_emphasis = rest.starts_with(|c: char| c.is_alphanumeric() || c == '_');
114-
if !is_emphasis {
115-
// Strip at most one leading space after `*` (the conventional ` * ` prefix)
116-
// to preserve any additional indentation (e.g. for indented code blocks)
117-
result.push_str(rest.strip_prefix(' ').unwrap_or(rest));
118-
continue;
108+
let content = match trimmed.strip_prefix('*') {
109+
// Strip `*` as a comment continuation prefix
110+
// UNLESS it looks like markdown emphasis (`*word*`).
111+
// Emphasis has `*` followed by an alphanumeric char;
112+
// continuation prefixes have `*` followed by space, backtick, punctuation, or nothing.
113+
// Strip at most one leading space after `*` (the conventional ` * ` prefix)
114+
// to preserve any additional indentation (e.g. for indented code blocks)
115+
Some(rest) if !rest.starts_with(|c: char| c.is_alphanumeric() || c == '_') => {
116+
rest.strip_prefix(' ').unwrap_or(rest)
119117
}
118+
_ => trimmed,
119+
};
120+
result.push_str(content);
121+
// Keep a Markdown hard line break, as 2 trailing spaces
122+
if line.ends_with(" ") && !content.is_empty() {
123+
result.push_str(" ");
120124
}
121-
result.push_str(trimmed);
122125
}
123126
result
124127
}

0 commit comments

Comments
 (0)