boa_ast 0.21.1

Abstract Syntax Tree definition for the Boa JavaScript engine.
Documentation
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
//! A pattern binding or assignment node.
//!
//! A [`Pattern`] Corresponds to the [`BindingPattern`][spec1] and the [`AssignmentPattern`][spec2]
//! nodes, each of which is used in different situations and have slightly different grammars.
//! For example, a variable declaration combined with a destructuring expression is a `BindingPattern`:
//!
//! ```Javascript
//! const obj = { a: 1, b: 2 };
//! const { a, b } = obj; // BindingPattern
//! ```
//!
//! On the other hand, a simple destructuring expression with already declared variables is called
//! an `AssignmentPattern`:
//!
//! ```Javascript
//! let a = 1;
//! let b = 3;
//! [a, b] = [b, a]; // AssignmentPattern
//! ```
//!
//! [spec1]: https://tc39.es/ecma262/#prod-BindingPattern
//! [spec2]: https://tc39.es/ecma262/#prod-AssignmentPattern
//! [destr]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

use crate::{
    Expression, Span, Spanned,
    expression::{Identifier, access::PropertyAccess},
    property::PropertyName,
    visitor::{VisitWith, Visitor, VisitorMut},
};
use boa_interner::{Interner, ToInternedString};
use core::{fmt::Write as _, ops::ControlFlow};

/// An object or array pattern binding or assignment.
///
/// See the [module level documentation][self] for more information.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub enum Pattern {
    /// An object pattern (`let {a, b, c} = object`).
    Object(ObjectPattern),
    /// An array pattern (`[a, b, c] = array`).
    Array(ArrayPattern),
}

impl Spanned for Pattern {
    #[inline]
    fn span(&self) -> Span {
        match self {
            Pattern::Object(object_pattern) => object_pattern.span(),
            Pattern::Array(array_pattern) => array_pattern.span(),
        }
    }
}

impl From<ObjectPattern> for Pattern {
    fn from(obj: ObjectPattern) -> Self {
        Self::Object(obj)
    }
}

impl From<ArrayPattern> for Pattern {
    fn from(obj: ArrayPattern) -> Self {
        Self::Array(obj)
    }
}

impl ToInternedString for Pattern {
    fn to_interned_string(&self, interner: &Interner) -> String {
        match &self {
            Self::Object(o) => o.to_interned_string(interner),
            Self::Array(a) => a.to_interned_string(interner),
        }
    }
}

impl VisitWith for Pattern {
    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: Visitor<'a>,
    {
        match self {
            Self::Object(op) => visitor.visit_object_pattern(op),
            Self::Array(ap) => visitor.visit_array_pattern(ap),
        }
    }

    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: VisitorMut<'a>,
    {
        match self {
            Self::Object(op) => visitor.visit_object_pattern_mut(op),
            Self::Array(ap) => visitor.visit_array_pattern_mut(ap),
        }
    }
}

/// An object binding or assignment pattern.
///
/// Corresponds to the [`ObjectBindingPattern`][spec1] and the [`ObjectAssignmentPattern`][spec2]
/// Parse Nodes.
///
/// For more information on what is a valid binding in an object pattern, see [`ObjectPatternElement`].
///
/// [spec1]: https://tc39.es/ecma262/#prod-ObjectBindingPattern
/// [spec2]: https://tc39.es/ecma262/#prod-ObjectAssignmentPattern
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct ObjectPattern {
    elements: Box<[ObjectPatternElement]>,
    span: Span,
}

impl ToInternedString for ObjectPattern {
    fn to_interned_string(&self, interner: &Interner) -> String {
        let mut buf = "{".to_owned();
        for (i, binding) in self.elements.iter().enumerate() {
            let binding = binding.to_interned_string(interner);
            let str = if i == self.elements.len() - 1 {
                format!("{binding} ")
            } else {
                format!("{binding},")
            };

            buf.push_str(&str);
        }
        if self.elements.is_empty() {
            buf.push(' ');
        }
        buf.push('}');
        buf
    }
}

impl ObjectPattern {
    /// Creates a new object binding pattern.
    #[inline]
    #[must_use]
    pub const fn new(elements: Box<[ObjectPatternElement]>, span: Span) -> Self {
        Self { elements, span }
    }

