Skip to content

Commit c35cbdf

Browse files
committed
feat: 支持将样式节点记录写入 ast 中
1 parent 73a5bcb commit c35cbdf

File tree

6 files changed

+185
-45
lines changed

6 files changed

+185
-45
lines changed

Cargo.lock

Lines changed: 43 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ once_cell = "1.18.0"
1414
selectors = "0.25.0"
1515
smallvec = "1.11.0"
1616
style = "0.1.0"
17-
swc_common = {version = "0.32.1", features = ["tty-emitter", "sourcemap"]}
18-
swc_ecma_ast = {version = "0.109.1"}
19-
swc_ecma_parser = "0.139.1"
20-
swc_ecma_visit = "0.95.1"
17+
swc_common = {version = "0.33.0", features = ["tty-emitter", "sourcemap"]}
18+
swc_ecma_ast = {version = "0.110.0"}
19+
swc_ecma_codegen = "0.146.0"
20+
swc_ecma_parser = "0.141.0"
21+
swc_ecma_visit = "0.96.0"

asset/test.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.mod {
2+
padding-top:29px;
3+
padding-bottom:29px;
4+
}

src/document.rs

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1+
use std::{collections::HashMap, rc};
2+
13
use ego_tree::Tree;
2-
use swc_common::{sync::Lrc, SourceMap, errors::{Handler, ColorConfig}};
3-
use swc_ecma_ast::EsVersion;
4+
use swc_common::{sync::Lrc, SourceMap, errors::{Handler, ColorConfig}, comments::SingleThreadedComments};
5+
use swc_ecma_ast::{EsVersion, Module};
6+
use swc_ecma_codegen::{text_writer::JsWriter, Emitter};
47
use swc_ecma_parser::{lexer::Lexer, Syntax, TsConfig, StringInput, Parser};
5-
use swc_ecma_visit::VisitWith;
8+
use swc_ecma_visit::{VisitWith, VisitMutWith};
69

7-
use crate::{scraper::{Node, Selector, ElementRef}, visitor::AstVisitor};
10+
use crate::{scraper::{Node, Selector, ElementRef}, visitor::{AstVisitor, JSXRecord, AstMutVisitor}};
811

912
pub struct JSXDocument {
10-
pub tree: Tree<Node>
13+
pub tree: Tree<Node>,
14+
pub module: Option<Module>,
1115
}
1216

1317
impl JSXDocument {
1418
pub fn new() -> Self {
15-
JSXDocument { tree: Tree::new(Node::Document) }
19+
JSXDocument {
20+
tree: Tree::new(Node::Document),
21+
module: None,
22+
}
1623
}
1724

1825
pub fn parse(&mut self, jsx: String) {
@@ -50,15 +57,36 @@ impl JSXDocument {
5057
e.into_diagnostic(&handler).emit();
5158
}
5259

53-
let module = parser
60+
let mut module = parser
5461
.parse_module()
5562
.map_err(|e| {
5663
e.into_diagnostic(&handler).emit()
5764
})
5865
.expect("failed to parser module");
59-
60-
let mut vistor = AstVisitor::new(&module, &mut self.tree);
66+
let mut jsx_record: JSXRecord = HashMap::new();
67+
let mut vistor = AstVisitor::new(&module, &mut self.tree, &mut jsx_record);
6168
module.visit_with(&mut vistor);
69+
let mut mut_visitor = AstMutVisitor::new( &mut jsx_record);
70+
module.visit_mut_with(&mut mut_visitor);
71+
// // ast 转代码
72+
// let cm = rc::Rc::new(SourceMap::default());
73+
// let handler = Handler::with_tty_emitter(ColorConfig::Auto, true, false, Some(cm.clone()));
74+
// let comments = SingleThreadedComments::default();
75+
76+
// let mut buf = Vec::new();
77+
// {
78+
// let writer = Box::new(JsWriter::new(cm.clone(), "\n", &mut buf, None));
79+
// let mut emitter = Emitter {
80+
// cfg: Default::default(),
81+
// cm: cm.clone(),
82+
// wr: writer,
83+
// comments: Some(&comments),
84+
// };
85+
// emitter.emit_module(&module).unwrap();
86+
// }
87+
// let code = String::from_utf8(buf).unwrap();
88+
// println!("{}", code);
89+
self.module = Some(module);
6290
}
6391

6492
pub fn select<'a>(&self, selector: &'a Selector) -> Vec<ElementRef> {

src/utils.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::collections::HashSet;
1+
use std::{collections::{HashSet, hash_map::DefaultHasher}, hash::{Hash, Hasher}};
22

33
use html5ever::{QualName, ns, LocalName, namespace_url};
44
use lightningcss::properties::PropertyId;
@@ -95,3 +95,9 @@ pub fn is_style_inheritable(style: PropertyId<'_>) -> bool {
9595
pub fn is_starts_with_uppercase(str: &str) -> bool {
9696
str.chars().next().unwrap().is_uppercase()
9797
}
98+
99+
pub fn calculate_hash<T: Hash>(t: &T) -> u64 {
100+
let mut hasher = DefaultHasher::new();
101+
t.hash(&mut hasher);
102+
hasher.finish()
103+
}

0 commit comments

Comments
 (0)