feat(tasks,oxc_allocator): Add new method clone_in_with_semantic_ids for CloneIn trait (#9894)

This commit is contained in:
IWANABETHATGUY committed 2025-03-21 04:11:22 +00:00
1 parent 2e8198e6ae
commit bc0670c8a9
8 files changed
+4096 -7

No files matched your search

+19
View File
@@ -31,6 +31,13 @@ pub trait CloneIn<'new_alloc>: Sized {
/// Clone `self` into the given `allocator`. `allocator` may be the same one
/// that `self` is already in.
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned;
/// Almost identical as `clone_in`, but for some special type, it will also clone the semantic ids.
/// Please use this method only if you make sure semantic info is synced with the ast node.
#[inline]
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
self.clone_in(allocator)
}
}
impl<'alloc, T, C> CloneIn<'alloc> for Option<T>
@@ -42,6 +49,10 @@ where
fn clone_in(&self, allocator: &'alloc Allocator) -> Self::Cloned {
self.as_ref().map(|it| it.clone_in(allocator))
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
self.as_ref().map(|it| it.clone_in_with_semantic_ids(allocator))
}
}
impl<'new_alloc, T, C> CloneIn<'new_alloc> for Box<'_, T>
@@ -53,6 +64,10 @@ where
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Box::new_in(self.as_ref().clone_in(allocator), allocator)
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Box::new_in(self.as_ref().clone_in_with_semantic_ids(allocator), allocator)
}
}
impl<'new_alloc, T, C> CloneIn<'new_alloc> for Vec<'_, T>
@@ -67,6 +82,10 @@ where
fn clone_in(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Vec::from_iter_in(self.iter().map(|it| it.clone_in(allocator)), allocator)
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Vec::from_iter_in(self.iter().map(|it| it.clone_in_with_semantic_ids(allocator)), allocator)
}
}
impl<'alloc, T: Copy> CloneIn<'alloc> for Cell<T> {
File diff suppressed because it is too large. Load diff
@@ -15,6 +15,13 @@ impl<'new_alloc> CloneIn<'new_alloc> for Pattern<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Pattern {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for Disjunction<'_> {
@@ -25,6 +32,13 @@ impl<'new_alloc> CloneIn<'new_alloc> for Disjunction<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Disjunction {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for Alternative<'_> {
@@ -35,6 +49,13 @@ impl<'new_alloc> CloneIn<'new_alloc> for Alternative<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Alternative {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for Term<'_> {
@@ -63,6 +84,45 @@ impl<'new_alloc> CloneIn<'new_alloc> for Term<'_> {
Self::NamedReference(it) => Term::NamedReference(CloneIn::clone_in(it, allocator)),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
match self {
Self::BoundaryAssertion(it) => {
Term::BoundaryAssertion(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::LookAroundAssertion(it) => {
Term::LookAroundAssertion(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::Quantifier(it) => {
Term::Quantifier(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::Character(it) => {
Term::Character(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::Dot(it) => Term::Dot(CloneIn::clone_in_with_semantic_ids(it, allocator)),
Self::CharacterClassEscape(it) => {
Term::CharacterClassEscape(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::UnicodePropertyEscape(it) => {
Term::UnicodePropertyEscape(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::CharacterClass(it) => {
Term::CharacterClass(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::CapturingGroup(it) => {
Term::CapturingGroup(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::IgnoreGroup(it) => {
Term::IgnoreGroup(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::IndexedReference(it) => {
Term::IndexedReference(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
Self::NamedReference(it) => {
Term::NamedReference(CloneIn::clone_in_with_semantic_ids(it, allocator))
}
}
}
}
impl<'alloc> CloneIn<'alloc> for BoundaryAssertion {
@@ -73,6 +133,13 @@ impl<'alloc> CloneIn<'alloc> for BoundaryAssertion {
kind: CloneIn::clone_in(&self.kind, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
BoundaryAssertion {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
kind: CloneIn::clone_in_with_semantic_ids(&self.kind, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for BoundaryAssertionKind {
@@ -85,6 +152,15 @@ impl<'alloc> CloneIn<'alloc> for BoundaryAssertionKind {
Self::NegativeBoundary => BoundaryAssertionKind::NegativeBoundary,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Start => BoundaryAssertionKind::Start,
Self::End => BoundaryAssertionKind::End,
Self::Boundary => BoundaryAssertionKind::Boundary,
Self::NegativeBoundary => BoundaryAssertionKind::NegativeBoundary,
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for LookAroundAssertion<'_> {
@@ -96,6 +172,14 @@ impl<'new_alloc> CloneIn<'new_alloc> for LookAroundAssertion<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
LookAroundAssertion {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
kind: CloneIn::clone_in_with_semantic_ids(&self.kind, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for LookAroundAssertionKind {
@@ -108,6 +192,15 @@ impl<'alloc> CloneIn<'alloc> for LookAroundAssertionKind {
Self::NegativeLookbehind => LookAroundAssertionKind::NegativeLookbehind,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Lookahead => LookAroundAssertionKind::Lookahead,
Self::NegativeLookahead => LookAroundAssertionKind::NegativeLookahead,
Self::Lookbehind => LookAroundAssertionKind::Lookbehind,
Self::NegativeLookbehind => LookAroundAssertionKind::NegativeLookbehind,
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for Quantifier<'_> {
@@ -121,6 +214,16 @@ impl<'new_alloc> CloneIn<'new_alloc> for Quantifier<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
Quantifier {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
min: CloneIn::clone_in_with_semantic_ids(&self.min, allocator),
max: CloneIn::clone_in_with_semantic_ids(&self.max, allocator),
greedy: CloneIn::clone_in_with_semantic_ids(&self.greedy, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for Character {
@@ -132,6 +235,14 @@ impl<'alloc> CloneIn<'alloc> for Character {
value: CloneIn::clone_in(&self.value, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
Character {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
kind: CloneIn::clone_in_with_semantic_ids(&self.kind, allocator),
value: CloneIn::clone_in_with_semantic_ids(&self.value, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for CharacterKind {
@@ -150,6 +261,21 @@ impl<'alloc> CloneIn<'alloc> for CharacterKind {
Self::UnicodeEscape => CharacterKind::UnicodeEscape,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::ControlLetter => CharacterKind::ControlLetter,
Self::HexadecimalEscape => CharacterKind::HexadecimalEscape,
Self::Identifier => CharacterKind::Identifier,
Self::Null => CharacterKind::Null,
Self::Octal1 => CharacterKind::Octal1,
Self::Octal2 => CharacterKind::Octal2,
Self::Octal3 => CharacterKind::Octal3,
Self::SingleEscape => CharacterKind::SingleEscape,
Self::Symbol => CharacterKind::Symbol,
Self::UnicodeEscape => CharacterKind::UnicodeEscape,
}
}
}
impl<'alloc> CloneIn<'alloc> for CharacterClassEscape {
@@ -160,6 +286,13 @@ impl<'alloc> CloneIn<'alloc> for CharacterClassEscape {
kind: CloneIn::clone_in(&self.kind, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
CharacterClassEscape {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
kind: CloneIn::clone_in_with_semantic_ids(&self.kind, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for CharacterClassEscapeKind {
@@ -174,6 +307,17 @@ impl<'alloc> CloneIn<'alloc> for CharacterClassEscapeKind {
Self::NegativeW => CharacterClassEscapeKind::NegativeW,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::D => CharacterClassEscapeKind::D,
Self::NegativeD => CharacterClassEscapeKind::NegativeD,
Self::S => CharacterClassEscapeKind::S,
Self::NegativeS => CharacterClassEscapeKind::NegativeS,
Self::W => CharacterClassEscapeKind::W,
Self::NegativeW => CharacterClassEscapeKind::NegativeW,
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for UnicodePropertyEscape<'_> {
@@ -187,6 +331,16 @@ impl<'new_alloc> CloneIn<'new_alloc> for UnicodePropertyEscape<'_> {
value: CloneIn::clone_in(&self.value, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
UnicodePropertyEscape {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
negative: CloneIn::clone_in_with_semantic_ids(&self.negative, allocator),
strings: CloneIn::clone_in_with_semantic_ids(&self.strings, allocator),
name: CloneIn::clone_in_with_semantic_ids(&self.name, allocator),
value: CloneIn::clone_in_with_semantic_ids(&self.value, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for Dot {
@@ -194,6 +348,10 @@ impl<'alloc> CloneIn<'alloc> for Dot {
fn clone_in(&self, allocator: &'alloc Allocator) -> Self::Cloned {
Dot { span: CloneIn::clone_in(&self.span, allocator) }
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
Dot { span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator) }
}
}
impl<'new_alloc> CloneIn<'new_alloc> for CharacterClass<'_> {
@@ -207,6 +365,16 @@ impl<'new_alloc> CloneIn<'new_alloc> for CharacterClass<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
CharacterClass {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
negative: CloneIn::clone_in_with_semantic_ids(&self.negative, allocator),
strings: CloneIn::clone_in_with_semantic_ids(&self.strings, allocator),
kind: CloneIn::clone_in_with_semantic_ids(&self.kind, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for CharacterClassContentsKind {
@@ -218,6 +386,14 @@ impl<'alloc> CloneIn<'alloc> for CharacterClassContentsKind {
Self::Subtraction => CharacterClassContentsKind::Subtraction,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Union => CharacterClassContentsKind::Union,
Self::Intersection => CharacterClassContentsKind::Intersection,
Self::Subtraction => CharacterClassContentsKind::Subtraction,
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for CharacterClassContents<'_> {
@@ -244,6 +420,29 @@ impl<'new_alloc> CloneIn<'new_alloc> for CharacterClassContents<'_> {
}
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
match self {
Self::CharacterClassRange(it) => CharacterClassContents::CharacterClassRange(
CloneIn::clone_in_with_semantic_ids(it, allocator),
),
Self::CharacterClassEscape(it) => CharacterClassContents::CharacterClassEscape(
CloneIn::clone_in_with_semantic_ids(it, allocator),
),
Self::UnicodePropertyEscape(it) => CharacterClassContents::UnicodePropertyEscape(
CloneIn::clone_in_with_semantic_ids(it, allocator),
),
Self::Character(it) => CharacterClassContents::Character(
CloneIn::clone_in_with_semantic_ids(it, allocator),
),
Self::NestedCharacterClass(it) => CharacterClassContents::NestedCharacterClass(
CloneIn::clone_in_with_semantic_ids(it, allocator),
),
Self::ClassStringDisjunction(it) => CharacterClassContents::ClassStringDisjunction(
CloneIn::clone_in_with_semantic_ids(it, allocator),
),
}
}
}
impl<'alloc> CloneIn<'alloc> for CharacterClassRange {
@@ -255,6 +454,14 @@ impl<'alloc> CloneIn<'alloc> for CharacterClassRange {
max: CloneIn::clone_in(&self.max, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
CharacterClassRange {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
min: CloneIn::clone_in_with_semantic_ids(&self.min, allocator),
max: CloneIn::clone_in_with_semantic_ids(&self.max, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for ClassStringDisjunction<'_> {
@@ -266,6 +473,14 @@ impl<'new_alloc> CloneIn<'new_alloc> for ClassStringDisjunction<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
ClassStringDisjunction {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
strings: CloneIn::clone_in_with_semantic_ids(&self.strings, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for ClassString<'_> {
@@ -277,6 +492,14 @@ impl<'new_alloc> CloneIn<'new_alloc> for ClassString<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
ClassString {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
strings: CloneIn::clone_in_with_semantic_ids(&self.strings, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for CapturingGroup<'_> {
@@ -288,6 +511,14 @@ impl<'new_alloc> CloneIn<'new_alloc> for CapturingGroup<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
CapturingGroup {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
name: CloneIn::clone_in_with_semantic_ids(&self.name, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for IgnoreGroup<'_> {
@@ -299,6 +530,14 @@ impl<'new_alloc> CloneIn<'new_alloc> for IgnoreGroup<'_> {
body: CloneIn::clone_in(&self.body, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
IgnoreGroup {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
modifiers: CloneIn::clone_in_with_semantic_ids(&self.modifiers, allocator),
body: CloneIn::clone_in_with_semantic_ids(&self.body, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for Modifiers {
@@ -310,6 +549,14 @@ impl<'alloc> CloneIn<'alloc> for Modifiers {
disabling: CloneIn::clone_in(&self.disabling, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
Modifiers {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
enabling: CloneIn::clone_in_with_semantic_ids(&self.enabling, allocator),
disabling: CloneIn::clone_in_with_semantic_ids(&self.disabling, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for Modifier {
@@ -321,6 +568,14 @@ impl<'alloc> CloneIn<'alloc> for Modifier {
sticky: CloneIn::clone_in(&self.sticky, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
Modifier {
ignore_case: CloneIn::clone_in_with_semantic_ids(&self.ignore_case, allocator),
multiline: CloneIn::clone_in_with_semantic_ids(&self.multiline, allocator),
sticky: CloneIn::clone_in_with_semantic_ids(&self.sticky, allocator),
}
}
}
impl<'alloc> CloneIn<'alloc> for IndexedReference {
@@ -331,6 +586,13 @@ impl<'alloc> CloneIn<'alloc> for IndexedReference {
index: CloneIn::clone_in(&self.index, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'alloc Allocator) -> Self::Cloned {
IndexedReference {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
index: CloneIn::clone_in_with_semantic_ids(&self.index, allocator),
}
}
}
impl<'new_alloc> CloneIn<'new_alloc> for NamedReference<'_> {
@@ -341,4 +603,11 @@ impl<'new_alloc> CloneIn<'new_alloc> for NamedReference<'_> {
name: CloneIn::clone_in(&self.name, allocator),
}
}
fn clone_in_with_semantic_ids(&self, allocator: &'new_alloc Allocator) -> Self::Cloned {
NamedReference {
span: CloneIn::clone_in_with_semantic_ids(&self.span, allocator),
name: CloneIn::clone_in_with_semantic_ids(&self.name, allocator),
}
}
}
@@ -19,6 +19,16 @@ impl<'alloc> CloneIn<'alloc> for NumberBase {
Self::Hex => NumberBase::Hex,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Float => NumberBase::Float,
Self::Decimal => NumberBase::Decimal,
Self::Binary => NumberBase::Binary,
Self::Octal => NumberBase::Octal,
Self::Hex => NumberBase::Hex,
}
}
}
impl<'alloc> CloneIn<'alloc> for BigintBase {
@@ -31,6 +41,15 @@ impl<'alloc> CloneIn<'alloc> for BigintBase {
Self::Hex => BigintBase::Hex,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Decimal => BigintBase::Decimal,
Self::Binary => BigintBase::Binary,
Self::Octal => BigintBase::Octal,
Self::Hex => BigintBase::Hex,
}
}
}
impl<'alloc> CloneIn<'alloc> for AssignmentOperator {
@@ -55,6 +74,27 @@ impl<'alloc> CloneIn<'alloc> for AssignmentOperator {
Self::LogicalNullish => AssignmentOperator::LogicalNullish,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Assign => AssignmentOperator::Assign,
Self::Addition => AssignmentOperator::Addition,
Self::Subtraction => AssignmentOperator::Subtraction,
Self::Multiplication => AssignmentOperator::Multiplication,
Self::Division => AssignmentOperator::Division,
Self::Remainder => AssignmentOperator::Remainder,
Self::Exponential => AssignmentOperator::Exponential,
Self::ShiftLeft => AssignmentOperator::ShiftLeft,
Self::ShiftRight => AssignmentOperator::ShiftRight,
Self::ShiftRightZeroFill => AssignmentOperator::ShiftRightZeroFill,
Self::BitwiseOR => AssignmentOperator::BitwiseOR,
Self::BitwiseXOR => AssignmentOperator::BitwiseXOR,
Self::BitwiseAnd => AssignmentOperator::BitwiseAnd,
Self::LogicalOr => AssignmentOperator::LogicalOr,
Self::LogicalAnd => AssignmentOperator::LogicalAnd,
Self::LogicalNullish => AssignmentOperator::LogicalNullish,
}
}
}
impl<'alloc> CloneIn<'alloc> for BinaryOperator {
@@ -85,6 +125,33 @@ impl<'alloc> CloneIn<'alloc> for BinaryOperator {
Self::Instanceof => BinaryOperator::Instanceof,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Equality => BinaryOperator::Equality,
Self::Inequality => BinaryOperator::Inequality,
Self::StrictEquality => BinaryOperator::StrictEquality,
Self::StrictInequality => BinaryOperator::StrictInequality,
Self::LessThan => BinaryOperator::LessThan,
Self::LessEqualThan => BinaryOperator::LessEqualThan,
Self::GreaterThan => BinaryOperator::GreaterThan,
Self::GreaterEqualThan => BinaryOperator::GreaterEqualThan,
Self::Addition => BinaryOperator::Addition,
Self::Subtraction => BinaryOperator::Subtraction,
Self::Multiplication => BinaryOperator::Multiplication,
Self::Division => BinaryOperator::Division,
Self::Remainder => BinaryOperator::Remainder,
Self::Exponential => BinaryOperator::Exponential,
Self::ShiftLeft => BinaryOperator::ShiftLeft,
Self::ShiftRight => BinaryOperator::ShiftRight,
Self::ShiftRightZeroFill => BinaryOperator::ShiftRightZeroFill,
Self::BitwiseOR => BinaryOperator::BitwiseOR,
Self::BitwiseXOR => BinaryOperator::BitwiseXOR,
Self::BitwiseAnd => BinaryOperator::BitwiseAnd,
Self::In => BinaryOperator::In,
Self::Instanceof => BinaryOperator::Instanceof,
}
}
}
impl<'alloc> CloneIn<'alloc> for LogicalOperator {
@@ -96,6 +163,14 @@ impl<'alloc> CloneIn<'alloc> for LogicalOperator {
Self::Coalesce => LogicalOperator::Coalesce,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Or => LogicalOperator::Or,
Self::And => LogicalOperator::And,
Self::Coalesce => LogicalOperator::Coalesce,
}
}
}
impl<'alloc> CloneIn<'alloc> for UnaryOperator {
@@ -111,6 +186,18 @@ impl<'alloc> CloneIn<'alloc> for UnaryOperator {
Self::Delete => UnaryOperator::Delete,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::UnaryPlus => UnaryOperator::UnaryPlus,
Self::UnaryNegation => UnaryOperator::UnaryNegation,
Self::LogicalNot => UnaryOperator::LogicalNot,
Self::BitwiseNot => UnaryOperator::BitwiseNot,
Self::Typeof => UnaryOperator::Typeof,
Self::Void => UnaryOperator::Void,
Self::Delete => UnaryOperator::Delete,
}
}
}
impl<'alloc> CloneIn<'alloc> for UpdateOperator {
@@ -121,4 +208,11 @@ impl<'alloc> CloneIn<'alloc> for UpdateOperator {
Self::Decrement => UpdateOperator::Decrement,
}
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self::Cloned {
match self {
Self::Increment => UpdateOperator::Increment,
Self::Decrement => UpdateOperator::Decrement,
}
}
}
+14 -1
View File
@@ -5,7 +5,7 @@ use oxc_index::Idx;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};
use oxc_allocator::CloneIn;
use oxc_allocator::{Allocator, CloneIn};
use crate::{node::NodeId, symbol::SymbolId};
@@ -19,6 +19,19 @@ use oxc_ast_macros::ast;
#[estree(skip)]
pub struct ReferenceId(NonMaxU32);
impl<'alloc> CloneIn<'alloc> for ReferenceId {
type Cloned = Self;
fn clone_in(&self, _: &'alloc Allocator) -> Self {
// `clone_in` should never reach this, because `CloneIn` skips reference_id field
unreachable!();
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self {
*self
}
}
impl Idx for ReferenceId {
#[expect(clippy::cast_possible_truncation)]
fn from_usize(idx: usize) -> Self {
+14
View File
@@ -1,6 +1,7 @@
#![expect(missing_docs)] // fixme
use bitflags::bitflags;
use nonmax::NonMaxU32;
use oxc_allocator::{Allocator, CloneIn};
use oxc_index::Idx;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};
@@ -15,6 +16,19 @@ use oxc_ast_macros::ast;
#[estree(skip)]
pub struct ScopeId(NonMaxU32);
impl<'alloc> CloneIn<'alloc> for ScopeId {
type Cloned = Self;
fn clone_in(&self, _: &'alloc Allocator) -> Self {
// `clone_in` should never reach this, because `CloneIn` skips scope_id field
unreachable!();
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self {
*self
}
}
impl ScopeId {
/// Create `ScopeId` from `u32`.
///
+14
View File
@@ -1,6 +1,7 @@
#![expect(missing_docs)] // fixme
use bitflags::bitflags;
use nonmax::NonMaxU32;
use oxc_allocator::{Allocator, CloneIn};
use oxc_index::Idx;
#[cfg(feature = "serialize")]
use serde::{Serialize, Serializer};
@@ -15,6 +16,19 @@ use oxc_ast_macros::ast;
#[estree(skip)]
pub struct SymbolId(NonMaxU32);
impl<'alloc> CloneIn<'alloc> for SymbolId {
type Cloned = Self;
fn clone_in(&self, _: &'alloc Allocator) -> Self {
// `clone_in` should never reach this, because `CloneIn` skips symbol_id field
unreachable!();
}
fn clone_in_with_semantic_ids(&self, _: &'alloc Allocator) -> Self {
*self
}
}
impl SymbolId {
/// Create `SymbolId` from `u32`.
///
+54 -6
View File
@@ -97,7 +97,23 @@ fn derive_struct(struct_def: &StructDef, schema: &Schema) -> TokenStream {
quote!( #type_ident )
};
generate_impl(&type_ident, &body, struct_def.has_lifetime, has_fields)
let clone_in_with_semantic_ids_body = if has_fields {
let fields = struct_def.fields.iter().map(|field| {
let field_ident = field.ident();
quote!( #field_ident: CloneIn::clone_in_with_semantic_ids(&self.#field_ident, allocator) )
});
quote!( #type_ident { #(#fields),* } )
} else {
quote!( #type_ident )
};
generate_impl(
&type_ident,
&body,
&clone_in_with_semantic_ids_body,
struct_def.has_lifetime,
has_fields,
)
}
/// Get if a struct field should be filled with default value when cloning.
@@ -137,18 +153,41 @@ fn derive_enum(enum_def: &EnumDef, schema: &Schema) -> TokenStream {
}
});
let body = quote! {
let clone_in_with_semantic_ids_match_arms = enum_def.all_variants(schema).map(|variant| {
let ident = variant.ident();
if variant.is_fieldless() {
quote!( Self::#ident => #type_ident::#ident )
} else {
// does not needs mutate `uses_allocator` since it should be the same as `clone_in`
quote!( Self::#ident(it) => #type_ident::#ident(CloneIn::clone_in_with_semantic_ids(it, allocator)) )
}
});
let clone_in_body = quote! {
match self {
#(#match_arms),*
}
};
generate_impl(&type_ident, &body, enum_def.has_lifetime, uses_allocator)
let clone_in_with_semantic_ids_body = quote! {
match self {
#(#clone_in_with_semantic_ids_match_arms),*
}
};
generate_impl(
&type_ident,
&clone_in_body,
&clone_in_with_semantic_ids_body,
enum_def.has_lifetime,
uses_allocator,
)
}
fn generate_impl(
type_ident: &Ident,
body: &TokenStream,
clone_in_body: &TokenStream,
clone_in_with_semantic_ids_body: &TokenStream,
has_lifetime: bool,
uses_allocator: bool,
) -> TokenStream {
@@ -159,7 +198,11 @@ fn generate_impl(
impl<'new_alloc> CloneIn<'new_alloc> for #type_ident<'_> {
type Cloned = #type_ident<'new_alloc>;
fn clone_in(&self, #alloc_ident: &'new_alloc Allocator) -> Self::Cloned {
#body
#clone_in_body
}
///@@line_break
fn clone_in_with_semantic_ids(&self, #alloc_ident: &'new_alloc Allocator) -> Self::Cloned {
#clone_in_with_semantic_ids_body
}
}
}
@@ -168,7 +211,12 @@ fn generate_impl(
impl<'alloc> CloneIn<'alloc> for #type_ident {
type Cloned = #type_ident;
fn clone_in(&self, #alloc_ident: &'alloc Allocator) -> Self::Cloned {
#body
#clone_in_body
}
///@@line_break
fn clone_in_with_semantic_ids(&self, #alloc_ident: &'alloc Allocator) -> Self::Cloned {
#clone_in_with_semantic_ids_body
}
}
}