Skip to content

Commit ee4861a

Browse files
author
Declan Vong
authored
fix(comments): trim excessive whitespace at the start of multi-line block comments (#460)
1 parent 81462d5 commit ee4861a

File tree

2 files changed

+38
-19
lines changed

2 files changed

+38
-19
lines changed

src/generation/generate.rs

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6357,34 +6357,35 @@ fn gen_comment(comment: &Comment, context: &mut Context) -> Option<PrintItems> {
63576357

63586358
return Some(match comment.kind {
63596359
CommentKind::Block => {
6360-
if is_js_doc(&comment.text) {
6361-
gen_js_doc(comment, context)
6360+
if has_leading_astrisk_each_line(&comment.text) {
6361+
gen_js_doc_or_multiline_block(comment, context)
63626362
} else {
6363+
// Single-line comment block
63636364
ir_helpers::gen_js_like_comment_block(&comment.text)
63646365
}
63656366
}
63666367
CommentKind::Line => ir_helpers::gen_js_like_comment_line(&comment.text, context.config.comment_line_force_space_after_slashes),
63676368
});
63686369

6369-
fn is_js_doc(text: &str) -> bool {
6370-
// be strict about what a js doc is for now
6371-
if text.starts_with('*') && text.contains('\n') {
6372-
for line in text.trim().split('\n').skip(1) {
6373-
let first_non_whitespace = line.trim_start().chars().next();
6374-
if !matches!(first_non_whitespace, Some('*')) {
6375-
return false;
6376-
}
6377-
}
6370+
fn has_leading_astrisk_each_line(text: &str) -> bool {
6371+
if !text.contains('\n') {
6372+
return false;
6373+
}
63786374

6379-
true
6380-
} else {
6381-
false
6375+
for line in text.trim().split('\n') {
6376+
let first_non_whitespace = line.trim_start().chars().next();
6377+
if !matches!(first_non_whitespace, Some('*')) {
6378+
return false;
6379+
}
63826380
}
6381+
6382+
true
63836383
}
63846384
}
63856385

6386-
fn gen_js_doc(comment: &Comment, _context: &mut Context) -> PrintItems {
6387-
return lines_to_print_items(build_lines(comment));
6386+
fn gen_js_doc_or_multiline_block(comment: &Comment, _context: &mut Context) -> PrintItems {
6387+
let is_js_doc = comment.text.starts_with('*');
6388+
return lines_to_print_items(is_js_doc, build_lines(comment));
63886389

63896390
fn build_lines(comment: &Comment) -> Vec<&str> {
63906391
let mut lines: Vec<&str> = Vec::new();
@@ -6411,7 +6412,7 @@ fn gen_js_doc(comment: &Comment, _context: &mut Context) -> PrintItems {
64116412
0
64126413
}
64136414

6414-
fn lines_to_print_items(lines: Vec<&str>) -> PrintItems {
6415+
fn lines_to_print_items(is_js_doc: bool, lines: Vec<&str>) -> PrintItems {
64156416
let mut items = PrintItems::new();
64166417

64176418
items.push_str("/*");
@@ -6421,8 +6422,12 @@ fn gen_js_doc(comment: &Comment, _context: &mut Context) -> PrintItems {
64216422
items.push_signal(Signal::NewLine);
64226423
}
64236424
let mut text = String::new();
6424-
// leading asterisk
6425-
text.push_str(if i == 0 { "*" } else { " *" });
6425+
// leading asterisk on the first line for jsdoc only
6426+
if is_js_doc && i == 0 {
6427+
text.push_str("*");
6428+
} else if i > 0 {
6429+
text.push_str(" *");
6430+
}
64266431

64276432
// line start space
64286433
let is_space_or_asterisk = matches!(line.chars().next(), Some('*' | ' '));

tests/specs/comments/CommentBlocks_All.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,20 @@ class Test {
7373
class Test {
7474
}
7575

76+
== should handle changing indentation of block comments ==
77+
/*
78+
* A
79+
* B
80+
*/
81+
const t;
82+
83+
[expect]
84+
/*
85+
* A
86+
* B
87+
*/
88+
const t;
89+
7690
== should format comments in arguments ==
7791
call(/* test */5);
7892

0 commit comments

Comments
 (0)