Skip to main content

boa_ast/expression/operator/unary/
mod.rs

1//! Unary expression nodes.
2//!
3//! A unary expression comprises any operation applied to a single expression. Some examples include:
4//!
5//! - The [`delete`][del] operator.
6//! - The [bitwise NOT][not] operator (`~`).
7//!
8//! The full list of valid unary operators is defined in [`UnaryOp`].
9//!
10//! [del]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/delete
11//! [not]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_NOT
12mod 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/// A unary expression is an operation with only one operand.
25///
26/// More information:
27///  - [ECMAScript reference][spec]
28///  - [MDN documentation][mdn]
29///
30/// [spec]: https://tc39.es/ecma262/#prod-UnaryExpression
31/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Expressions_and_Operators#Unary_operators
32#[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    /// Creates a new `UnaryOp` AST Expression.
43    #[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    /// Gets the unary operation of the Expression.
54    #[inline]
55    #[must_use]
56    pub const fn op(&self) -> UnaryOp {
57        self.op
58    }
59
60    /// Gets the target of this unary operator.
61    #[inline]
62    #[must_use]
63    pub fn target(&self) -> &Expression {
64        self.target.as_ref()
65    }
66
67    /// Gets the target of this unary operator.
68    #[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}