Skip to content

Commit

Permalink
format code with rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed Apr 18, 2024
1 parent 66bd99c commit f858c5c
Show file tree
Hide file tree
Showing 22 changed files with 1,438 additions and 1,489 deletions.
28 changes: 14 additions & 14 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,20 @@ jobs:
with:
command: test

# fmt:
# name: Rustfmt
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v2
# - uses: actions-rs/toolchain@v1
# with:
# profile: minimal
# - uses: Swatinem/rust-cache@v2
# - run: rustup component add rustfmt
# - uses: actions-rs/cargo@v1
# with:
# command: fmt
# args: --all -- --check
fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
- uses: Swatinem/rust-cache@v2
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

# clippy:
# name: Clippy
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tab_spaces = 2
198 changes: 98 additions & 100 deletions src/ast_utils.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,48 @@
use std::collections::HashSet;
use swc_core::common::DUMMY_SP;
use swc_core::ecma::ast::{*};
use swc_core::ecma::ast::*;
use swc_core::ecma::atoms::JsWord;
use swc_core::ecma::utils::quote_ident;

pub fn get_jsx_attr<'a>(el: &'a JSXOpeningElement, name: &str) -> Option<&'a JSXAttr> {
for attr in &el.attrs {
if let JSXAttrOrSpread::JSXAttr(attr) = &attr {
if let JSXAttrName::Ident(ident) = &attr.name {
if (&ident.sym) == name {
return Some(attr);
}
}
for attr in &el.attrs {
if let JSXAttrOrSpread::JSXAttr(attr) = &attr {
if let JSXAttrName::Ident(ident) = &attr.name {
if (&ident.sym) == name {
return Some(attr);
}
}
}
}

return None;
return None;
}

pub fn get_jsx_attr_value_as_string(val: &JSXAttrValue) -> Option<String> {
match val {
// offset="5"
JSXAttrValue::Lit(Lit::Str(Str { value, .. })) => {
return Some(value.to_string());
match val {
// offset="5"
JSXAttrValue::Lit(Lit::Str(Str { value, .. })) => {
return Some(value.to_string());
}
// offset={..}
JSXAttrValue::JSXExprContainer(JSXExprContainer {
expr: JSXExpr::Expr(expr),
..
}) => {
match expr.as_ref() {
// offset={"5"}
Expr::Lit(Lit::Str(Str { value, .. })) => {
return Some(value.to_string());
}
// offset={..}
JSXAttrValue::JSXExprContainer(JSXExprContainer { expr: JSXExpr::Expr(expr), .. }) => {
match expr.as_ref() {
// offset={"5"}
Expr::Lit(Lit::Str(Str { value, .. })) => {
return Some(value.to_string());
}
// offset={5}
Expr::Lit(Lit::Num(Number { value, .. })) => {
return Some(value.to_string());
}
_ => None
}
// offset={5}
Expr::Lit(Lit::Num(Number { value, .. })) => {
return Some(value.to_string());
}
_ => None
_ => None,
}
}
_ => None,
}
}

pub fn get_expr_as_string(val: &Box<Expr>) -> Option<String> {
Expand All @@ -50,58 +53,62 @@ pub fn get_expr_as_string(val: &Box<Expr>) -> Option<String> {
}

// `Hello`
Expr::Tpl(Tpl {quasis, ..}) => {
Expr::Tpl(Tpl { quasis, .. }) => {
if quasis.len() == 1 {
return Some(quasis.get(0).unwrap().raw.to_string());
} else { None }
} else {
None
}
}

_ => None
_ => None,
}
}

pub fn pick_jsx_attrs(mut attrs: Vec<JSXAttrOrSpread>, names: HashSet<&str>) -> Vec<JSXAttrOrSpread> {
attrs.retain(|attr| {
if let JSXAttrOrSpread::JSXAttr(attr) = attr {
if let JSXAttrName::Ident(ident) = &attr.name {
let name: &str = &ident.sym.to_string();
if let Some(_) = names.get(name) {
return true;
}
}
pub fn pick_jsx_attrs(
mut attrs: Vec<JSXAttrOrSpread>,
names: HashSet<&str>,
) -> Vec<JSXAttrOrSpread> {
attrs.retain(|attr| {
if let JSXAttrOrSpread::JSXAttr(attr) = attr {
if let JSXAttrName::Ident(ident) = &attr.name {
let name: &str = &ident.sym.to_string();
if let Some(_) = names.get(name) {
return true;
}
return false;
});
}
}
return false;
});

attrs
attrs
}


pub fn create_jsx_attribute(name: &str, exp: Box<Expr>) -> JSXAttrOrSpread {
JSXAttrOrSpread::JSXAttr(JSXAttr {
span: DUMMY_SP,
name: JSXAttrName::Ident(Ident {
span: DUMMY_SP,
sym: name.into(),
optional: false,
}),
value: Some(JSXAttrValue::JSXExprContainer(JSXExprContainer {
span: DUMMY_SP,
expr: JSXExpr::Expr(exp),
})),
})
JSXAttrOrSpread::JSXAttr(JSXAttr {
span: DUMMY_SP,
name: JSXAttrName::Ident(Ident {
span: DUMMY_SP,
sym: name.into(),
optional: false,
}),
value: Some(JSXAttrValue::JSXExprContainer(JSXExprContainer {
span: DUMMY_SP,
expr: JSXExpr::Expr(exp),
})),
})
}

pub fn match_callee_name<F: Fn(&Ident) -> bool>(call: &CallExpr, predicate: F) -> Option<&Ident> {
if let Callee::Expr(expr) = &call.callee {
if let Expr::Ident(ident) = expr.as_ref() {
if predicate(&ident) {
return Some(ident);
}
}
if let Callee::Expr(expr) = &call.callee {
if let Expr::Ident(ident) = expr.as_ref() {
if predicate(&ident) {
return Some(ident);
}
}
}

None
None
}

pub fn to_key_value_prop(prop_or_spread: &PropOrSpread) -> Option<&KeyValueProp> {
Expand All @@ -115,55 +122,46 @@ pub fn to_key_value_prop(prop_or_spread: &PropOrSpread) -> Option<&KeyValueProp>
}

pub fn get_object_prop<'a>(props: &'a Vec<PropOrSpread>, name: &str) -> Option<&'a KeyValueProp> {
props.iter()
props
.iter()
.filter_map(|prop_or_spread| to_key_value_prop(prop_or_spread))
.find(|prop| {
get_prop_key(prop)
.and_then(|key| {
if key == name { Some(key) } else { None }
}).is_some()
.and_then(|key| if key == name { Some(key) } else { None })
.is_some()
})
}

pub fn get_prop_key(prop: &KeyValueProp) -> Option<&JsWord> {
match &prop.key {
PropName::Ident(Ident { sym, .. })
| PropName::Str(Str { value: sym, .. }) => {
Some(sym)
}
_ => {
None
}
}
match &prop.key {
PropName::Ident(Ident { sym, .. }) | PropName::Str(Str { value: sym, .. }) => Some(sym),
_ => None,
}
}

pub fn create_key_value_prop(key: &str, value: Box<Expr>) -> PropOrSpread {
return PropOrSpread::Prop(Box::new(Prop::KeyValue(
KeyValueProp {
key: PropName::Ident(quote_ident!(key)),
value,
}
)));
return PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp {
key: PropName::Ident(quote_ident!(key)),
value,
})));
}

pub fn create_import(source: JsWord, specifier: Ident) -> ModuleItem {
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
specifiers: vec![
ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: specifier,
imported: None,
is_type_only: false,
})
],
src: Box::new(Str {
span: DUMMY_SP,
value: source,
raw: None,
}),
with: None,
type_only: false,
phase: Default::default(),
}))
ModuleItem::ModuleDecl(ModuleDecl::Import(ImportDecl {
span: DUMMY_SP,
specifiers: vec![ImportSpecifier::Named(ImportNamedSpecifier {
span: DUMMY_SP,
local: specifier,
imported: None,
is_type_only: false,
})],
src: Box::new(Str {
span: DUMMY_SP,
value: source,
raw: None,
}),
with: None,
type_only: false,
phase: Default::default(),
}))
}
Loading

0 comments on commit f858c5c

Please sign in to comment.