diff --git a/crates/oxc_formatter/src/ast_nodes/iterator.rs b/crates/oxc_formatter/src/ast_nodes/iterator.rs index 779b39d747901..20a6b04e2e6ef 100644 --- a/crates/oxc_formatter/src/ast_nodes/iterator.rs +++ b/crates/oxc_formatter/src/ast_nodes/iterator.rs @@ -196,8 +196,8 @@ macro_rules! impl_ast_node_vec_for_option { parent: self.parent, allocator: self.allocator, following_span_start: inner_iter - .next() - .and_then(|opt| opt.as_ref().map(|n| n.span().start)) + // Skip over `None` (elision) to find the next real element's span + .find_map(|opt| opt.as_ref().map(|n| n.span().start)) .unwrap_or(following), } })) @@ -228,11 +228,25 @@ macro_rules! impl_ast_node_vec_for_option { let following = self.following_span_start; let get_span = self.get_following_span_start; allocator - .alloc(self.inner.next().map(|inner| AstNode { - parent: self.parent, - inner, - allocator, - following_span_start: self.inner.peek().map_or(following, |n| get_span(*n)), + .alloc(self.inner.next().map(|inner| { + AstNode { + parent: self.parent, + inner, + allocator, + following_span_start: match self.inner.peek().map(|n| get_span(n)) { + Some(span) if span != 0 => span, + // Skip over `None` (elision) to find the next real element's span + Some(_) => self + .inner + .clone() + .find_map(|n| { + let span = get_span(n); + (span != 0).then_some(span) + }) + .unwrap_or(following), + None => following, + }, + } })) .as_ref() } diff --git a/crates/oxc_formatter/tests/fixtures/ts/array-elision/issue-20962.ts b/crates/oxc_formatter/tests/fixtures/ts/array-elision/issue-20962.ts new file mode 100644 index 0000000000000..3c533c7cdf604 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/array-elision/issue-20962.ts @@ -0,0 +1,11 @@ +const foo = { + createNewConnection: ([ + password, + , + // @ts-expect-error THIS SHOULD STAY HERE + username, + + ]) => { + void password; void username; + }, +} diff --git a/crates/oxc_formatter/tests/fixtures/ts/array-elision/issue-20962.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/array-elision/issue-20962.ts.snap new file mode 100644 index 0000000000000..cced81c043be2 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/array-elision/issue-20962.ts.snap @@ -0,0 +1,48 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +const foo = { + createNewConnection: ([ + password, + , + // @ts-expect-error THIS SHOULD STAY HERE + username, + + ]) => { + void password; void username; + }, +} + +==================== Output ==================== +------------------ +{ printWidth: 80 } +------------------ +const foo = { + createNewConnection: ([ + password, + , + // @ts-expect-error THIS SHOULD STAY HERE + username, + ]) => { + void password; + void username; + }, +}; + +------------------- +{ printWidth: 100 } +------------------- +const foo = { + createNewConnection: ([ + password, + , + // @ts-expect-error THIS SHOULD STAY HERE + username, + ]) => { + void password; + void username; + }, +}; + +===================== End =====================