Skip to content

Commit

Permalink
feat(traverse): add methods to BoundIdentifier + `MaybeBoundIdentif…
Browse files Browse the repository at this point in the history
…ier` to create `SimpleAssignmentTarget`s
  • Loading branch information
overlookmotel committed Nov 22, 2024
1 parent 88966eb commit 1a6116b
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 2 deletions.
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))
}
}

0 comments on commit 1a6116b

Please sign in to comment.