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
use std::mem::replace;

use rnode::NodeId;
use stc_ts_ast_rnode::{
    RBool, RClassMember, RDecl, REmptyStmt, RExpr, RIdent, RInvalid, RModuleItem, RPat, RPropName, RStmt, RTsKeywordType, RTsLit, RTsType,
    RVarDecl,
};
use stc_ts_types::{LitType, Type};
use swc_atoms::js_word;
use swc_common::DUMMY_SP;
use swc_ecma_ast::*;

/// Helper for migration from [Fold] to [VisitMut]
pub trait MapWithMut: Sized {
    fn dummy() -> Self;

    fn take(&mut self) -> Self {
        replace(self, Self::dummy())
    }

    #[inline]
    fn map_with_mut<F>(&mut self, op: F)
    where
        F: FnOnce(Self) -> Self,
    {
        let dummy = Self::dummy();
        let v = replace(self, dummy);
        let v = op(v);
        let _dummy = replace(self, v);
    }
}

impl MapWithMut for Type {
    fn dummy() -> Self {
        Type::Lit(LitType {
            span: DUMMY_SP,
            lit: RTsLit::Bool(RBool {
                span: DUMMY_SP,
                value: false,
            }),
            metadata: Default::default(),
            tracker: Default::default(),
        })
    }
}

impl MapWithMut for RPat {
    fn dummy() -> Self {
        RPat::Invalid(RInvalid { span: DUMMY_SP })
    }
}

impl MapWithMut for RTsType {
    fn dummy() -> Self {
        RTsType::TsKeywordType(RTsKeywordType {
            span: DUMMY_SP,
            kind: TsKeywordTypeKind::TsNeverKeyword,
        })
    }
}

impl MapWithMut for RClassMember {
    fn dummy() -> Self {
        RClassMember::Empty(REmptyStmt { span: DUMMY_SP })
    }
}

impl MapWithMut for RExpr {
    fn dummy() -> Self {
        RExpr::Invalid(RInvalid { span: DUMMY_SP })
    }
}

impl MapWithMut for RIdent {
    fn dummy() -> Self {
        RIdent::new(js_word!(""), DUMMY_SP)
    }
}

impl MapWithMut for RPropName {
    fn dummy() -> Self {
        RPropName::Ident(RIdent::dummy())
    }
}

impl MapWithMut for RDecl {
    fn dummy() -> Self {
        RDecl::Var(box RVarDecl {
            node_id: NodeId::invalid(),
            span: DUMMY_SP,
            kind: VarDeclKind::Var,
            declare: false,
            decls: vec![],
        })
    }
}

impl<T> MapWithMut for Vec<T> {
    fn dummy() -> Self {
        Vec::new()
    }
}

impl MapWithMut for RStmt {
    fn dummy() -> Self {
        RStmt::Empty(REmptyStmt { span: DUMMY_SP })
    }
}

impl MapWithMut for RModuleItem {
    fn dummy() -> Self {
        RStmt::dummy().into()
    }
}