Skip to main content

boa_ast/statement/iteration/
continue.rs

1use crate::statement::Statement;
2use crate::visitor::{VisitWith, Visitor, VisitorMut};
3use boa_interner::{Interner, Sym, ToInternedString};
4use core::ops::ControlFlow;
5
6/// The `continue` statement terminates execution of the statements in the current iteration of
7/// the current or labeled loop, and continues execution of the loop with the next iteration.
8///
9/// The continue statement can include an optional label that allows the program to jump to the
10/// next iteration of a labeled loop statement instead of the current loop. In this case, the
11/// continue statement needs to be nested within this labeled statement.
12///
13/// More information:
14///  - [ECMAScript reference][spec]
15///  - [MDN documentation][mdn]
16///
17/// [spec]: https://tc39.es/ecma262/#prod-ContinueStatement
18/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/continue
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
21#[derive(Clone, Copy, Debug, PartialEq, Eq)]
22pub struct Continue {
23    label: Option<Sym>,
24}
25
26impl Continue {
27    /// Creates a `Continue` AST node.
28    #[must_use]
29    pub const fn new(label: Option<Sym>) -> Self {
30        Self { label }
31    }
32
33    /// Gets the label of this `Continue` statement.
34    #[must_use]
35    pub const fn label(&self) -> Option<Sym> {
36        self.label
37    }
38}
39
40impl ToInternedString for Continue {
41    fn to_interned_string(&self, interner: &Interner) -> String {
42        self.label.map_or_else(
43            || "continue".to_owned(),
44            |label| format!("continue {}", interner.resolve_expect(label)),
45        )
46    }
47}
48
49impl From<Continue> for Statement {
50    fn from(cont: Continue) -> Self {
51        Self::Continue(cont)
52    }
53}
54
55impl VisitWith for Continue {
56    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
57    where
58        V: Visitor<'a>,
59    {
60        if let Some(sym) = &self.label {
61            visitor.visit_sym(sym)
62        } else {
63            ControlFlow::Continue(())
64        }
65    }
66
67    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
68    where
69        V: VisitorMut<'a>,
70    {
71        if let Some(sym) = &mut self.label {
72            visitor.visit_sym_mut(sym)
73        } else {
74            ControlFlow::Continue(())
75        }
76    }
77}