diff --git a/crates/oxc_formatter/src/print/parameters.rs b/crates/oxc_formatter/src/print/parameters.rs index b523ed09acf11..7cc36a1fb9b25 100644 --- a/crates/oxc_formatter/src/print/parameters.rs +++ b/crates/oxc_formatter/src/print/parameters.rs @@ -114,7 +114,13 @@ impl<'a> FormatWrite<'a> for AstNode<'a, FormalParameter<'a>> { if self.optional { write!(f, "?"); } - write!(f, self.type_annotation()); + + if let Some(type_ann) = self.type_annotation() { + if f.comments().has_comment_before(type_ann.span().start) { + write!(f, space()); + } + write!(f, type_ann); + } }) .memoized(); diff --git a/crates/oxc_formatter/tests/fixtures/ts/function-parameters/comment-after-param-name.ts b/crates/oxc_formatter/tests/fixtures/ts/function-parameters/comment-after-param-name.ts new file mode 100644 index 0000000000000..c43f9c5e5feff --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/function-parameters/comment-after-param-name.ts @@ -0,0 +1,15 @@ +// Issue #18970 - comment between parameter name and type annotation +function f(x /* a */ : number) {} + +// Additional test cases +function g(y /* comment */ : string, z /* another */ : boolean) {} + +// With different comment styles +function h(a /* inline */ : number) {} + +// Multiple parameters with comments +const arrow = (x /* c1 */ : number, y /* c2 */ : string) => {}; + +// Optional parameters with comments +function optional(x? /* comment */ : number) {} +function optionalMultiple(a? /* c1 */ : string, b? /* c2 */ : number) {} diff --git a/crates/oxc_formatter/tests/fixtures/ts/function-parameters/comment-after-param-name.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/function-parameters/comment-after-param-name.ts.snap new file mode 100644 index 0000000000000..49b4466b45414 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/function-parameters/comment-after-param-name.ts.snap @@ -0,0 +1,60 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +// Issue #18970 - comment between parameter name and type annotation +function f(x /* a */ : number) {} + +// Additional test cases +function g(y /* comment */ : string, z /* another */ : boolean) {} + +// With different comment styles +function h(a /* inline */ : number) {} + +// Multiple parameters with comments +const arrow = (x /* c1 */ : number, y /* c2 */ : string) => {}; + +// Optional parameters with comments +function optional(x? /* comment */ : number) {} +function optionalMultiple(a? /* c1 */ : string, b? /* c2 */ : number) {} + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +// Issue #18970 - comment between parameter name and type annotation +function f(x /* a */ : number) {} + +// Additional test cases +function g(y /* comment */ : string, z /* another */ : boolean) {} + +// With different comment styles +function h(a /* inline */ : number) {} + +// Multiple parameters with comments +const arrow = (x /* c1 */ : number, y /* c2 */ : string) => {}; + +// Optional parameters with comments +function optional(x? /* comment */ : number) {} +function optionalMultiple(a? /* c1 */ : string, b? /* c2 */ : number) {} + +------------------- +{ printWidth: 100 } +------------------- +// Issue #18970 - comment between parameter name and type annotation +function f(x /* a */ : number) {} + +// Additional test cases +function g(y /* comment */ : string, z /* another */ : boolean) {} + +// With different comment styles +function h(a /* inline */ : number) {} + +// Multiple parameters with comments +const arrow = (x /* c1 */ : number, y /* c2 */ : string) => {}; + +// Optional parameters with comments +function optional(x? /* comment */ : number) {} +function optionalMultiple(a? /* c1 */ : string, b? /* c2 */ : number) {} + +===================== End =====================