swc_visit/
lib.rs

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
//! Visitor generator for the rust language.
//!
//!
//! There are three variants of visitor in swc. Those are `Fold`, `VisitMut`,
//! `Visit`.
//!
//! # Comparisons
//!
//! ## `Fold` vs `VisitMut`
//!
//! `Fold` and `VisitMut` do almost identical tasks, but `Fold` is easier to use
//! while being slower and weak to stack overflow for very deep asts. `Fold` is
//! fast enough for almost all cases so it would be better to start with `Fold`.
//!
//! By very deep asts, I meant code like thousands of `a + a + a + a + ...`.
//!
//!
//! # `Fold`
//!
//! > WARNING: `Fold` is slow, and it's recommended to use VisitMut if you are
//! > experienced.
//!
//!
//! `Fold` takes ownership of value, which means you have to return the new
//! value. Returning new value means returning ownership of the value. But you
//! don't have to care about ownership or about managing memories while using
//! such visitors. `rustc` handles them automatically and all allocations will
//! be freed when it goes out of the scope.
//!
//! You can invoke your `Fold` implementation like `node.fold_with(&mut
//! visitor)` where `visitor` is your visitor. Note that as it takes ownership
//! of value, you have to call `node.fold_children_with(self)` in e.g. `fn
//! fold_module(&mut self, m: Module) -> Module` if you override the default
//! behavior. Also you have to store return value from `fold_children_with`,
//! like `let node = node.fold_children_with(self)`. Order of execution can be
//! controlled using this. If there is some logic that should be applied to the
//! parent first, you can call `fold_children_with` after such logic.
//!
//! # `VisitMut`
//!
//! `VisitMut` uses a mutable reference to AST nodes (e.g. `&mut Expr`). You can
//! use `Take` from `swc_common::util::take::Take` to get owned value from a
//! mutable reference.
//!
//! You will typically use code like
//!
//! ```ignore
//! *e = return_value.take();
//! ```
//!
//! where `e = &mut Expr` and `return_value` is also `&mut Expr`. `take()` is an
//! extension method defined on `MapWithMut`.  It's almost identical to `Fold`,
//! so I'll skip memory management.
//!
//! You can invoke your `VisitMut` implementation like `node.visit_mut_with(&mut
//! visitor)` where `visitor` is your visitor. Again, you need to call
//! `node.visit_mut_children_with(self)` in visitor implementation if you want
//! to modify children nodes. You don't need to store the return value in this
//! case.
//!
//!
//! # `Visit`
//!
//!`Visit` uses non-mutable references to AST nodes. It can be used to see if
//! an AST node contains a specific node nested deeply in the AST. This is
//! useful for checking if AST node contains `this`. This is useful for lots of
//! cases - `this` in arrow expressions are special and we need to generate
//! different code if a `this` expression is used.
//!
//! You can use your `Visit` implementation like  `node.visit_with(&Invalid{
//! span: DUMMY_SP, }, &mut visitor`. I think API is mis-designed, but it works
//! and there are really lots of code using `Visit` already.
//!
//!
//!
//! # Cargo features
//!
//! You should add
//! ```toml
//! [features]
//! path = []
//! ```
//!
//! If you want to allow using path-aware visitor.
//!
//!
//! # Path-aware visitor
//!
//! Path-aware visitor is a visitor that can be used to visit AST nodes with
//! current path from the entrypoint.
//!
//! `VisitMutAstPath` and `FoldAstPath` can be used to transform AST nodes with
//! the path to the node.

use std::ops::{Deref, DerefMut};

pub use either::Either;

pub mod util;

/// Visit all children nodes. This converts `VisitAll` to `Visit`. The type
/// parameter `V` should implement `VisitAll` and `All<V>` implements `Visit`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct All<V> {
    pub visitor: V,
}

/// A visitor which visits node only if `enabled` is true.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Optional<V> {
    pub enabled: bool,
    pub visitor: V,
}

impl<V> Optional<V> {
    pub const fn new(visitor: V, enabled: bool) -> Self {
        Self { enabled, visitor }
    }
}

/// Trait for a pass which is designed to invoked multiple time to same input.
///
/// See [Repeat].
pub trait Repeated {
    /// Should run again?
    fn changed(&self) -> bool;

    /// Reset.
    fn reset(&mut self);
}

macro_rules! impl_repeated_for_tuple {
    (
        [$idx:tt, $name:ident], $([$idx_rest:tt, $name_rest:ident]),*
    ) => {
        impl<$name, $($name_rest),*> Repeated for ($name, $($name_rest),*)
        where
            $name: Repeated,
            $($name_rest: Repeated),*
        {
            fn changed(&self) -> bool {
                self.$idx.changed() || $(self.$idx_rest.changed() ||)* false
            }

            fn reset(&mut self) {
                self.$idx.reset();
                $(self.$idx_rest.reset();)*
            }
        }
    };
}

