perf(ast): generate derive for CloneIn on Comment (#27542)

Follow-on after #27512.

Generate the derive of `CloneIn`, rather than writing it out manually. The auto-generated version is more performant as it uses a branchless method to clone the `node_id` field.
This commit is contained in:
overlookmotel committed 2026-10-10 18:07:54 +00:00
1 parent 2a280a2d7e
commit 5487247ae5
2 files changed
+36 -14

No files matched your search

+2 -14
View File
@@ -163,7 +163,7 @@ impl<'alloc> CloneIn<'alloc> for CommentNewlines {
///
/// This is separate from the token-relative position stored on the source comment.
#[ast]
#[generate_derive(ContentEq)]
#[generate_derive(ContentEq, CloneIn)]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum CommentPlacement {
/// Before the node.
@@ -199,7 +199,7 @@ pub enum CommentPlacement {
///
/// Borrow comments when reading them, or use [`Clone::clone`] to obtain an owned value.
#[ast]
#[generate_derive(ContentEq, ESTree, GetSpan)]
#[generate_derive(ContentEq, CloneIn, ESTree, GetSpan)]
#[derive(Debug, Default, Clone, Eq, PartialEq)]
#[estree(add_fields(value = CommentValue), no_ts_def, no_parent)]
pub struct Comment {
@@ -421,15 +421,3 @@ impl Comment {
self.newlines.set(CommentNewlines::Trailing, followed_by_newline);
}
}
impl<'alloc> CloneIn<'alloc> for Comment {
type Cloned = Self;
fn clone_in_impl(&self, with_semantic_ids: CloneInSemanticIds, _: &'alloc Allocator) -> Self {
let comment = self.clone();
if with_semantic_ids == CloneInSemanticIds::Without {
comment.node_id.set(NodeId::DUMMY);
}
comment
}
}
@@ -5783,3 +5783,37 @@ impl<'new_alloc> CloneIn<'new_alloc> for CommentContent {
*self
}
}
impl<'new_alloc> CloneIn<'new_alloc> for CommentPlacement {
type Cloned = CommentPlacement;
#[inline(always)]
fn clone_in_impl(
&self,
_with_semantic_ids: CloneInSemanticIds,
allocator: &'new_alloc Allocator,
) -> Self::Cloned {
*self
}
}
impl<'new_alloc> CloneIn<'new_alloc> for Comment {
type Cloned = Comment;
fn clone_in_impl(
&self,
with_semantic_ids: CloneInSemanticIds,
allocator: &'new_alloc Allocator,
) -> Self::Cloned {
Comment {
span: CloneIn::clone_in_impl(&self.span, with_semantic_ids, allocator),
attached_to: CloneIn::clone_in_impl(&self.attached_to, with_semantic_ids, allocator),
node_id: CloneIn::clone_in_impl(&self.node_id, with_semantic_ids, allocator),
placement: CloneIn::clone_in_impl(&self.placement, with_semantic_ids, allocator),
kind: CloneIn::clone_in_impl(&self.kind, with_semantic_ids, allocator),
position: CloneIn::clone_in_impl(&self.position, with_semantic_ids, allocator),
newlines: CloneIn::clone_in_impl(&self.newlines, with_semantic_ids, allocator),
content: CloneIn::clone_in_impl(&self.content, with_semantic_ids, allocator),
}
}
}