1use is_macro::Is;
2use swc_common::{ast_node, util::take::Take, EqIgnoreSpan, Span, DUMMY_SP};
3
4use crate::{
5 expr::Expr,
6 function::Function,
7 ident::Ident,
8 lit::{BigInt, Number, Str},
9 stmt::BlockStmt,
10 typescript::TsTypeAnn,
11 Id, IdentName, MemberProp, Pat,
12};
13
14#[ast_node]
15#[derive(Eq, Hash, Is, EqIgnoreSpan)]
16#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
17#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
18pub enum Prop {
19 #[tag("Identifier")]
21 Shorthand(Ident),
22
23 #[tag("KeyValueProperty")]
25 KeyValue(KeyValueProp),
26
27 #[tag("AssignmentProperty")]
29 Assign(AssignProp),
30
31 #[tag("GetterProperty")]
32 Getter(GetterProp),
33
34 #[tag("SetterProperty")]
35 Setter(SetterProp),
36
37 #[tag("MethodProperty")]
38 Method(MethodProp),
39}
40
41bridge_from!(Prop, Ident, IdentName);
42
43#[ast_node("KeyValueProperty")]
44#[derive(Eq, Hash, EqIgnoreSpan)]
45#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
46#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
47pub struct KeyValueProp {
48 #[span(lo)]
49 pub key: PropName,
50
51 #[span(hi)]
52 pub value: Box<Expr>,
53}
54
55#[ast_node("AssignmentProperty")]
56#[derive(Eq, Hash, EqIgnoreSpan)]
57#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
58#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
59pub struct AssignProp {
60 pub span: Span,
61 pub key: Ident,
62 pub value: Box<Expr>,
63}
64
65#[ast_node("GetterProperty")]
66#[derive(Eq, Hash, EqIgnoreSpan, Default)]
67#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
68#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
69pub struct GetterProp {
70 pub span: Span,
71 pub key: PropName,
72 #[cfg_attr(feature = "serde-impl", serde(default, rename = "typeAnnotation"))]
73 #[cfg_attr(
74 feature = "encoding-impl",
75 encoding(with = "cbor4ii::core::types::Maybe")
76 )]
77 pub type_ann: Option<Box<TsTypeAnn>>,
78 #[cfg_attr(feature = "serde-impl", serde(default))]
79 #[cfg_attr(
80 feature = "encoding-impl",
81 encoding(with = "cbor4ii::core::types::Maybe")
82 )]
83 pub body: Option<BlockStmt>,
84}
85#[ast_node("SetterProperty")]
86#[derive(Eq, Hash, EqIgnoreSpan, Default)]
87#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
88#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
89pub struct SetterProp {
90 pub span: Span,
91 pub key: PropName,
92 #[cfg_attr(
93 feature = "encoding-impl",
94 encoding(with = "cbor4ii::core::types::Maybe")
95 )]
96 pub this_param: Option<Pat>,
97 pub param: Box<Pat>,
98 #[cfg_attr(feature = "serde-impl", serde(default))]
99 #[cfg_attr(
100 feature = "encoding-impl",
101 encoding(with = "cbor4ii::core::types::Maybe")
102 )]
103 pub body: Option<BlockStmt>,
104}
105#[ast_node("MethodProperty")]
106#[derive(Eq, Hash, EqIgnoreSpan)]
107#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
108#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
109pub struct MethodProp {
110 pub key: PropName,
111
112 #[cfg_attr(feature = "serde-impl", serde(flatten))]
113 #[span]
114 pub function: Box<Function>,
115}
116
117#[ast_node]
118#[derive(Eq, Hash, Is, EqIgnoreSpan)]
119#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
120#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
121pub enum PropName {
122 #[tag("Identifier")]
123 Ident(IdentName),
124 #[tag("StringLiteral")]
126 Str(Str),
127 #[tag("NumericLiteral")]
129 Num(Number),
130 #[tag("Computed")]
131 Computed(ComputedPropName),
132 #[tag("BigIntLiteral")]
133 BigInt(BigInt),
134}
135
136bridge_from!(PropName, IdentName, Ident);
137bridge_from!(PropName, Ident, Id);
138
139impl Default for PropName {
140 fn default() -> Self {
141 PropName::Ident(Default::default())
142 }
143}
144
145impl Take for PropName {
146 fn dummy() -> Self {
147 PropName::Ident(Take::dummy())
148 }
149}
150
151impl From<PropName> for MemberProp {
152 fn from(p: PropName) -> Self {
153 match p {
154 PropName::Ident(p) => MemberProp::Ident(p),
155 PropName::Computed(p) => MemberProp::Computed(p),
156 PropName::Str(p) => MemberProp::Computed(ComputedPropName {
157 span: DUMMY_SP,
158 expr: p.into(),
159 }),
160 PropName::Num(p) => MemberProp::Computed(ComputedPropName {
161 span: DUMMY_SP,
162 expr: p.into(),
163 }),
164 PropName::BigInt(p) => MemberProp::Computed(ComputedPropName {
165 span: DUMMY_SP,
166 expr: p.into(),
167 }),
168 #[cfg(all(swc_ast_unknown, feature = "encoding-impl"))]
169 _ => swc_common::unknown!(),
170 }
171 }
172}
173
174#[ast_node("Computed")]
175#[derive(Eq, Hash, EqIgnoreSpan)]
176#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
177#[cfg_attr(feature = "shrink-to-fit", derive(shrink_to_fit::ShrinkToFit))]
178pub struct ComputedPropName {
179 pub span: Span,
181 #[cfg_attr(feature = "serde-impl", serde(rename = "expression"))]
182 pub expr: Box<Expr>,
183}
184
185impl Take for ComputedPropName {
186 fn dummy() -> Self {
187 Self {
188 span: DUMMY_SP,
189 expr: Take::dummy(),
190 }
191 }
192}