Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions crates/oxc_formatter/src/ast_nodes/generated/ast_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4274,13 +4274,7 @@ impl<'a> AstNode<'a, FormalParameters<'a>> {

#[inline]
pub fn items(&self) -> &AstNode<'a, Vec<'a, FormalParameter<'a>>> {
let following_span_start = self
.inner
.rest
.as_deref()
.map(|n| n.span().start)
.or(Some(self.following_span_start))
.unwrap_or(0);
let following_span_start = self.inner.rest.as_deref().map_or(0, |n| n.span().start);
self.allocator.alloc(AstNode {
inner: &self.inner.items,
allocator: self.allocator,
Expand All @@ -4291,7 +4285,7 @@ impl<'a> AstNode<'a, FormalParameters<'a>> {

#[inline]
pub fn rest(&self) -> Option<&AstNode<'a, FormalParameterRest<'a>>> {
let following_span_start = self.following_span_start;
let following_span_start = 0;
self.allocator
.alloc(self.inner.rest.as_ref().map(|inner| AstNode {
inner: inner.as_ref(),
Expand Down
5 changes: 4 additions & 1 deletion crates/oxc_formatter/src/ast_nodes/iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,6 @@ impl_ast_node_vec!(AssignmentTargetProperty<'a>);
impl_ast_node_vec!(VariableDeclarator<'a>);
impl_ast_node_vec!(SwitchCase<'a>);
impl_ast_node_vec!(BindingProperty<'a>);
impl_ast_node_vec!(FormalParameter<'a>);
impl_ast_node_vec!(ClassElement<'a>);
impl_ast_node_vec!(ImportDeclarationSpecifier<'a>);
impl_ast_node_vec!(ImportAttribute<'a>);
Expand All @@ -265,6 +264,10 @@ impl_ast_node_vec_for_option!(Option<BindingPattern<'a>>);
// Directive needs `following_span_start` to distinguish trailing comments from leading comments
// of the first statement. See the struct field comment for `following_span_start` for details.
impl_ast_node_vec!(Directive<'a>, has_following_span_in_the_last_item);
// FormalParameter needs `following_span_start` to correctly attribute comments between
// the last parameter and the rest parameter (e.g., `param, /** @type {string[]} */ ...rest`).
impl_ast_node_vec!(FormalParameter<'a>, has_following_span_in_the_last_item);

// Custom get_span for Statement to handle decorated exports.
// <https://github.com/oxc-project/oxc/issues/10409>
impl_ast_node_vec!(Statement<'a>, false, get_statement_span);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// type cast on rest parameter should stay after comma
(/** @type {string} */ param, /** @type {string[]} */ ...rest) => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
source: crates/oxc_formatter/tests/fixtures/mod.rs
---
==================== Input ====================
// type cast on rest parameter should stay after comma
(/** @type {string} */ param, /** @type {string[]} */ ...rest) => {};

==================== Output ====================
------------------
{ printWidth: 80 }
------------------
// type cast on rest parameter should stay after comma
(/** @type {string} */ param, /** @type {string[]} */ ...rest) => {};

-------------------
{ printWidth: 100 }
-------------------
// type cast on rest parameter should stay after comma
(/** @type {string} */ param, /** @type {string[]} */ ...rest) => {};

===================== End =====================
8 changes: 5 additions & 3 deletions tasks/ast_tools/src/generators/formatter/ast_nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ pub fn get_node_type(ty: &TokenStream) -> TokenStream {
quote! { AstNode<'a, #ty> }
}

/// Based on the printing comments algorithm, the last child of these AST nodes don't need to print comments.
/// Without following nodes could lead to only print comments that before the end of the node, which is what we want.
const AST_NODE_WITHOUT_FOLLOWING_NODE_LIST: &[&str] = &[];
/// AST nodes whose last child should have `following_span_start = 0`.
///
/// This ensures trailing comments are correctly attributed to the last child itself,
/// rather than being treated as leading comments of a following sibling outside the parent.
const AST_NODE_WITHOUT_FOLLOWING_NODE_LIST: &[&str] = &["FormalParameters"];

const AST_NODE_WITH_FOLLOWING_NODE_LIST: &[&str] = &["Function", "Class"];

Expand Down
Loading