-
-
Notifications
You must be signed in to change notification settings - Fork 613
Expand file tree
/
Copy pathoptional.rs
More file actions
252 lines (230 loc) · 7.76 KB
/
optional.rs
File metadata and controls
252 lines (230 loc) · 7.76 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
use super::{access::PropertyAccessField, Expression};
use crate::{
function::PrivateName,
join_nodes,
visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, ToInternedString};
use core::ops::ControlFlow;
/// List of valid operations in an [`Optional`] chain.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub enum OptionalOperationKind {
/// A property access (`a?.prop`).
SimplePropertyAccess {
/// The field accessed.
field: PropertyAccessField,
},
/// A private property access (`a?.#prop`).
PrivatePropertyAccess {
/// The private property accessed.
field: PrivateName,
},
/// A function call (`a?.(arg)`).
Call {
/// The args passed to the function call.
args: Box<[Expression]>,
},
}
impl VisitWith for OptionalOperationKind {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
match self {
Self::SimplePropertyAccess { field } => visitor.visit_property_access_field(field),
Self::PrivatePropertyAccess { field } => visitor.visit_private_name(field),
Self::Call { args } => {
for arg in args {
visitor.visit_expression(arg)?;
}
ControlFlow::Continue(())
}
}
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
match self {
Self::SimplePropertyAccess { field } => visitor.visit_property_access_field_mut(field),
Self::PrivatePropertyAccess { field } => visitor.visit_private_name_mut(field),
Self::Call { args } => {
for arg in args.iter_mut() {
visitor.visit_expression_mut(arg)?;
}
ControlFlow::Continue(())
}
}
}
}
/// Operation within an [`Optional`] chain.
///
/// An operation within an `Optional` chain can be either shorted or non-shorted. A shorted operation
/// (`?.item`) will force the expression to return `undefined` if the target is `undefined` or `null`.
/// In contrast, a non-shorted operation (`.prop`) will try to access the property, even if the target
/// is `undefined` or `null`.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct OptionalOperation {
kind: OptionalOperationKind,
shorted: bool,
}
impl OptionalOperation {
/// Creates a new `OptionalOperation`.
#[inline]
#[must_use]
pub const fn new(kind: OptionalOperationKind, shorted: bool) -> Self {
Self { kind, shorted }
}
/// Gets the kind of operation.
#[inline]
#[must_use]
pub const fn kind(&self) -> &OptionalOperationKind {
&self.kind
}
/// Returns `true` if the operation short-circuits the [`Optional`] chain when the target is
/// `undefined` or `null`.
#[inline]
#[must_use]
pub const fn shorted(&self) -> bool {
self.shorted
}
}
impl ToInternedString for OptionalOperation {
fn to_interned_string(&self, interner: &Interner) -> String {
let mut buf = if self.shorted {
String::from("?.")
} else {
if let OptionalOperationKind::SimplePropertyAccess {
field: PropertyAccessField::Const(name),
} = &self.kind
{
return format!(".{}", interner.resolve_expect(*name));
}
if let OptionalOperationKind::PrivatePropertyAccess { field } = &self.kind {
return format!(".#{}", interner.resolve_expect(field.description()));
}
String::new()
};
buf.push_str(&match &self.kind {
OptionalOperationKind::SimplePropertyAccess { field } => match field {
PropertyAccessField::Const(name) => interner.resolve_expect(*name).to_string(),
PropertyAccessField::Expr(expr) => {
format!("[{}]", expr.to_interned_string(interner))
}
},
OptionalOperationKind::PrivatePropertyAccess { field } => {
format!("#{}", interner.resolve_expect(field.description()))
}
OptionalOperationKind::Call { args } => format!("({})", join_nodes(interner, args)),
});
buf
}
}
impl VisitWith for OptionalOperation {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
visitor.visit_optional_operation_kind(&self.kind)
}
fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: VisitorMut<'a>,
{
visitor.visit_optional_operation_kind_mut(&mut self.kind)
}
}
/// An optional chain expression, as defined by the [spec].
///
/// [Optional chaining][mdn] allows for short-circuiting property accesses and function calls, which
/// will return `undefined` instead of returning an error if the access target or the call is
/// either `undefined` or `null`.
///
/// An example of optional chaining:
///
/// ```Javascript
/// const adventurer = {
/// name: 'Alice',
/// cat: {
/// name: 'Dinah'
/// }
/// };
///
/// console.log(adventurer.cat?.name); // Dinah
/// console.log(adventurer.dog?.name); // undefined
/// ```
///
/// [spec]: https://tc39.es/ecma262/multipage/ecmascript-language-expressions.html#prod-OptionalExpression
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct Optional {
target: Box<Expression>,
chain: Box<[OptionalOperation]>,
}
impl VisitWith for Optional {
fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
where
V: Visitor<'a>,
{
visitor.visit_expression(&self.target)?;
for op in &*self.chain {
visitor.visit_optional_operation(op)?;
}
ControlFlow::Continue(())
}
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.target)?;
for op in &mut *self.chain {
visitor.visit_optional_operation_mut(op)?;
}
ControlFlow::Continue(())
}
}
impl Optional {
/// Creates a new `Optional` expression.
#[inline]
#[must_use]
pub fn new(target: Box<Expression>, chain: Box<[OptionalOperation]>) -> Self {
Self { target, chain }
}
/// Gets the target of this `Optional` expression.
#[inline]
#[must_use]
pub fn target(&self) -> &Expression {
self.target.as_ref()
}
/// Gets the chain of accesses and calls that will be applied to the target at runtime.
#[inline]
#[must_use]
pub fn chain(&self) -> &[OptionalOperation] {
self.chain.as_ref()
}
}
impl From<Optional> for Expression {
fn from(opt: Optional) -> Self {
Self::Optional(opt)
}
}
impl From<Optional> for Box<Expression> {
fn from(opt: Optional) -> Self {
Box::new(Expression::Optional(opt))
}
}
impl ToInternedString for Optional {
fn to_interned_string(&self, interner: &Interner) -> String {
let mut buf = self.target.to_interned_string(interner);
for item in &*self.chain {
buf.push_str(&item.to_interned_string(interner));
}
buf
}
}