mod op;
use crate::{
Expression, Span, Spanned,
expression::{Identifier, access::PropertyAccess},
visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, ToInternedString};
use core::ops::ControlFlow;
pub use op::*;
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct Update {
op: UpdateOp,
target: Box<UpdateTarget>,
span: Span,
}
impl Update {
#[inline]
#[must_use]
pub fn new(op: UpdateOp, target: UpdateTarget, span: Span) -> Self {
Self {
op,
target: Box::new(target),
span,
}
}
#[inline]
#[must_use]
pub const fn op(&self) -> UpdateOp {
self.op
}
#[inline]
#[must_use]
pub fn target(&self) -> &UpdateTarget {
self.target.as_ref()
}
}
impl Spanned for Update {
#[inline]
fn span(&self) -> Span {
self.span
}
}
impl ToInternedString for Update {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
match self.op {
UpdateOp::IncrementPost | UpdateOp::DecrementPost => {
format!("{}{}", self.target.to_interned_string(interner), self.op)
}
UpdateOp::IncrementPre | UpdateOp::DecrementPre => {
format!("{}{}", self.op, self.target.to_interned_string(interner))
}
}
}
}
impl From<Update> for Expression {
#[inline]
fn from(op: Update) -> Self {
Self::Update(op)
}
}
impl VisitWith for Update {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
match self.target.as_ref() {
UpdateTarget::Identifier(ident) => visitor.visit_identifier(ident),
UpdateTarget::PropertyAccess(access) => visitor.visit_property_access(access),
}
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
match &mut *self.target {
UpdateTarget::Identifier(ident) => visitor.visit_identifier_mut(ident),
UpdateTarget::PropertyAccess(access) => visitor.visit_property_access_mut(access),
}
}
}
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub enum UpdateTarget {
Identifier(Identifier),
PropertyAccess(PropertyAccess),
}
impl ToInternedString for UpdateTarget {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
match self {
Self::Identifier(identifier) => identifier.to_interned_string(interner),
Self::PropertyAccess(access) => access.to_interned_string(interner),
}
}
}