swc_estree_compat/swcify/
pat.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
use swc_common::Spanned;
use swc_ecma_ast::*;
use swc_estree_ast::{
    ArrayPattern, AssignmentPattern, AssignmentPatternLeft, LVal, ObjectPattern, ObjectPatternProp,
    PatternLike, RestElement,
};

use crate::swcify::{Context, Swcify};

impl Swcify for LVal {
    type Output = Pat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        match self {
            LVal::Id(i) => i.swcify(ctx).into(),
            LVal::MemberExpr(e) => Box::new(e.swcify(ctx)).into(),
            LVal::RestEl(e) => e.swcify(ctx).into(),
            LVal::AssignmentPat(e) => e.swcify(ctx).into(),
            LVal::ArrayPat(e) => e.swcify(ctx).into(),
            LVal::ObjectPat(e) => e.swcify(ctx).into(),
            LVal::TSParamProp(..) => todo!(),
        }
    }
}

impl Swcify for RestElement {
    type Output = RestPat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        let span = ctx.span(&self.base);

        RestPat {
            span,
            dot3_token: span,
            arg: Box::new(self.argument.swcify(ctx)),
            type_ann: None,
        }
    }
}

impl Swcify for AssignmentPattern {
    type Output = AssignPat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        AssignPat {
            span: ctx.span(&self.base),
            left: Box::new(self.left.swcify(ctx)),
            right: self.right.swcify(ctx),
        }
    }
}

impl Swcify for AssignmentPatternLeft {
    type Output = Pat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        match self {
            AssignmentPatternLeft::Id(v) => v.swcify(ctx).into(),
            AssignmentPatternLeft::Object(v) => v.swcify(ctx).into(),
            AssignmentPatternLeft::Array(v) => v.swcify(ctx).into(),
            AssignmentPatternLeft::Member(v) => Box::new(v.swcify(ctx)).into(),
        }
    }
}

impl Swcify for ArrayPattern {
    type Output = ArrayPat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        ArrayPat {
            span: ctx.span(&self.base),
            elems: self.elements.swcify(ctx),
            optional: false,
            type_ann: None,
        }
    }
}

impl Swcify for PatternLike {
    type Output = Pat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        match self {
            PatternLike::Id(v) => v.swcify(ctx).into(),
            PatternLike::RestEl(v) => v.swcify(ctx).into(),
            PatternLike::AssignmentPat(v) => v.swcify(ctx).into(),
            PatternLike::ArrayPat(v) => v.swcify(ctx).into(),
            PatternLike::ObjectPat(v) => v.swcify(ctx).into(),
        }
    }
}

impl Swcify for ObjectPattern {
    type Output = ObjectPat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        ObjectPat {
            span: ctx.span(&self.base),
            props: self.properties.swcify(ctx),
            optional: false,
            type_ann: None,
        }
    }
}

impl Swcify for ObjectPatternProp {
    type Output = ObjectPatProp;

    fn swcify(self, ctx: &Context) -> Self::Output {
        match self {
            ObjectPatternProp::Rest(v) => ObjectPatProp::Rest(v.swcify(ctx)),
            ObjectPatternProp::Prop(prop) => {
                if prop.shorthand {
                    return ObjectPatProp::Assign(AssignPatProp {
                        span: ctx.span(&prop.base),
                        key: prop.key.swcify(ctx).expect_ident().into(),
                        value: None,
                    });
                }

                match prop.value {
                    swc_estree_ast::ObjectPropVal::Pattern(v) => {
                        ObjectPatProp::KeyValue(KeyValuePatProp {
                            key: prop.key.swcify(ctx),
                            value: Box::new(v.swcify(ctx)),
                        })
                    }
                    swc_estree_ast::ObjectPropVal::Expr(v) => {
                        ObjectPatProp::Assign(AssignPatProp {
                            span: ctx.span(&prop.base),
                            key: prop.key.swcify(ctx).expect_ident().into(),
                            value: Some(v.swcify(ctx)),
                        })
                    }
                }
            }
        }
    }
}

impl Swcify for swc_estree_ast::Pattern {
    type Output = Pat;

    fn swcify(self, ctx: &Context) -> Self::Output {
        match self {
            swc_estree_ast::Pattern::Assignment(v) => v.swcify(ctx).into(),
            swc_estree_ast::Pattern::Array(v) => v.swcify(ctx).into(),
            swc_estree_ast::Pattern::Object(v) => v.swcify(ctx).into(),
        }
    }
}

impl Swcify for swc_estree_ast::Param {
    type Output = swc_ecma_ast::Param;

    fn swcify(self, ctx: &Context) -> Self::Output {
        match self {
            swc_estree_ast::Param::Id(v) => {
                let pat = v.swcify(ctx);

                swc_ecma_ast::Param {
                    span: pat.span(),
                    decorators: Default::default(),
                    pat: pat.into(),
                }
            }
            swc_estree_ast::Param::Pat(v) => {
                let pat = v.swcify(ctx);

                swc_ecma_ast::Param {
                    span: pat.span(),
                    decorators: Default::default(),
                    pat,
                }
            }
            swc_estree_ast::Param::Rest(v) => swc_ecma_ast::Param {
                span: ctx.span(&v.base),
                decorators: v.decorators.swcify(ctx).unwrap_or_default(),
                pat: v.argument.swcify(ctx),
            },
            swc_estree_ast::Param::TSProp(..) => todo!(),
        }
    }
}