boa_ast/expression/
await.rs

1//! Await expression Expression.
2
3use core::ops::ControlFlow;
4
5use super::Expression;
6use crate::{
7    Span, Spanned,
8    visitor::{VisitWith, Visitor, VisitorMut},
9};
10use boa_interner::{Interner, ToIndentedString, ToInternedString};
11
12/// An await expression is used within an async function to pause execution and wait for a
13/// promise to resolve.
14///
15/// More information:
16///  - [ECMAScript reference][spec]
17///  - [MDN documentation][mdn]
18///
19/// [spec]: https://tc39.es/ecma262/#prod-AwaitExpression
20/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await
21#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
22#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
23#[derive(Clone, Debug, PartialEq)]
24pub struct Await {
25    target: Box<Expression>,
26    span: Span,
27}
28
29impl Await {
30    /// Create a new [`Await`] node.
31    #[must_use]
32    pub const fn new(target: Box<Expression>, span: Span) -> Self {
33        Self { target, span }
34    }
35
36    /// Return the target expression that should be awaited.
37    #[inline]
38    #[must_use]
39    pub const fn target(&self) -> &Expression {
40        &self.target
41    }
42}
43
44impl Spanned for Await {
45    #[inline]
46    fn span(&self) -> Span {
47        self.span
48    }
49}
50
51impl ToInternedString for Await {
52    #[inline]
53    fn to_interned_string(&self, interner: &Interner) -> String {
54        format!("await {}", self.target.to_indented_string(interner, 0))
55    }
56}
57
58impl From<Await> for Expression {
59    #[inline]
60    fn from(awaitexpr: Await) -> Self {
61        Self::Await(awaitexpr)
62    }
63}
64
65impl VisitWith for Await {
66    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
67    where
68        V: Visitor<'a>,
69    {
70        visitor.visit_expression(&self.target)
71    }
72
73    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
74    where
75        V: VisitorMut<'a>,
76    {
77        visitor.visit_expression_mut(&mut self.target)
78    }
79}