impl_repeated_for_tuple!([0, A], [1, B]);
impl_repeated_for_tuple!([0, A], [1, B], [2, C]);
impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D]);
impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E]);
impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F]);
impl_repeated_for_tuple!([0, A], [1, B], [2, C], [3, D], [4, E], [5, F], [6, G]);
impl_repeated_for_tuple!(
    [0, A],
    [1, B],
    [2, C],
    [3, D],
    [4, E],
    [5, F],
    [6, G],
    [7, H]
);
impl_repeated_for_tuple!(
    [0, A],
    [1, B],
    [2, C],
    [3, D],
    [4, E],
    [5, F],
    [6, G],
    [7, H],
    [8, I]
);
impl_repeated_for_tuple!(
    [0, A],
    [1, B],
    [2, C],
    [3, D],
    [4, E],
    [5, F],
    [6, G],
    [7, H],
    [8, I],
    [9, J]
);
impl_repeated_for_tuple!(
    [0, A],
    [1, B],
    [2, C],
    [3, D],
    [4, E],
    [5, F],
    [6, G],
    [7, H],
    [8, I],
    [9, J],
    [10, K]
);
impl_repeated_for_tuple!(
    [0, A],
    [1, B],
    [2, C],
    [3, D],
    [4, E],
    [5, F],
    [6, G],
    [7, H],
    [8, I],
    [9, J],
    [10, K],
    [11, L]
);
impl_repeated_for_tuple!(
    [0, A],
    [1, B],
    [2, C],
    [3, D],
    [4, E],
    [5, F],
    [6, G],
    [7, H],
    [8, I],
    [9, J],
    [10, K],
    [11, L],
    [12, M]
);

/// A visitor which applies `V` again and again if `V` modifies the node.
///
/// # Note
/// `V` should return `true` from `changed()` to make the pass run multiple
/// time.
///
/// See: [Repeated]
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
pub struct Repeat<V>
where
    V: Repeated,
{
    pub pass: V,
}

impl<V> Repeat<V>
where
    V: Repeated,
{
    pub fn new(pass: V) -> Self {
        Self { pass }
    }
}

impl<V> Repeated for Repeat<V>
where
    V: Repeated,
{
    fn changed(&self) -> bool {
        self.pass.changed()
    }

    fn reset(&mut self) {
        self.pass.reset()
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AstKindPath<K>
where
    K: ParentKind,
{
    path: Vec<K>,
}

impl<K> std::ops::Deref for AstKindPath<K>
where
    K: ParentKind,
{
    type Target = Vec<K>;

    fn deref(&self) -> &Self::Target {
        &self.path
    }
}

impl<K> Default for AstKindPath<K>
where
    K: ParentKind,
{
    fn default() -> Self {
        Self {
            path: Default::default(),
        }
    }
}

impl<K> AstKindPath<K>
where
    K: ParentKind,
{
    pub fn new(path: Vec<K>) -> Self {
        Self { path }
    }

    pub fn with_guard(&mut self, kind: K) -> AstKindPathGuard<K> {
        self.path.push(kind);

        AstKindPathGuard { path: self }
    }

    pub fn with_index_guard(&mut self, index: usize) -> AstKindPathIndexGuard<K> {
        self.path.last_mut().unwrap().set_index(index);

        AstKindPathIndexGuard { path: self }
    }

    #[deprecated = "Use with_guard instead"]
    pub fn with<Ret>(&mut self, path: K, op: impl FnOnce(&mut Self) -> Ret) -> Ret {
        self.path.push(path);
        let ret = op(self);
        self.path.pop();
        ret
    }

    #[deprecated = "Use with_index_guard instead"]
    pub fn with_index<Ret>(&mut self, index: usize, op: impl FnOnce(&mut Self) -> Ret) -> Ret {
        self.path.last_mut().unwrap().set_index(index);
        let res = op(self);
        self.path.last_mut().unwrap().set_index(usize::MAX);
        res
    }
}

pub struct AstKindPathGuard<'a, K>
where
    K: ParentKind,
{
    path: &'a mut AstKindPath<K>,
}

impl<K> Deref for AstKindPathGuard<'_, K>
where
    K: ParentKind,
{
    type Target = AstKindPath<K>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.path
    }
}

impl<K> DerefMut for AstKindPathGuard<'_, K>
where
    K: ParentKind,
{
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.path
    }
}

impl<K> Drop for AstKindPathGuard<'_, K>
where
    K: ParentKind,
{
    fn drop(&mut self) {
        self.path.path.pop();
    }
}

pub struct AstKindPathIndexGuard<'a, K>
where
    K: ParentKind,
{
    path: &'a mut AstKindPath<K>,
}

