logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::syntax::ast::node::Node;
use boa_interner::{Interner, ToInternedString};

#[cfg(feature = "deser")]
use serde::{Deserialize, Serialize};

/// The `conditional` (ternary) operator is the only JavaScript operator that takes three
/// operands.
///
/// This operator is the only JavaScript operator that takes three operands: a condition
/// followed by a question mark (`?`), then an expression to execute `if` the condition is
/// truthy followed by a colon (`:`), and finally the expression to execute if the condition
/// is `false`. This operator is frequently used as a shortcut for the `if` statement.
///
/// More information:
///  - [ECMAScript reference][spec]
///  - [MDN documentation][mdn]
///
/// [spec]: https://tc39.es/ecma262/#prod-ConditionalExpression
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_types#Literals
#[cfg_attr(feature = "deser", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ConditionalOp {
    condition: Box<Node>,
    if_true: Box<Node>,
    if_false: Box<Node>,
}

impl ConditionalOp {
    pub fn cond(&self) -> &Node {
        &self.condition
    }

    pub fn if_true(&self) -> &Node {
        &self.if_true
    }

    pub fn if_false(&self) -> &Node {
        &self.if_false
    }

    /// Creates a `ConditionalOp` AST node.
    pub fn new<C, T, F>(condition: C, if_true: T, if_false: F) -> Self
    where
        C: Into<Node>,
        T: Into<Node>,
        F: Into<Node>,
    {
        Self {
            condition: Box::new(condition.into()),
            if_true: Box::new(if_true.into()),
            if_false: Box::new(if_false.into()),
        }
    }
}

impl ToInternedString for ConditionalOp {
    fn to_interned_string(&self, interner: &Interner) -> String {
        format!(
            "{} ? {} : {}",
            self.cond().to_interned_string(interner),
            self.if_true().to_interned_string(interner),
            self.if_false().to_interned_string(interner)
        )
    }
}

impl From<ConditionalOp> for Node {
    fn from(cond_op: ConditionalOp) -> Self {
        Self::ConditionalOp(cond_op)
    }
}