swc_estree_compat/swcify/
pat.rs1use swc_common::Spanned;
2use swc_ecma_ast::*;
3use swc_estree_ast::{
4 ArrayPattern, AssignmentPattern, AssignmentPatternLeft, LVal, ObjectPattern, ObjectPatternProp,
5 PatternLike, RestElement,
6};
7
8use crate::swcify::{Context, Swcify};
9
10impl Swcify for LVal {
11 type Output = Pat;
12
13 fn swcify(self, ctx: &Context) -> Self::Output {
14 match self {
15 LVal::Id(i) => i.swcify(ctx).into(),
16 LVal::MemberExpr(e) => Box::new(e.swcify(ctx)).into(),
17 LVal::RestEl(e) => e.swcify(ctx).into(),
18 LVal::AssignmentPat(e) => e.swcify(ctx).into(),
19 LVal::ArrayPat(e) => e.swcify(ctx).into(),
20 LVal::ObjectPat(e) => e.swcify(ctx).into(),
21 LVal::TSParamProp(..) => todo!(),
22 }
23 }
24}
25
26impl Swcify for RestElement {
27 type Output = RestPat;
28
29 fn swcify(self, ctx: &Context) -> Self::Output {
30 let span = ctx.span(&self.base);
31
32 RestPat {
33 span,
34 dot3_token: span,
35 arg: Box::new(self.argument.swcify(ctx)),
36 type_ann: None,
37 }
38 }
39}
40
41impl Swcify for AssignmentPattern {
42 type Output = AssignPat;
43
44 fn swcify(self, ctx: &Context) -> Self::Output {
45 AssignPat {
46 span: ctx.span(&self.base),
47 left: Box::new(self.left.swcify(ctx)),
48 right: self.right.swcify(ctx),
49 }
50 }
51}
52
53impl Swcify for AssignmentPatternLeft {
54 type Output = Pat;
55
56 fn swcify(self, ctx: &Context) -> Self::Output {
57 match self {
58 AssignmentPatternLeft::Id(v) => v.swcify(ctx).into(),
59 AssignmentPatternLeft::Object(v) => v.swcify(ctx).into(),
60 AssignmentPatternLeft::Array(v) => v.swcify(ctx).into(),
61 AssignmentPatternLeft::Member(v) => Box::new(v.swcify(ctx)).into(),
62 }
63 }
64}
65
66impl Swcify for ArrayPattern {
67 type Output = ArrayPat;
68
69 fn swcify(self, ctx: &Context) -> Self::Output {
70 ArrayPat {
71 span: ctx.span(&self.base),
72 elems: self.elements.swcify(ctx),
73 optional: false,
74 type_ann: None,
75 }
76 }
77}
78
79impl Swcify for PatternLike {
80 type Output = Pat;
81
82 fn swcify(self, ctx: &Context) -> Self::Output {
83 match self {
84 PatternLike::Id(v) => v.swcify(ctx).into(),
85 PatternLike::RestEl(v) => v.swcify(ctx).into(),
86 PatternLike::AssignmentPat(v) => v.swcify(ctx).into(),
87 PatternLike::ArrayPat(v) => v.swcify(ctx).into(),
88 PatternLike::ObjectPat(v) => v.swcify(ctx).into(),
89 }
90 }
91}
92
93impl Swcify for ObjectPattern {
94 type Output = ObjectPat;
95
96 fn swcify(self, ctx: &Context) -> Self::Output {
97 ObjectPat {
98 span: ctx.span(&self.base),
99 props: self.properties.swcify(ctx),
100 optional: false,
101 type_ann: None,
102 }
103 }
104}
105
106impl Swcify for ObjectPatternProp {
107 type Output = ObjectPatProp;
108
109 fn swcify(self, ctx: &Context) -> Self::Output {
110 match self {
111 ObjectPatternProp::Rest(v) => ObjectPatProp::Rest(v.swcify(ctx)),
112 ObjectPatternProp::Prop(prop) => {
113 if prop.shorthand {
114 return ObjectPatProp::Assign(AssignPatProp {
115 span: ctx.span(&prop.base),
116 key: prop.key.swcify(ctx).expect_ident().into(),
117 value: None,
118 });
119 }
120
121 match prop.value {
122 swc_estree_ast::ObjectPropVal::Pattern(v) => {
123 ObjectPatProp::KeyValue(KeyValuePatProp {
124 key: prop.key.swcify(ctx),
125 value: Box::new(v.swcify(ctx)),
126 })
127 }
128 swc_estree_ast::ObjectPropVal::Expr(v) => {
129 ObjectPatProp::Assign(AssignPatProp {
130 span: ctx.span(&prop.base),
131 key: prop.key.swcify(ctx).expect_ident().into(),
132 value: Some(v.swcify(ctx)),
133 })
134 }
135 }
136 }
137 }
138 }
139}
140
141impl Swcify for swc_estree_ast::Pattern {
142 type Output = Pat;
143
144 fn swcify(self, ctx: &Context) -> Self::Output {
145 match self {
146 swc_estree_ast::Pattern::Assignment(v) => v.swcify(ctx).into(),
147 swc_estree_ast::Pattern::Array(v) => v.swcify(ctx).into(),
148 swc_estree_ast::Pattern::Object(v) => v.swcify(ctx).into(),
149 }
150 }
151}
152
153impl Swcify for swc_estree_ast::Param {
154 type Output = swc_ecma_ast::Param;
155
156 fn swcify(self, ctx: &Context) -> Self::Output {
157 match self {
158 swc_estree_ast::Param::Id(v) => {
159 let pat = v.swcify(ctx);
160
161 swc_ecma_ast::Param {
162 span: pat.span(),
163 decorators: Default::default(),
164 pat: pat.into(),
165 }
166 }
167 swc_estree_ast::Param::Pat(v) => {
168 let pat = v.swcify(ctx);
169
170 swc_ecma_ast::Param {
171 span: pat.span(),
172 decorators: Default::default(),
173 pat,
174 }
175 }
176 swc_estree_ast::Param::Rest(v) => swc_ecma_ast::Param {
177 span: ctx.span(&v.base),
178 decorators: v.decorators.swcify(ctx).unwrap_or_default(),
179 pat: v.argument.swcify(ctx),
180 },
181 swc_estree_ast::Param::TSProp(..) => todo!(),
182 }
183 }
184}