use crate::syntax::ast::{node::Node, op};
use boa_interner::{Interner, ToInternedString};
#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct UnaryOp {
op: op::UnaryOp,
target: Box<Node>,
}
impl UnaryOp {
pub(in crate::syntax) fn new<V>(op: op::UnaryOp, target: V) -> Self
where
V: Into<Node>,
{
Self {
op,
target: Box::new(target.into()),
}
}
pub fn op(&self) -> op::UnaryOp {
self.op
}
pub fn target(&self) -> &Node {
self.target.as_ref()
}
}
impl ToInternedString for UnaryOp {
fn to_interned_string(&self, interner: &Interner) -> String {
format!("{}{}", self.op, self.target.to_interned_string(interner))
}
}
impl From<UnaryOp> for Node {
fn from(op: UnaryOp) -> Self {
Self::UnaryOp(op)
}
}