Skip to content

Commit 7f58d85

Browse files
committed
fix: rust lint error
1 parent d11e22d commit 7f58d85

File tree

11 files changed

+76
-209
lines changed

11 files changed

+76
-209
lines changed

crates/core/src/add_jsx_attribute.rs

Lines changed: 30 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@ use swc_core::{
77
};
88
use super::core;
99

10-
pub enum AttributeValue {
11-
Bool(bool),
12-
Num(f64),
13-
Str(String),
14-
}
15-
1610
pub enum AttributePosition {
1711
Start,
1812
End,
@@ -21,7 +15,7 @@ pub enum AttributePosition {
2115
#[derive(Default)]
2216
pub struct Attribute {
2317
pub name: String,
24-
pub value: Option<AttributeValue>,
18+
pub value: Option<String>,
2519
pub spread: bool,
2620
pub literal: bool,
2721
pub position: Option<AttributePosition>,
@@ -43,40 +37,31 @@ impl Visitor {
4337
}
4438
}
4539

46-
let _ref = match config._ref {
47-
Some(r) => r,
48-
None => false
49-
};
40+
let _ref = config._ref.unwrap_or(false);
5041
if _ref {
5142
attributes.push(Attribute {
5243
name: "ref".to_string(),
53-
value: Some(AttributeValue::Str("ref".to_string())),
44+
value: Some("ref".to_string()),
5445
literal: true,
5546
..Default::default()
5647
});
5748
}
5849

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);
6351
if title_prop {
6452
attributes.push(Attribute {
6553
name: "aria-labelledby".to_string(),
66-
value: Some(AttributeValue::Str("titleId".to_string())),
54+
value: Some("titleId".to_string()),
6755
literal: true,
6856
..Default::default()
6957
});
7058
}
7159

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);
7661
if desc_prop {
7762
attributes.push(Attribute {
7863
name: "aria-describedby".to_string(),
79-
value: Some(AttributeValue::Str("descId".to_string())),
64+
value: Some("descId".to_string()),
8065
literal: true,
8166
..Default::default()
8267
});
@@ -131,20 +116,20 @@ impl VisitMut for Visitor {
131116
None => &AttributePosition::End,
132117
};
133118

134-
let new_attr = get_attr(*spread, &name, value.as_ref(), *literal);
119+
let new_attr = get_attr(*spread, name, value.as_ref(), *literal);
135120

136121
let is_equal_attr = |attr: &JSXAttrOrSpread| -> bool {
137122
if *spread {
138123
if let JSXAttrOrSpread::SpreadElement(spread) = attr {
139124
if let Expr::Ident(ident) = spread.expr.as_ref() {
140-
return ident.sym.to_string() == *name
125+
return ident.sym == *name
141126
}
142127
}
143128
false
144129
} else {
145130
if let JSXAttrOrSpread::JSXAttr(attr) = attr {
146131
if let JSXAttrName::Ident(ident) = &attr.name {
147-
return ident.sym.to_string() == *name
132+
return ident.sym == *name
148133
}
149134
}
150135
false
@@ -173,7 +158,7 @@ impl VisitMut for Visitor {
173158
}
174159
}
175160

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 {
177162
if spread {
178163
JSXAttrOrSpread::SpreadElement(SpreadElement {
179164
dot3_token: DUMMY_SP,
@@ -197,47 +182,24 @@ fn get_attr(spread: bool, name: &str, value: Option<&AttributeValue>, literal: b
197182
}
198183
}
199184

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> {
201186
match attr_value {
202187
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(),
220193
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+
})))
241203
}
242204
},
243205
None => None,
@@ -253,7 +215,7 @@ fn svg_prop_to_attr(key: &str, value: &str) -> Attribute {
253215
};
254216
Attribute {
255217
name: key.to_string(),
256-
value: Some(AttributeValue::Str(str.to_string())),
218+
value: Some(str.to_string()),
257219
literal,
258220
..Default::default()
259221
}
@@ -336,20 +298,6 @@ mod tests {
336298

337299
#[test]
338300
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-
);
353301

354302
code_test(
355303
r#"<div/>;"#,
@@ -358,28 +306,13 @@ mod tests {
358306
attributes: vec![
359307
Attribute {
360308
name: "disabled".to_string(),
361-
value: Some(AttributeValue::Str("true".to_string())),
309+
value: Some("true".to_string()),
362310
..Default::default()
363311
}
364312
],
365313
},
366314
r#"<div disabled="true"/>;"#,
367315
);
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-
);
383316
}
384317

385318
#[test]
@@ -391,7 +324,7 @@ mod tests {
391324
attributes: vec![
392325
Attribute {
393326
name: "ref".to_string(),
394-
value: Some(AttributeValue::Str("ref".to_string())),
327+
value: Some("ref".to_string()),
395328
literal: true,
396329
..Default::default()
397330
}
@@ -407,7 +340,7 @@ mod tests {
407340
attributes: vec![
408341
Attribute {
409342
name: "ref".to_string(),
410-
value: Some(AttributeValue::Str("ref ? ref : null".to_string())),
343+
value: Some("ref ? ref : null".to_string()),
411344
literal: true,
412345
..Default::default()
413346
}
@@ -451,22 +384,4 @@ mod tests {
451384
r#"<div><span foo="bar" {...props}/></div>;"#,
452385
);
453386
}
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-
}
472387
}

crates/core/src/core/config.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,17 @@ mod strings {
6565
named_unit_variant!(automatic);
6666
}
6767

68-
#[derive(Debug, Clone, Serialize, Deserialize)]
68+
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
6969
#[serde(untagged)]
7070
pub enum ExpandProps {
7171
Bool(bool),
7272
#[serde(with = "strings::start")]
7373
Start,
7474
#[serde(with = "strings::end")]
75+
#[default]
7576
End
7677
}
7778

78-
impl Default for ExpandProps {
79-
fn default() -> Self {
80-
ExpandProps::End
81-
}
82-
}
83-
8479
#[derive(Debug, Clone, Serialize, Deserialize)]
8580
#[serde(untagged)]
8681
pub enum JSXRuntime {

crates/core/src/hast_to_swc_ast/decode_xml.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,10 @@ pub fn decode_xml(s: &str) -> String {
7070
}
7171

7272
// &amp;
73-
if bytes.len() - cur_idx > 5 {
74-
if s[cur_idx..cur_idx + 4].to_string() == "amp;" {
75-
ret.push('&');
76-
cur_idx += 4;
77-
last_idx = cur_idx;
78-
}
73+
if bytes.len() - cur_idx > 5 && s[cur_idx..cur_idx + 4].to_string() == "amp;" {
74+
ret.push('&');
75+
cur_idx += 4;
76+
last_idx = cur_idx;
7977
}
8078

8179
// &apos;

0 commit comments

Comments
 (0)