boa_ast/expression/operator/unary/
mod.rs1mod op;
13
14use crate::{
15 Span, Spanned,
16 expression::Expression,
17 visitor::{VisitWith, Visitor, VisitorMut},
18};
19use boa_interner::{Interner, ToInternedString};
20use core::ops::ControlFlow;
21
22pub use op::*;
23
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
33#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
34#[derive(Clone, Debug, PartialEq)]
35pub struct Unary {
36 op: UnaryOp,
37 target: Box<Expression>,
38 span: Span,
39}
40
41impl Unary {
42 #[inline]
44 #[must_use]
45 pub fn new(op: UnaryOp, target: Expression, span: Span) -> Self {
46 Self {
47 op,
48 target: Box::new(target),
49 span,
50 }
51 }
52
53 #[inline]
55 #[must_use]
56 pub const fn op(&self) -> UnaryOp {
57 self.op
58 }
59
60 #[inline]
62 #[must_use]
63 pub fn target(&self) -> &Expression {
64 self.target.as_ref()
65 }
66
67 #[inline]
69 #[must_use]
70 pub fn target_mut(&mut self) -> &mut Expression {
71 self.target.as_mut()
72 }
73}
74
75impl Spanned for Unary {
76 #[inline]
77 fn span(&self) -> Span {
78 self.span
79 }
80}
81
82impl ToInternedString for Unary {
83 #[inline]
84 fn to_interned_string(&self, interner: &Interner) -> String {
85 format!("{} {}", self.op, self.target.to_interned_string(interner))
86 }
87}
88
89impl From<Unary> for Expression {
90 #[inline]
91 fn from(op: Unary) -> Self {
92 Self::Unary(op)
93 }
94}
95
96impl VisitWith for Unary {
97 fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
98 where
99 V: Visitor<'a>,
100 {
101 visitor.visit_expression(&self.target)
102 }
103
104 fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
105 where
106 V: VisitorMut<'a>,
107 {
108 visitor.visit_expression_mut(&mut self.target)
109 }
110}