    /// Gets the bindings for the object binding pattern.
    #[inline]
    #[must_use]
    pub const fn bindings(&self) -> &[ObjectPatternElement] {
        &self.elements
    }

    /// Returns true if the object binding pattern has a rest element.
    #[inline]
    #[must_use]
    pub const fn has_rest(&self) -> bool {
        matches!(
            self.elements.last(),
            Some(ObjectPatternElement::RestProperty { .. })
        )
    }
}

impl Spanned for ObjectPattern {
    #[inline]
    fn span(&self) -> Span {
        self.span
    }
}

impl VisitWith for ObjectPattern {
    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: Visitor<'a>,
    {
        for elem in &*self.elements {
            visitor.visit_object_pattern_element(elem)?;
        }
        ControlFlow::Continue(())
    }

    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: VisitorMut<'a>,
    {
        for elem in &mut *self.elements {
            visitor.visit_object_pattern_element_mut(elem)?;
        }
        ControlFlow::Continue(())
    }
}

/// An array binding or assignment pattern.
///
/// Corresponds to the [`ArrayBindingPattern`][spec1] and the [`ArrayAssignmentPattern`][spec2]
/// Parse Nodes.
///
/// For more information on what is a valid binding in an array pattern, see [`ArrayPatternElement`].
///
/// [spec1]: https://tc39.es/ecma262/#prod-ArrayBindingPattern
/// [spec2]: https://tc39.es/ecma262/#prod-ArrayAssignmentPattern
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub struct ArrayPattern {
    bindings: Box<[ArrayPatternElement]>,
    span: Span,
}

impl ToInternedString for ArrayPattern {
    fn to_interned_string(&self, interner: &Interner) -> String {
        let mut buf = "[".to_owned();
        for (i, binding) in self.bindings.iter().enumerate() {
            if i == self.bindings.len() - 1 {
                match binding {
                    ArrayPatternElement::Elision => {
                        let _ = write!(buf, "{}, ", binding.to_interned_string(interner));
                    }
                    _ => {
                        let _ = write!(buf, "{} ", binding.to_interned_string(interner));
                    }
                }
            } else {
                let _ = write!(buf, "{},", binding.to_interned_string(interner));
            }
        }
        buf.push(']');
        buf
    }
}

impl ArrayPattern {
    /// Creates a new array binding pattern.
    #[inline]
    #[must_use]
    pub fn new(bindings: Box<[ArrayPatternElement]>, span: Span) -> Self {
        Self { bindings, span }
    }

    /// Gets the bindings for the array binding pattern.
    #[inline]
    #[must_use]
    pub const fn bindings(&self) -> &[ArrayPatternElement] {
        &self.bindings
    }
}

impl Spanned for ArrayPattern {
    #[inline]
    fn span(&self) -> Span {
        self.span
    }
}

impl VisitWith for ArrayPattern {
    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: Visitor<'a>,
    {
        for elem in &*self.bindings {
            visitor.visit_array_pattern_element(elem)?;
        }
        ControlFlow::Continue(())
    }

    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: VisitorMut<'a>,
    {
        for elem in &mut *self.bindings {
            visitor.visit_array_pattern_element_mut(elem)?;
        }
        ControlFlow::Continue(())
    }
}

