diff --git a/crates/oxc_formatter/src/formatter/separated.rs b/crates/oxc_formatter/src/formatter/separated.rs index de55a47b5f54b..4058134f28103 100644 --- a/crates/oxc_formatter/src/formatter/separated.rs +++ b/crates/oxc_formatter/src/formatter/separated.rs @@ -1,3 +1,5 @@ +use std::ops::Deref; + use oxc_span::{GetSpan, Span}; use crate::{ @@ -21,6 +23,14 @@ pub struct FormatSeparatedElement { options: FormatSeparatedOptions, } +impl Deref for FormatSeparatedElement { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.element + } +} + impl GetSpan for FormatSeparatedElement { fn span(&self) -> Span { self.element.span() diff --git a/crates/oxc_formatter/src/write/variable_declaration.rs b/crates/oxc_formatter/src/write/variable_declaration.rs index 2a885ecf8bc36..2e2fdcf70c837 100644 --- a/crates/oxc_formatter/src/write/variable_declaration.rs +++ b/crates/oxc_formatter/src/write/variable_declaration.rs @@ -69,7 +69,14 @@ impl<'a> Format<'a> for AstNode<'a, Vec<'a, VariableDeclarator<'a>>> { let first_declarator = declarators.next().unwrap(); if length == 1 && !f.comments().has_comment_before(first_declarator.span().start) { - return write!(f, first_declarator); + return if first_declarator.init.is_none() + && f.comments() + .has_comment_in_range(first_declarator.span.end, self.parent.span().end) + { + write!(f, indent(&first_declarator)); + } else { + write!(f, &first_declarator); + }; } write!( diff --git a/crates/oxc_formatter/tests/fixtures/ts/variable-declarations/issue-16193.ts b/crates/oxc_formatter/tests/fixtures/ts/variable-declarations/issue-16193.ts new file mode 100644 index 0000000000000..9af0a40c0514d --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/variable-declarations/issue-16193.ts @@ -0,0 +1,3 @@ +declare const PAGE_PATH: string + //<- THIS spaces +;(()=>{})() diff --git a/crates/oxc_formatter/tests/fixtures/ts/variable-declarations/issue-16193.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/variable-declarations/issue-16193.ts.snap new file mode 100644 index 0000000000000..fd9aa8f978305 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/variable-declarations/issue-16193.ts.snap @@ -0,0 +1,24 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +declare const PAGE_PATH: string + //<- THIS spaces +;(()=>{})() + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +declare const PAGE_PATH: string; + //<- THIS spaces +(() => {})(); + +------------------- +{ printWidth: 100 } +------------------- +declare const PAGE_PATH: string; + //<- THIS spaces +(() => {})(); + +===================== End =====================