Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(traverse): add methods to BoundIdentifier + MaybeBoundIdentifier to create SimpleAssignmentTargets #7418

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
59 changes: 58 additions & 1 deletion crates/oxc_traverse/src/context/bound_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use oxc_ast::{
ast::{
AssignmentTarget, BindingIdentifier, BindingPattern, BindingPatternKind, Expression,
IdentifierReference,
IdentifierReference, SimpleAssignmentTarget,
},
NONE,
};
Expand Down Expand Up @@ -120,6 +120,14 @@ impl<'a> BoundIdentifier<'a> {
self.create_spanned_write_target(SPAN, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is written to, with dummy `Span`
pub fn create_write_simple_target(
&self,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_write_simple_target(SPAN, ctx)
}

/// Create `IdentifierReference` referencing this binding, which is written to, with specified `Span`
pub fn create_spanned_write_reference(
&self,
Expand Down Expand Up @@ -147,6 +155,15 @@ impl<'a> BoundIdentifier<'a> {
self.create_spanned_target(span, ReferenceFlags::Write, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is written to, with specified `Span`
pub fn create_spanned_write_simple_target(
&self,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_simple_target(span, ReferenceFlags::Write, ctx)
}

// --- Read and write ---

/// Create `IdentifierReference` referencing this binding, which is read from + written to,
Expand All @@ -170,6 +187,15 @@ impl<'a> BoundIdentifier<'a> {
self.create_spanned_read_write_target(SPAN, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is read from + written to,
/// with dummy `Span`
pub fn create_read_write_simple_target(
&self,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_read_write_simple_target(SPAN, ctx)
}

/// Create `IdentifierReference` referencing this binding, which is read from + written to,
/// with specified `Span`
pub fn create_spanned_read_write_reference(
Expand Down Expand Up @@ -200,6 +226,16 @@ impl<'a> BoundIdentifier<'a> {
self.create_spanned_target(span, ReferenceFlags::Read | ReferenceFlags::Write, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is read from + written to,
/// with specified `Span`
pub fn create_spanned_read_write_simple_target(
&self,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_simple_target(span, ReferenceFlags::Read | ReferenceFlags::Write, ctx)
}

// --- Specified ReferenceFlags ---

/// Create `IdentifierReference` referencing this binding, with specified `ReferenceFlags`
Expand Down Expand Up @@ -229,6 +265,15 @@ impl<'a> BoundIdentifier<'a> {
self.create_spanned_target(SPAN, flags, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, with specified `ReferenceFlags`
pub fn create_simple_target(
&self,
flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_simple_target(SPAN, flags, ctx)
}

/// Create `IdentifierReference` referencing this binding, with specified `Span` and `ReferenceFlags`
pub fn create_spanned_reference(
&self,
Expand Down Expand Up @@ -261,4 +306,16 @@ impl<'a> BoundIdentifier<'a> {
let ident = self.create_spanned_reference(span, flags, ctx);
AssignmentTarget::AssignmentTargetIdentifier(ctx.alloc(ident))
}

/// Create `SimpleAssignmentTarget::AssignmentTargetIdentifier` referencing this binding,
/// with specified `Span` and `ReferenceFlags`
pub fn create_spanned_simple_target(
&self,
span: Span,
flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
let ident = self.create_spanned_reference(span, flags, ctx);
SimpleAssignmentTarget::AssignmentTargetIdentifier(ctx.alloc(ident))
}
}
59 changes: 58 additions & 1 deletion crates/oxc_traverse/src/context/maybe_bound_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use oxc_ast::ast::{AssignmentTarget, Expression, IdentifierReference};
use oxc_ast::ast::{AssignmentTarget, Expression, IdentifierReference, SimpleAssignmentTarget};
use oxc_span::{Atom, Span, SPAN};
use oxc_syntax::{reference::ReferenceFlags, symbol::SymbolId};

Expand Down Expand Up @@ -102,6 +102,14 @@ impl<'a> MaybeBoundIdentifier<'a> {
self.create_spanned_write_target(SPAN, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is written to, with dummy `Span`
pub fn create_write_simple_target(
&self,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_write_simple_target(SPAN, ctx)
}

/// Create `IdentifierReference` referencing this binding, which is written to, with specified `Span`
pub fn create_spanned_write_reference(
&self,
Expand Down Expand Up @@ -129,6 +137,15 @@ impl<'a> MaybeBoundIdentifier<'a> {
self.create_spanned_target(span, ReferenceFlags::Write, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is written to, with specified `Span`
pub fn create_spanned_write_simple_target(
&self,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_simple_target(span, ReferenceFlags::Write, ctx)
}

// --- Read and write ---

/// Create `IdentifierReference` referencing this binding, which is read from + written to,
Expand All @@ -152,6 +169,15 @@ impl<'a> MaybeBoundIdentifier<'a> {
self.create_spanned_read_write_target(SPAN, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is read from + written to,
/// with dummy `Span`
pub fn create_read_write_simple_target(
&self,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_read_write_simple_target(SPAN, ctx)
}

/// Create `IdentifierReference` referencing this binding, which is read from + written to,
/// with specified `Span`
pub fn create_spanned_read_write_reference(
Expand Down Expand Up @@ -182,6 +208,16 @@ impl<'a> MaybeBoundIdentifier<'a> {
self.create_spanned_target(span, ReferenceFlags::Read | ReferenceFlags::Write, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, which is read from + written to,
/// with specified `Span`
pub fn create_spanned_read_write_simple_target(
&self,
span: Span,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_simple_target(span, ReferenceFlags::Read | ReferenceFlags::Write, ctx)
}

// --- Specified ReferenceFlags ---

/// Create `IdentifierReference` referencing this binding, with specified `ReferenceFlags`
Expand Down Expand Up @@ -211,6 +247,15 @@ impl<'a> MaybeBoundIdentifier<'a> {
self.create_spanned_target(SPAN, flags, ctx)
}

/// Create `SimpleAssignmentTarget` referencing this binding, with specified `ReferenceFlags`
pub fn create_simple_target(
&self,
flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
self.create_spanned_simple_target(SPAN, flags, ctx)
}

/// Create `IdentifierReference` referencing this binding, with specified `Span` and `ReferenceFlags`
pub fn create_spanned_reference(
&self,
Expand Down Expand Up @@ -243,4 +288,16 @@ impl<'a> MaybeBoundIdentifier<'a> {
let ident = self.create_spanned_reference(span, flags, ctx);
AssignmentTarget::AssignmentTargetIdentifier(ctx.alloc(ident))
}

/// Create `SimpleAssignmentTarget::AssignmentTargetIdentifier` referencing this binding,
/// with specified `Span` and `ReferenceFlags`
pub fn create_spanned_simple_target(
&self,
span: Span,
flags: ReferenceFlags,
ctx: &mut TraverseCtx<'a>,
) -> SimpleAssignmentTarget<'a> {
let ident = self.create_spanned_reference(span, flags, ctx);
SimpleAssignmentTarget::AssignmentTargetIdentifier(ctx.alloc(ident))
}
}
Loading