/// The different types of bindings that an [`ObjectPattern`] may contain.
///
/// Corresponds to the [`BindingProperty`][spec1] and the [`AssignmentProperty`][spec2] nodes.
///
/// [spec1]: https://tc39.es/ecma262/#prod-BindingProperty
/// [spec2]: https://tc39.es/ecma262/#prod-AssignmentProperty
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub enum ObjectPatternElement {
    /// `SingleName` represents one of the following properties:
    ///
    /// - `SingleName` with an identifier and an optional default initializer.
    /// - `BindingProperty` with an property name and a `SingleNameBinding` as  the `BindingElement`.
    ///
    /// More information:
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - SingleNameBinding][spec1]
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - BindingProperty][spec2]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-SingleNameBinding
    /// [spec2]: https://tc39.es/ecma262/#prod-BindingProperty
    SingleName {
        /// The identifier name of the property to be destructured.
        name: PropertyName,
        /// The variable name where the property value will be stored.
        ident: Identifier,
        /// An optional default value for the variable, in case the property doesn't exist.
        default_init: Option<Expression>,
    },

    /// `RestProperty` represents a `BindingRestProperty` with an identifier.
    ///
    /// It also includes a list of the property keys that should be excluded from the rest,
    /// because they where already assigned.
    ///
    /// More information:
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - BindingRestProperty][spec1]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-BindingRestProperty
    RestProperty {
        /// The variable name where the unassigned properties will be stored.
        ident: Identifier,
    },

    /// `AssignmentGetField` represents an `AssignmentProperty` with an expression field member expression `AssignmentElement`.
    ///
    /// Note: According to the spec this is not part of an `ObjectBindingPattern`.
    /// This is only used when a object literal is used to cover an `AssignmentPattern`.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#prod-AssignmentProperty
    AssignmentPropertyAccess {
        /// The identifier name of the property to be destructured.
        name: PropertyName,
        /// The property access where the property value will be destructured.
        access: PropertyAccess,
        /// An optional default value for the variable, in case the property doesn't exist.
        default_init: Option<Expression>,
    },

    /// `AssignmentRestProperty` represents a rest property with a `DestructuringAssignmentTarget`.
    ///
    /// Note: According to the spec this is not part of an `ObjectBindingPattern`.
    /// This is only used when a object literal is used to cover an `AssignmentPattern`.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#prod-AssignmentRestProperty
    AssignmentRestPropertyAccess {
        /// The property access where the unassigned properties will be stored.
        access: PropertyAccess,
    },

    /// Pattern represents a property with a `Pattern` as the element.
    ///
    /// Additionally to the identifier of the new property and the nested pattern,
    /// this may also include an optional default initializer.
    ///
    /// More information:
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - BindingProperty][spec1]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-BindingProperty
    Pattern {
        /// The identifier name of the property to be destructured.
        name: PropertyName,
        /// The pattern where the property value will be destructured.
        pattern: Pattern,
        /// An optional default value for the variable, in case the property doesn't exist.
        default_init: Option<Expression>,
    },
}

impl ToInternedString for ObjectPatternElement {
    fn to_interned_string(&self, interner: &Interner) -> String {
        match self {
            Self::SingleName {
                ident,
                name,
                default_init,
            } => {
                let mut buf = match name {
                    PropertyName::Literal(name) if name == ident => {
                        format!(" {}", interner.resolve_expect(ident.sym()))
                    }
                    PropertyName::Literal(name) => {
                        format!(
                            " {} : {}",
                            interner.resolve_expect(name.sym()),
                            interner.resolve_expect(ident.sym())
                        )
                    }
                    PropertyName::Computed(node) => {
                        format!(
                            " [{}] : {}",
                            node.to_interned_string(interner),
                            interner.resolve_expect(ident.sym())
                        )
                    }
                };
                if let Some(init) = default_init {
                    let _ = write!(buf, " = {}", init.to_interned_string(interner));
                }
                buf
            }
            Self::RestProperty { ident } => {
                format!(" ... {}", interner.resolve_expect(ident.sym()))
            }
            Self::AssignmentRestPropertyAccess { access } => {
                format!(" ... {}", access.to_interned_string(interner))
            }
            Self::AssignmentPropertyAccess {
                name,
                access,
                default_init,
            } => {
                let mut buf = match name {
                    PropertyName::Literal(name) => {
                        format!(
                            " {} : {}",
                            interner.resolve_expect(name.sym()),
                            access.to_interned_string(interner)
                        )
                    }
                    PropertyName::Computed(node) => {
                        format!(
                            " [{}] : {}",
                            node.to_interned_string(interner),
                            access.to_interned_string(interner)
                        )
                    }
                };
                if let Some(init) = &default_init {
                    let _ = write!(buf, " = {}", init.to_interned_string(interner));
                }
                buf
            }
            Self::Pattern {
                name,
                pattern,
                default_init,
            } => {
                let mut buf = match name {
                    PropertyName::Literal(name) => {
                        format!(
                            " {} : {}",
                            interner.resolve_expect(name.sym()),
                            pattern.to_interned_string(interner),
                        )
                    }
                    PropertyName::Computed(node) => {
                        format!(
                            " [{}] : {}",
                            node.to_interned_string(interner),
                            pattern.to_interned_string(interner),
                        )
                    }
                };
                if let Some(init) = default_init {
                    let _ = write!(buf, " = {}", init.to_interned_string(interner));
                }
                buf
            }
        }
    }
}

