@@ -7,12 +7,6 @@ use swc_core::{
7
7
} ;
8
8
use super :: core;
9
9
10
- pub enum AttributeValue {
11
- Bool ( bool ) ,
12
- Num ( f64 ) ,
13
- Str ( String ) ,
14
- }
15
-
16
10
pub enum AttributePosition {
17
11
Start ,
18
12
End ,
@@ -21,7 +15,7 @@ pub enum AttributePosition {
21
15
#[ derive( Default ) ]
22
16
pub struct Attribute {
23
17
pub name : String ,
24
- pub value : Option < AttributeValue > ,
18
+ pub value : Option < String > ,
25
19
pub spread : bool ,
26
20
pub literal : bool ,
27
21
pub position : Option < AttributePosition > ,
@@ -43,40 +37,31 @@ impl Visitor {
43
37
}
44
38
}
45
39
46
- let _ref = match config. _ref {
47
- Some ( r) => r,
48
- None => false
49
- } ;
40
+ let _ref = config. _ref . unwrap_or ( false ) ;
50
41
if _ref {
51
42
attributes. push ( Attribute {
52
43
name : "ref" . to_string ( ) ,
53
- value : Some ( AttributeValue :: Str ( "ref" . to_string ( ) ) ) ,
44
+ value : Some ( "ref" . to_string ( ) ) ,
54
45
literal : true ,
55
46
..Default :: default ( )
56
47
} ) ;
57
48
}
58
49
59
- let title_prop = match config. title_prop {
60
- Some ( r) => r,
61
- None => false
62
- } ;
50
+ let title_prop = config. title_prop . unwrap_or ( false ) ;
63
51
if title_prop {
64
52
attributes. push ( Attribute {
65
53
name : "aria-labelledby" . to_string ( ) ,
66
- value : Some ( AttributeValue :: Str ( "titleId" . to_string ( ) ) ) ,
54
+ value : Some ( "titleId" . to_string ( ) ) ,
67
55
literal : true ,
68
56
..Default :: default ( )
69
57
} ) ;
70
58
}
71
59
72
- let desc_prop = match config. desc_prop {
73
- Some ( r) => r,
74
- None => false
75
- } ;
60
+ let desc_prop = config. desc_prop . unwrap_or ( false ) ;
76
61
if desc_prop {
77
62
attributes. push ( Attribute {
78
63
name : "aria-describedby" . to_string ( ) ,
79
- value : Some ( AttributeValue :: Str ( "descId" . to_string ( ) ) ) ,
64
+ value : Some ( "descId" . to_string ( ) ) ,
80
65
literal : true ,
81
66
..Default :: default ( )
82
67
} ) ;
@@ -131,20 +116,20 @@ impl VisitMut for Visitor {
131
116
None => & AttributePosition :: End ,
132
117
} ;
133
118
134
- let new_attr = get_attr ( * spread, & name, value. as_ref ( ) , * literal) ;
119
+ let new_attr = get_attr ( * spread, name, value. as_ref ( ) , * literal) ;
135
120
136
121
let is_equal_attr = |attr : & JSXAttrOrSpread | -> bool {
137
122
if * spread {
138
123
if let JSXAttrOrSpread :: SpreadElement ( spread) = attr {
139
124
if let Expr :: Ident ( ident) = spread. expr . as_ref ( ) {
140
- return ident. sym . to_string ( ) == * name
125
+ return ident. sym == * name
141
126
}
142
127
}
143
128
false
144
129
} else {
145
130
if let JSXAttrOrSpread :: JSXAttr ( attr) = attr {
146
131
if let JSXAttrName :: Ident ( ident) = & attr. name {
147
- return ident. sym . to_string ( ) == * name
132
+ return ident. sym == * name
148
133
}
149
134
}
150
135
false
@@ -173,7 +158,7 @@ impl VisitMut for Visitor {
173
158
}
174
159
}
175
160
176
- fn get_attr ( spread : bool , name : & str , value : Option < & AttributeValue > , literal : bool ) -> JSXAttrOrSpread {
161
+ fn get_attr ( spread : bool , name : & str , value : Option < & String > , literal : bool ) -> JSXAttrOrSpread {
177
162
if spread {
178
163
JSXAttrOrSpread :: SpreadElement ( SpreadElement {
179
164
dot3_token : DUMMY_SP ,
@@ -197,47 +182,24 @@ fn get_attr(spread: bool, name: &str, value: Option<&AttributeValue>, literal: b
197
182
}
198
183
}
199
184
200
- fn get_attr_value ( literal : bool , attr_value : Option < & AttributeValue > ) -> Option < JSXAttrValue > {
185
+ fn get_attr_value ( literal : bool , attr_value : Option < & String > ) -> Option < JSXAttrValue > {
201
186
match attr_value {
202
187
Some ( value) => {
203
- match value {
204
- AttributeValue :: Bool ( value) => {
205
- Some ( JSXAttrValue :: JSXExprContainer ( JSXExprContainer {
206
- expr : JSXExpr :: Expr ( Box :: new ( Expr :: Lit ( Lit :: Bool ( Bool {
207
- span : DUMMY_SP ,
208
- value : value. clone ( ) ,
209
- } ) ) ) ) ,
210
- span : DUMMY_SP ,
211
- } ) )
212
- } ,
213
- AttributeValue :: Num ( value) => {
214
- Some ( JSXAttrValue :: JSXExprContainer ( JSXExprContainer {
215
- expr : JSXExpr :: Expr ( Box :: new ( Expr :: Lit ( Lit :: Num ( Number {
216
- span : DUMMY_SP ,
217
- value : value. clone ( ) ,
218
- raw : None ,
219
- } ) ) ) ) ,
188
+ if literal {
189
+ Some ( JSXAttrValue :: JSXExprContainer ( JSXExprContainer {
190
+ span : DUMMY_SP ,
191
+ expr : JSXExpr :: Expr ( Box :: new ( Expr :: Ident ( Ident {
192
+ sym : value. to_string ( ) . into ( ) ,
220
193
span : DUMMY_SP ,
221
- } ) )
222
- } ,
223
- AttributeValue :: Str ( value) => {
224
- if literal {
225
- Some ( JSXAttrValue :: JSXExprContainer ( JSXExprContainer {
226
- span : DUMMY_SP ,
227
- expr : JSXExpr :: Expr ( Box :: new ( Expr :: Ident ( Ident {
228
- sym : value. to_string ( ) . into ( ) ,
229
- span : DUMMY_SP ,
230
- optional : false ,
231
- } ) ) ) ,
232
- } ) )
233
- } else {
234
- Some ( JSXAttrValue :: Lit ( Lit :: Str ( Str {
235
- span : DUMMY_SP ,
236
- value : value. to_string ( ) . into ( ) ,
237
- raw : None ,
238
- } ) ) )
239
- }
240
- } ,
194
+ optional : false ,
195
+ } ) ) ) ,
196
+ } ) )
197
+ } else {
198
+ Some ( JSXAttrValue :: Lit ( Lit :: Str ( Str {
199
+ span : DUMMY_SP ,
200
+ value : value. to_string ( ) . into ( ) ,
201
+ raw : None ,
202
+ } ) ) )
241
203
}
242
204
} ,
243
205
None => None ,
@@ -253,7 +215,7 @@ fn svg_prop_to_attr(key: &str, value: &str) -> Attribute {
253
215
} ;
254
216
Attribute {
255
217
name : key. to_string ( ) ,
256
- value : Some ( AttributeValue :: Str ( str. to_string ( ) ) ) ,
218
+ value : Some ( str. to_string ( ) ) ,
257
219
literal,
258
220
..Default :: default ( )
259
221
}
@@ -336,20 +298,6 @@ mod tests {
336
298
337
299
#[ test]
338
300
fn should_add_attribute_with_value ( ) {
339
- code_test (
340
- r#"<div/>;"# ,
341
- Options {
342
- elements : vec ! [ "div" . to_string( ) ] ,
343
- attributes : vec ! [
344
- Attribute {
345
- name: "disabled" . to_string( ) ,
346
- value: Some ( AttributeValue :: Bool ( true ) ) ,
347
- ..Default :: default ( )
348
- }
349
- ] ,
350
- } ,
351
- r#"<div disabled={true}/>;"# ,
352
- ) ;
353
301
354
302
code_test (
355
303
r#"<div/>;"# ,
@@ -358,28 +306,13 @@ mod tests {
358
306
attributes : vec ! [
359
307
Attribute {
360
308
name: "disabled" . to_string( ) ,
361
- value: Some ( AttributeValue :: Str ( "true" . to_string( ) ) ) ,
309
+ value: Some ( "true" . to_string( ) ) ,
362
310
..Default :: default ( )
363
311
}
364
312
] ,
365
313
} ,
366
314
r#"<div disabled="true"/>;"# ,
367
315
) ;
368
-
369
- code_test (
370
- r#"<div/>;"# ,
371
- Options {
372
- elements : vec ! [ "div" . to_string( ) ] ,
373
- attributes : vec ! [
374
- Attribute {
375
- name: "disabled" . to_string( ) ,
376
- value: Some ( AttributeValue :: Num ( 200.0 ) ) ,
377
- ..Default :: default ( )
378
- }
379
- ] ,
380
- } ,
381
- r#"<div disabled={200}/>;"# ,
382
- ) ;
383
316
}
384
317
385
318
#[ test]
@@ -391,7 +324,7 @@ mod tests {
391
324
attributes : vec ! [
392
325
Attribute {
393
326
name: "ref" . to_string( ) ,
394
- value: Some ( AttributeValue :: Str ( "ref" . to_string( ) ) ) ,
327
+ value: Some ( "ref" . to_string( ) ) ,
395
328
literal: true ,
396
329
..Default :: default ( )
397
330
}
@@ -407,7 +340,7 @@ mod tests {
407
340
attributes : vec ! [
408
341
Attribute {
409
342
name: "ref" . to_string( ) ,
410
- value: Some ( AttributeValue :: Str ( "ref ? ref : null" . to_string( ) ) ) ,
343
+ value: Some ( "ref ? ref : null" . to_string( ) ) ,
411
344
literal: true ,
412
345
..Default :: default ( )
413
346
}
@@ -451,22 +384,4 @@ mod tests {
451
384
r#"<div><span foo="bar" {...props}/></div>;"# ,
452
385
) ;
453
386
}
454
-
455
- #[ test]
456
- fn should_replace_attribute ( ) {
457
- code_test (
458
- r#"<div disabled/>;"# ,
459
- Options {
460
- elements : vec ! [ "div" . to_string( ) ] ,
461
- attributes : vec ! [
462
- Attribute {
463
- name: "disabled" . to_string( ) ,
464
- value: Some ( AttributeValue :: Bool ( false ) ) ,
465
- ..Default :: default ( )
466
- }
467
- ] ,
468
- } ,
469
- r#"<div disabled={false}/>;"# ,
470
- ) ;
471
- }
472
387
}
0 commit comments