-
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathconditional.rs
More file actions
113 lines (103 loc) · 3.22 KB
/
conditional.rs
File metadata and controls
113 lines (103 loc) · 3.22 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use crate::{
expression::Expression,
visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, ToInternedString};
use core::ops::ControlFlow;
/// The `conditional` (ternary) operation is the only ECMAScript operation that takes three
/// operands.
///
/// This operation 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 = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct Conditional {
condition: Box<Expression>,
if_true: Box<Expression>,
if_false: Box<Expression>,
}
impl Conditional {
/// Gets the condition of the `Conditional` expression.
#[inline]
#[must_use]
pub const fn condition(&self) -> &Expression {
&self.condition
}
/// Gets the expression returned if `condition` is truthy.
#[inline]
#[must_use]
pub const fn if_true(&self) -> &Expression {
&self.if_true
}
/// Gets the expression returned if `condition` is falsy.
#[inline]
#[must_use]
pub const fn if_false(&self) -> &Expression {
&self.if_false
}
/// Creates a `Conditional` AST Expression.
#[inline]
#[must_use]
pub fn new(
condition: Box<Expression>,
if_true: Box<Expression>,
if_false: Box<Expression>,
) -> Self {
Self {
condition,
if_true,
if_false,
}
}
}
impl ToInternedString for Conditional {
#[inline]
fn to_interned_string(&self, interner: &Interner) -> String {
format!(
"{} ? {} : {}",
self.condition().to_interned_string(interner),
self.if_true().to_interned_string(interner),
self.if_false().to_interned_string(interner)
)
}
}
impl From<Conditional> for Expression {
#[inline]
fn from(cond_op: Conditional) -> Self {
Self::Conditional(cond_op)
}
}
impl From<Conditional> for Box<Expression> {
#[inline]
fn from(cond_op: Conditional) -> Self {
Box::new(Expression::Conditional(cond_op))
}
}
impl VisitWith for Conditional {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
visitor.visit_expression(&self.condition)?;
visitor.visit_expression(&self.if_true)?;
visitor.visit_expression(&self.if_false)
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
visitor.visit_expression_mut(&mut self.condition)?;
visitor.visit_expression_mut(&mut self.if_true)?;
visitor.visit_expression_mut(&mut self.if_false)
}
}