impl VisitWith for ObjectPatternElement {
    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: Visitor<'a>,
    {
        match self {
            Self::SingleName {
                name,
                ident,
                default_init,
            } => {
                visitor.visit_property_name(name)?;
                visitor.visit_identifier(ident)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::RestProperty { ident, .. } => visitor.visit_identifier(ident),
            Self::AssignmentPropertyAccess {
                name,
                access,
                default_init,
            } => {
                visitor.visit_property_name(name)?;
                visitor.visit_property_access(access)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::AssignmentRestPropertyAccess { access, .. } => {
                visitor.visit_property_access(access)
            }
            Self::Pattern {
                name,
                pattern,
                default_init,
            } => {
                visitor.visit_property_name(name)?;
                visitor.visit_pattern(pattern)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
        }
    }

    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: VisitorMut<'a>,
    {
        match self {
            Self::SingleName {
                name,
                ident,
                default_init,
            } => {
                visitor.visit_property_name_mut(name)?;
                visitor.visit_identifier_mut(ident)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression_mut(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::RestProperty { ident, .. } => visitor.visit_identifier_mut(ident),
            Self::AssignmentPropertyAccess {
                name,
                access,
                default_init,
            } => {
                visitor.visit_property_name_mut(name)?;
                visitor.visit_property_access_mut(access)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression_mut(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::AssignmentRestPropertyAccess { access, .. } => {
                visitor.visit_property_access_mut(access)
            }
            Self::Pattern {
                name,
                pattern,
                default_init,
            } => {
                visitor.visit_property_name_mut(name)?;
                visitor.visit_pattern_mut(pattern)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression_mut(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
        }
    }
}

/// The different types of bindings that an array binding pattern may contain.
///
/// Corresponds to the [`BindingElement`][spec1] and the [`AssignmentElement`][spec2] nodes.
///
/// [spec1]: https://tc39.es/ecma262/#prod-BindingElement
/// [spec2]: https://tc39.es/ecma262/#prod-AssignmentElement
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[derive(Clone, Debug, PartialEq)]
pub enum ArrayPatternElement {
    /// Elision represents the elision of an item in the array binding pattern.
    ///
    /// An `Elision` may occur at multiple points in the pattern and may be multiple elisions.
    /// This variant strictly represents one elision. If there are multiple, this should be used multiple times.
    ///
    /// More information:
    ///  - [ECMAScript reference: 13.2.4 Array Initializer - Elision][spec1]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-Elision
    Elision,

    /// `SingleName` represents a `SingleName` with an identifier and an optional default initializer.
    ///
    /// More information:
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - SingleNameBinding][spec1]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-SingleNameBinding
    SingleName {
        /// The variable name where the index element will be stored.
        ident: Identifier,
        /// An optional default value for the variable, in case the index element doesn't exist.
        default_init: Option<Expression>,
    },

    /// `PropertyAccess` represents a binding with a property accessor.
    ///
    /// Note: According to the spec this is not part of an `ArrayBindingPattern`.
    /// This is only used when a array literal is used as the left-hand-side of an assignment expression.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#prod-AssignmentExpression
    PropertyAccess {
        /// The property access where the index element will be stored.
        access: PropertyAccess,
        /// An optional default value for the variable, in case the index element doesn't exist.
        default_init: Option<Expression>,
    },

    /// Pattern represents a `Pattern` in an `Element` of an array pattern.
    ///
    /// The pattern and the optional default initializer are both stored in the `DeclarationPattern`.
    ///
    /// More information:
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - BindingElement][spec1]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-BindingElement
    Pattern {
        /// The pattern where the index element will be stored.
        pattern: Pattern,
        /// An optional default value for the pattern, in case the index element doesn't exist.
        default_init: Option<Expression>,
    },

    /// `SingleNameRest` represents a `BindingIdentifier` in a `BindingRestElement` of an array pattern.
    ///
    /// More information:
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - BindingRestElement][spec1]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-BindingRestElement
    SingleNameRest {
        /// The variable where the unassigned index elements will be stored.
        ident: Identifier,
    },

    /// `PropertyAccess` represents a rest (spread operator) with a property accessor.
    ///
    /// Note: According to the spec this is not part of an `ArrayBindingPattern`.
    /// This is only used when a array literal is used as the left-hand-side of an assignment expression.
    ///
    /// More information:
    ///  - [ECMAScript reference][spec]
    ///
    /// [spec]: https://tc39.es/ecma262/#prod-AssignmentExpression
    PropertyAccessRest {
        /// The property access where the unassigned index elements will be stored.
        access: PropertyAccess,
    },

    /// `PatternRest` represents a `Pattern` in a `RestElement` of an array pattern.
    ///
    /// More information:
    ///  - [ECMAScript reference: 14.3.3 Destructuring Binding Patterns - BindingRestElement][spec1]
    ///
    /// [spec1]: https://tc39.es/ecma262/#prod-BindingRestElement
    PatternRest {
        /// The pattern where the unassigned index elements will be stored.
        pattern: Pattern,
    },
}

impl ToInternedString for ArrayPatternElement {
    fn to_interned_string(&self, interner: &Interner) -> String {
        match self {
            Self::Elision => " ".to_owned(),
            Self::SingleName {
                ident,
                default_init,
            } => {
                let mut buf = format!(" {}", interner.resolve_expect(ident.sym()));
                if let Some(init) = default_init {
                    let _ = write!(buf, " = {}", init.to_interned_string(interner));
                }
                buf
            }
            Self::PropertyAccess {
                access,
                default_init,
            } => {
                let mut buf = format!(" {}", access.to_interned_string(interner));
                if let Some(init) = default_init {
                    let _ = write!(buf, " = {}", init.to_interned_string(interner));
                }
                buf
            }
            Self::Pattern {
                pattern,
                default_init,
            } => {
                let mut buf = format!(" {}", pattern.to_interned_string(interner));
                if let Some(init) = default_init {
                    let _ = write!(buf, " = {}", init.to_interned_string(interner));
                }
                buf
            }
            Self::SingleNameRest { ident } => {
                format!(" ... {}", interner.resolve_expect(ident.sym()))
            }
            Self::PropertyAccessRest { access } => {
                format!(" ... {}", access.to_interned_string(interner))
            }
            Self::PatternRest { pattern } => {
                format!(" ... {}", pattern.to_interned_string(interner))
            }
        }
    }
}

impl VisitWith for ArrayPatternElement {
    fn visit_with<'a, V>(&'a self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: Visitor<'a>,
    {
        match self {
            Self::SingleName {
                ident,
                default_init,
            } => {
                visitor.visit_identifier(ident)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::PropertyAccess {
                access,
                default_init,
            } => {
                visitor.visit_property_access(access)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::PropertyAccessRest { access } => visitor.visit_property_access(access),
            Self::Pattern {
                pattern,
                default_init,
            } => {
                visitor.visit_pattern(pattern)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::SingleNameRest { ident } => visitor.visit_identifier(ident),
            Self::PatternRest { pattern } => visitor.visit_pattern(pattern),
            Self::Elision => {
                // special case to be handled by user
                ControlFlow::Continue(())
            }
        }
    }

    fn visit_with_mut<'a, V>(&'a mut self, visitor: &mut V) -> ControlFlow<V::BreakTy>
    where
        V: VisitorMut<'a>,
    {
        match self {
            Self::SingleName {
                ident,
                default_init,
            } => {
                visitor.visit_identifier_mut(ident)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression_mut(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::PropertyAccess {
                access,
                default_init,
            } => {
                visitor.visit_property_access_mut(access)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression_mut(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::PropertyAccessRest { access } => visitor.visit_property_access_mut(access),
            Self::Pattern {
                pattern,
                default_init,
            } => {
                visitor.visit_pattern_mut(pattern)?;
                if let Some(expr) = default_init {
                    visitor.visit_expression_mut(expr)
                } else {
                    ControlFlow::Continue(())
                }
            }
            Self::SingleNameRest { ident } => visitor.visit_identifier_mut(ident),
            Self::PatternRest { pattern } => visitor.visit_pattern_mut(pattern),
            Self::Elision => {
                // special case to be handled by user
                ControlFlow::Continue(())
            }
        }
    }
}