impl<K> Deref for AstKindPathIndexGuard<'_, K>
where
    K: ParentKind,
{
    type Target = AstKindPath<K>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.path
    }
}

impl<K> DerefMut for AstKindPathIndexGuard<'_, K>
where
    K: ParentKind,
{
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.path
    }
}

impl<K> Drop for AstKindPathIndexGuard<'_, K>
where
    K: ParentKind,
{
    fn drop(&mut self) {
        self.path.path.last_mut().unwrap().set_index(usize::MAX);
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AstNodePath<N>
where
    N: NodeRef,
{
    kinds: AstKindPath<N::ParentKind>,
    path: Vec<N>,
}

impl<N> std::ops::Deref for AstNodePath<N>
where
    N: NodeRef,
{
    type Target = Vec<N>;

    fn deref(&self) -> &Self::Target {
        &self.path
    }
}

impl<N> Default for AstNodePath<N>
where
    N: NodeRef,
{
    fn default() -> Self {
        Self {
            kinds: Default::default(),
            path: Default::default(),
        }
    }
}

impl<N> AstNodePath<N>
where
    N: NodeRef,
{
    pub fn new(kinds: AstKindPath<N::ParentKind>, path: Vec<N>) -> Self {
        Self { kinds, path }
    }

    pub fn kinds(&self) -> &AstKindPath<N::ParentKind> {
        &self.kinds
    }

    pub fn with_guard(&mut self, node: N) -> AstNodePathGuard<N> {
        self.kinds.path.push(node.kind());
        self.path.push(node);

        AstNodePathGuard { path: self }
    }

    pub fn with_index_guard(&mut self, index: usize) -> AstNodePathIndexGuard<N> {
        self.kinds.path.last_mut().unwrap().set_index(index);
        self.path.last_mut().unwrap().set_index(index);

        AstNodePathIndexGuard { path: self }
    }

    #[deprecated = "Use with_guard instead"]
    pub fn with<F, Ret>(&mut self, node: N, op: F) -> Ret
    where
        F: for<'aa> FnOnce(&'aa mut AstNodePath<N>) -> Ret,
    {
        let kind = node.kind();

        self.kinds.path.push(kind);
        self.path.push(node);
        let ret = op(self);
        self.path.pop();
        self.kinds.path.pop();

        ret
    }

    #[deprecated = "Use with_index_guard instead"]
    pub fn with_index<F, Ret>(&mut self, index: usize, op: F) -> Ret
    where
        F: for<'aa> FnOnce(&'aa mut AstNodePath<N>) -> Ret,
    {
        self.kinds.path.last_mut().unwrap().set_index(index);
        self.path.last_mut().unwrap().set_index(index);

        let res = op(self);

        self.path.last_mut().unwrap().set_index(usize::MAX);
        self.kinds.path.last_mut().unwrap().set_index(usize::MAX);
        res
    }
}

pub trait NodeRef: Copy {
    type ParentKind: ParentKind;

    fn kind(&self) -> Self::ParentKind;

    fn set_index(&mut self, index: usize);
}

pub trait ParentKind: Copy {
    fn set_index(&mut self, index: usize);
}

pub struct AstNodePathGuard<'a, N>
where
    N: NodeRef,
{
    path: &'a mut AstNodePath<N>,
}

impl<N> Deref for AstNodePathGuard<'_, N>
where
    N: NodeRef,
{
    type Target = AstNodePath<N>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.path
    }
}

impl<N> DerefMut for AstNodePathGuard<'_, N>
where
    N: NodeRef,
{
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.path
    }
}

impl<N> Drop for AstNodePathGuard<'_, N>
where
    N: NodeRef,
{
    fn drop(&mut self) {
        self.path.path.pop();
        self.path.kinds.path.pop();
    }
}

pub struct AstNodePathIndexGuard<'a, N>
where
    N: NodeRef,
{
    path: &'a mut AstNodePath<N>,
}

impl<N> Deref for AstNodePathIndexGuard<'_, N>
where
    N: NodeRef,
{
    type Target = AstNodePath<N>;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.path
    }
}

impl<N> DerefMut for AstNodePathIndexGuard<'_, N>
where
    N: NodeRef,
{
    #[inline]
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.path
    }
}

impl<N> Drop for AstNodePathIndexGuard<'_, N>
where
    N: NodeRef,
{
    fn drop(&mut self) {
        self.path.path.last_mut().unwrap().set_index(usize::MAX);
        self.path
            .kinds
            .path
            .last_mut()
            .unwrap()
            .set_index(usize::MAX);
    }
}

/// NOT A PUBLIC API
#[doc(hidden)]
pub fn wrong_ast_path() {
    unsafe {
        debug_unreachable::debug_unreachable!("Wrong ast path");
    }
}