Skip to content

Commit 43b415d

Browse files
committed
chore: 优化代码
1 parent 644a0d3 commit 43b415d

File tree

6 files changed

+131
-97
lines changed

6 files changed

+131
-97
lines changed

src/document.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,20 @@ use std::collections::HashMap;
22

33
use selectors::attr::CaseSensitivity;
44
use swc_common::{
5+
comments::SingleThreadedComments,
56
errors::{ColorConfig, Handler},
67
sync::Lrc,
7-
SourceMap, Mark, comments::SingleThreadedComments, Globals, GLOBALS,
8+
Globals, Mark, SourceMap, GLOBALS,
89
};
910
use swc_ecma_ast::{EsVersion, Program};
1011
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax, TsConfig};
11-
use swc_ecma_visit::{VisitWith, FoldWith, VisitAllWith};
1212
use swc_ecma_transforms_base::{fixer::fixer, hygiene::hygiene, resolver};
13+
use swc_ecma_visit::{FoldWith, VisitAllWith, VisitWith};
1314
use swc_ecmascript::transforms::typescript::strip;
1415

1516
use crate::{
1617
scraper::Element,
17-
visitor::{AstVisitor, JSXRecord, CollectVisitor},
18+
visitor::{AstVisitor, CollectVisitor, JSXRecord},
1819
};
1920

