Skip to content

Commit

Permalink
fix: rust lint error
Browse files Browse the repository at this point in the history
  • Loading branch information
SyMind committed Jan 3, 2024
1 parent d11e22d commit 023aab1
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 110 deletions.
25 changes: 8 additions & 17 deletions crates/core/src/add_jsx_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,7 @@ impl Visitor {
}
}

let _ref = match config._ref {
Some(r) => r,
None => false
};
let _ref = config._ref.unwrap_or(false);
if _ref {
attributes.push(Attribute {
name: "ref".to_string(),
Expand All @@ -56,10 +53,7 @@ impl Visitor {
});
}

let title_prop = match config.title_prop {
Some(r) => r,
None => false
};
let title_prop = config.title_prop.unwrap_or(false);
if title_prop {
attributes.push(Attribute {
name: "aria-labelledby".to_string(),
Expand All @@ -69,10 +63,7 @@ impl Visitor {
});
}

let desc_prop = match config.desc_prop {
Some(r) => r,
None => false
};
let desc_prop = config.desc_prop.unwrap_or(false);
if desc_prop {
attributes.push(Attribute {
name: "aria-describedby".to_string(),
Expand Down Expand Up @@ -131,20 +122,20 @@ impl VisitMut for Visitor {
None => &AttributePosition::End,
};

let new_attr = get_attr(*spread, &name, value.as_ref(), *literal);
let new_attr = get_attr(*spread, name, value.as_ref(), *literal);

let is_equal_attr = |attr: &JSXAttrOrSpread| -> bool {
if *spread {
if let JSXAttrOrSpread::SpreadElement(spread) = attr {
if let Expr::Ident(ident) = spread.expr.as_ref() {
return ident.sym.to_string() == *name
return ident.sym == *name
}
}
false
} else {
if let JSXAttrOrSpread::JSXAttr(attr) = attr {
if let JSXAttrName::Ident(ident) = &attr.name {
return ident.sym.to_string() == *name
return ident.sym == *name
}
}
false
Expand Down Expand Up @@ -205,7 +196,7 @@ fn get_attr_value(literal: bool, attr_value: Option<&AttributeValue>) -> Option<
Some(JSXAttrValue::JSXExprContainer(JSXExprContainer {
expr: JSXExpr::Expr(Box::new(Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value: value.clone(),
value: *value,
})))),
span: DUMMY_SP,
}))
Expand All @@ -214,7 +205,7 @@ fn get_attr_value(literal: bool, attr_value: Option<&AttributeValue>) -> Option<
Some(JSXAttrValue::JSXExprContainer(JSXExprContainer {
expr: JSXExpr::Expr(Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: value.clone(),
value: *value,
raw: None,
})))),
span: DUMMY_SP,
Expand Down
9 changes: 2 additions & 7 deletions crates/core/src/core/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,17 @@ mod strings {
named_unit_variant!(automatic);
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
#[serde(untagged)]
pub enum ExpandProps {
Bool(bool),
#[serde(with = "strings::start")]
Start,
#[serde(with = "strings::end")]
#[default]
End
}

impl Default for ExpandProps {
fn default() -> Self {
ExpandProps::End
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum JSXRuntime {
Expand Down
10 changes: 4 additions & 6 deletions crates/core/src/hast_to_swc_ast/decode_xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,10 @@ pub fn decode_xml(s: &str) -> String {
}

// &amp;
if bytes.len() - cur_idx > 5 {
if s[cur_idx..cur_idx + 4].to_string() == "amp;" {
ret.push('&');
cur_idx += 4;
last_idx = cur_idx;
}
if bytes.len() - cur_idx > 5 && s[cur_idx..cur_idx + 4].to_string() == "amp;" {
ret.push('&');
cur_idx += 4;
last_idx = cur_idx;
}

// &apos;
Expand Down
32 changes: 12 additions & 20 deletions crates/core/src/hast_to_swc_ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_core::{
common::DUMMY_SP,
ecma::{ast::*, atoms::JsWord}
};
use swc_xml::{visit::{Visit, VisitWith}};
use swc_xml::visit::{Visit, VisitWith};
use regex::{Regex, Captures};

mod decode_xml;
Expand Down Expand Up @@ -55,7 +55,7 @@ fn get_value(attr_name: &str, value: &JsWord) -> JSXAttrValue {
})
}

return JSXAttrValue::Lit(Lit::Str(Str {
JSXAttrValue::Lit(Lit::Str(Str {
span: DUMMY_SP,
value: replace_spaces(value).into(),
raw: None
Expand Down Expand Up @@ -100,11 +100,7 @@ impl HastVisitor {
fn element(&self, n: &swc_xml::ast::Element) -> JSXElement {
let attrs = n.attributes.iter().map(
|attr| {
let value = match attr.value.clone() {
Some(v) => Some(get_value(&attr.name, &v)),
None => None,
};

let value = attr.value.clone().map(|v| get_value(&attr.name, &v));
JSXAttrOrSpread::JSXAttr(JSXAttr {
span: DUMMY_SP,
name: JSXAttrName::Ident(self.get_key(&attr.name, &n.tag_name)),
Expand All @@ -120,11 +116,11 @@ impl HastVisitor {
span: DUMMY_SP,
name: name.clone(),
attrs,
self_closing: children.len() == 0,
self_closing: children.is_empty(),
type_args: None,
};

let closing = if children.len() > 0 {
let closing = if children.is_empty() {
Some(JSXClosingElement {
span: DUMMY_SP,
name,
Expand All @@ -141,17 +137,15 @@ impl HastVisitor {
}
}

fn all(&self, children: &Vec<swc_xml::ast::Child>) -> Vec<JSXElementChild> {
children.into_iter()
.map(|n| {
fn all(&self, children: &[swc_xml::ast::Child]) -> Vec<JSXElementChild> {
children.iter()
.filter_map(|n| {
match n {
swc_xml::ast::Child::Element(e) => Some(JSXElementChild::JSXElement(Box::new(self.element(&e)))),
swc_xml::ast::Child::Element(e) => Some(JSXElementChild::JSXElement(Box::new(self.element(e)))),
swc_xml::ast::Child::Text(t) => text(t),
_ => None,
}
})
.filter(|n| n.is_some())
.map(|n| n.unwrap())
.collect()
}

Expand Down Expand Up @@ -194,7 +188,7 @@ impl HastVisitor {
}
}

let kebab_key = kebab_case(&attr_name);
let kebab_key = kebab_case(attr_name);

if kebab_key.starts_with("aria-") {
return Ident {
Expand Down Expand Up @@ -248,9 +242,7 @@ mod tests {
let mut errors = vec![];
let doc = parse_file_as_document(
fm.borrow(),
parser::ParserConfig {
..Default::default()
},
Default::default(),
&mut errors
).unwrap();

Expand Down Expand Up @@ -290,7 +282,7 @@ mod tests {

let res = transform(cm, fm, false);

NormalizedOutput::from(res).compare_to_file(&jsx_path).unwrap();
NormalizedOutput::from(res).compare_to_file(jsx_path).unwrap();
}

fn code_test(input: &str, expected: &str) {
Expand Down
6 changes: 2 additions & 4 deletions crates/core/src/hast_to_swc_ast/string_to_object_style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ pub fn string_to_object_style(raw_style: &str) -> Expr {
let entries = raw_style.split(';');

let properties = entries.into_iter()
.map(|entry| {
.filter_map(|entry| {
let style = entry.trim();
if style.len() == 0 {
if style.is_empty() {
return None;
}

Expand All @@ -89,8 +89,6 @@ pub fn string_to_object_style(raw_style: &str) -> Expr {
None => None,
}
})
.filter(|p| p.is_some())
.map(|p| p.unwrap())
.collect::<Vec<PropOrSpread>>();

Expr::Object(ObjectLit {
Expand Down
14 changes: 4 additions & 10 deletions crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
extern crate napi_derive;

use std::{sync::Arc, borrow::Borrow};
use swc_xml::{parser::{parse_file_as_document, parser}};
use swc_xml::parser::parse_file_as_document;
use swc_core::{
common::{SourceMap, FileName, comments::SingleThreadedComments},
ecma::{
Expand Down Expand Up @@ -50,14 +50,12 @@ pub fn transform(code: String, config: Config, state: State) -> Result<String, S
let state = core::state::expand_state(&state);

let cm = Arc::<SourceMap>::default();
let fm = cm.new_source_file(FileName::Anon, code.to_string());
let fm = cm.new_source_file(FileName::Anon, code);

let mut errors = vec![];
let document = parse_file_as_document(
fm.borrow(),
parser::ParserConfig{
..Default::default()
},
Default::default(),
&mut errors
).unwrap();

Expand All @@ -67,11 +65,7 @@ pub fn transform(code: String, config: Config, state: State) -> Result<String, S
}
let jsx_element = jsx_element.unwrap();

let m = transform_svg_component::transform(jsx_element, &config, &state);
if m.is_err() {
return Err(m.unwrap_err());
}
let m = m.unwrap();
let m = transform_svg_component::transform(jsx_element, &config, &state)?;

let m = m.fold_with(&mut as_folder(remove_jsx_attribute::Visitor::new(&config)));
let m = m.fold_with(&mut as_folder(add_jsx_attribute::Visitor::new(&config)));
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/replace_jsx_attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl VisitMut for Visitor {
let old_value = str.value.to_string();

if self.values.contains_key(&old_value) {
let attr_value = get_attr_value(&self.values.get(&old_value).unwrap());
let attr_value = get_attr_value(self.values.get(&old_value).unwrap());
jsx_attr.value = Some(attr_value);
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/svg_dynamic_title.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ impl Visitor {
jsx_attr_value
};

if existing_title.children.len() == 0 {
if existing_title.children.is_empty() {
return JSXElementChild::JSXExprContainer(JSXExprContainer {
span: DUMMY_SP,
expr: JSXExpr::Expr(Box::new(self.get_tag_expr(id_attr_value))),
Expand Down Expand Up @@ -213,7 +213,7 @@ impl VisitMut for Visitor {
let has_tag = n.children.clone().iter_mut().enumerate().any(|(i, c)| {
if let JSXElementChild::JSXElement(e) = c {
if let JSXElementName::Ident(ident) = &e.opening.name {
if ident.sym.to_string() == self.tag {
if ident.sym == self.tag {
let tag_element = self.get_tag_element_with_existing_title(e);
n.children[i] = tag_element;
return true
Expand Down
4 changes: 2 additions & 2 deletions crates/core/src/svg_em_dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl Visitor {
match icon {
core::config::Icon::Str(s) => {
height = Some(Size::Str(s.clone()));
width = Some(Size::Str(s.clone()));
width = Some(Size::Str(s));
},
core::config::Icon::Num(n) => {
height = Some(Size::Num(n));
Expand Down Expand Up @@ -125,7 +125,7 @@ fn get_value(raw: Option<&Size>) -> JSXAttrValue {
JSXAttrValue::JSXExprContainer(JSXExprContainer {
expr: JSXExpr::Expr(Box::new(Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value: num.clone(),
value: *num,
raw: None,
})))),
span: DUMMY_SP,
Expand Down
3 changes: 1 addition & 2 deletions crates/core/src/transform_react_native_svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ impl VisitMut for SvgElementVisitor {
}
}
n.visit_mut_with(&mut jsx_element_visitor);
return;
}
}
}
Expand Down Expand Up @@ -197,7 +196,7 @@ impl VisitMut for ImportDeclVisitor {
for component in self.replaced_components.borrow().iter() {
if n.specifiers.iter().any(|specifier| {
if let ImportSpecifier::Named(named) = specifier {
if named.local.sym.to_string() == component.to_string() {
if named.local.sym == *component {
return true;
}
}
Expand Down
Loading

0 comments on commit 023aab1

Please sign in to comment.