2021
pub struct JSXDocument {
@@ -53,10 +54,10 @@ impl JSXDocument {
5354
e.into_diagnostic(&handler).emit();
5455
}
5556
let program = parser
56-
.parse_program()
57-
.map_err(|e| e.into_diagnostic(&handler).emit())
58-
.expect("解析 JSX 失败");
59-
57+
.parse_program()
58+
.map_err(|e| e.into_diagnostic(&handler).emit())
59+
.expect("解析 JSX 失败");
60+
6061
let globals = Globals::default();
6162
GLOBALS.set(&globals, || {
6263
let unresolved_mark = Mark::new();

src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use std::{cell::RefCell, rc::Rc};
44

5-
use swc_common::{comments::SingleThreadedComments, SourceMap, sync::Lrc};
5+
use swc_common::{comments::SingleThreadedComments, sync::Lrc, SourceMap};
66
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
77

88
use crate::{document::JSXDocument, style_parser::StyleParser, style_write::StyleWrite};
@@ -33,7 +33,12 @@ pub fn parse(component: String, styles: Vec<String>) -> String {
3333

3434
let program = Rc::new(RefCell::new(document.program.as_ref().unwrap().clone()));
3535
let jsx_record = Rc::new(RefCell::new(document.jsx_record.as_ref().unwrap().clone()));
36-
let mut style_write = StyleWrite::new(program.clone(), jsx_record.clone(), style_data.style_record.clone(), style_data.all_style.clone());
36+
let mut style_write = StyleWrite::new(
37+
program.clone(),
38+
jsx_record.clone(),
39+
style_data.style_record.clone(),
40+
style_data.all_style.clone(),
41+
);
3742
style_write.write();
3843

3944
// ast 转代码

src/scraper.rs

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
// inpired by https://github.com/causal-agent/scraper
22

33
use core::fmt;
4-
use std::{cell::OnceCell, collections::HashMap, ops::Deref, slice::Iter as SliceIter, collections::hash_map::Iter as HashMapIter};
4+
use std::{
5+
cell::OnceCell, collections::hash_map::Iter as HashMapIter, collections::HashMap, ops::Deref,
6+
slice::Iter as SliceIter,
7+
};
58

6-
use html5ever::{QualName, tendril::StrTendril, LocalName, Attribute};
9+
use html5ever::{tendril::StrTendril, Attribute, LocalName, QualName};
710
use selectors::attr::CaseSensitivity;
811

912
use crate::visitor::SpanKey;
1013

1114
pub struct Classes<'a> {
12-
inner: SliceIter<'a, LocalName>
15+
inner: SliceIter<'a, LocalName>,
1316
}
1417

1518
impl<'a> Iterator for Classes<'a> {
@@ -23,7 +26,7 @@ pub type AttributesIter<'a> = HashMapIter<'a, QualName, StrTendril>;
2326

2427
#[derive(Debug, Clone)]
2528
pub struct Attrs<'a> {
26-
inner: AttributesIter<'a>
29+
inner: AttributesIter<'a>,
2730
}
2831

2932
impl<'a> Iterator for Attrs<'a> {
@@ -41,7 +44,7 @@ pub struct Element {
4144
pub attrs: Attributes,
4245
pub span: SpanKey,
4346
id: OnceCell<Option<StrTendril>>,
44-
classes: OnceCell<Vec<LocalName>>
47+
classes: OnceCell<Vec<LocalName>>,
4548
}
4649

4750
impl Element {
@@ -55,7 +58,7 @@ impl Element {
5558
span,
5659
attrs,
5760
id: OnceCell::new(),
58-
classes: OnceCell::new()
61+
classes: OnceCell::new(),
5962
}
6063
}
6164

@@ -64,22 +67,28 @@ impl Element {
6467
}
6568

6669
pub fn id(&self) -> Option<&str> {
67-
self.id.get_or_init(|| {
68-
self.attrs
69-
.iter()
70-
.find(|(name,_ )| name.local.as_ref() == "id")
71-
.map(|(_, value)| value.clone())
72-
}).as_deref()
70+
self
71+
.id
72+
.get_or_init(|| {
73+
self
74+
.attrs
75+
.iter()
76+
.find(|(name, _)| name.local.as_ref() == "id")
77+
.map(|(_, value)| value.clone())
78+
})
79+
.as_deref()
7380
}
7481

7582
pub fn has_class(&self, class: &str, case_sensitive: CaseSensitivity) -> bool {
76-
self.classes()
83+
self
84+
.classes()
7785
.any(|class_name| case_sensitive.eq(class.as_bytes(), class_name.as_bytes()))
7886
}
7987

8088
pub fn classes(&self) -> Classes {
8189
let classes = self.classes.get_or_init(|| {
82-
let mut classes: Vec<LocalName> = self.attrs
90+
let mut classes: Vec<LocalName> = self
91+
.attrs
8392
.iter()
8493
.filter(|(name, _)| name.local.as_ref() == "className")
8594
.flat_map(|(_, value)| value.split_whitespace().map(LocalName::from))
@@ -89,11 +98,15 @@ impl Element {
8998
classes
9099
});
91100

92-
Classes { inner: classes.iter() }
101+
Classes {
102+
inner: classes.iter(),
103+
}
93104
}
94105

95106
pub fn attrs(&self) -> Attrs {
96-
Attrs { inner: self.attrs.iter() }
107+
Attrs {
108+
inner: self.attrs.iter(),
109+
}
97110
}
98111
}
99112

src/style_parser.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{cell::RefCell, collections::HashMap, convert::Infallible, rc::Rc, hash::Hash};
1+
use std::{cell::RefCell, collections::HashMap, convert::Infallible, hash::Hash, rc::Rc};
22

33
use lightningcss::{
44
declaration::DeclarationBlock,
@@ -96,11 +96,18 @@ impl<'i> StyleParser<'i> {
9696

9797
pub fn parse(&mut self, css: &'i str) {
9898
let mut stylesheet = StyleSheet::parse(css, ParserOptions::default()).expect("解析样式失败");
99-
let mut style_visitor = StyleVisitor::new(self.document, Rc::clone(&self.style_record), Rc::clone(&self.all_style));
99+
let mut style_visitor = StyleVisitor::new(
100+
self.document,
101+
Rc::clone(&self.style_record),
102+
Rc::clone(&self.all_style),
103+
);
100104
stylesheet.visit(&mut style_visitor).unwrap();
101105
}
102106

103-
fn calc_style_record<T: Hash + Eq + Clone>(&self, style_record: &mut HashMap<T, Vec<StyleDeclaration<'i>>>) -> HashMap<T, StyleDeclaration<'i>> {
107+
fn calc_style_record<T: Hash + Eq + Clone>(
108+
&self,
109+
style_record: &mut HashMap<T, Vec<StyleDeclaration<'i>>>,
110+
) -> HashMap<T, StyleDeclaration<'i>> {
104111
let mut final_style_record = HashMap::new();
105112
for (id, declarations) in style_record.iter_mut() {
106113
declarations.sort_by(|a, b| a.specificity.cmp(&b.specificity));

src/style_write.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ impl<'i> StyleWrite<'i> {
3131
}
3232

3333
pub fn write(&mut self) {
34-
let mut style_visitor = AstMutVisitor::new(self.jsx_record.clone(), self.style_record.clone(), self.all_style.clone());
34+
let mut style_visitor = AstMutVisitor::new(
35+
self.jsx_record.clone(),
36+
self.style_record.clone(),
37+
self.all_style.clone(),
38+
);
3539
self.module.borrow_mut().visit_mut_with(&mut style_visitor);
3640
}
3741
}

0 commit comments

Comments
 (0)