Skip to content

Commit b3f95fa

Browse files
committed
refactor(compiler-rs): use workspace
1 parent 2e30262 commit b3f95fa

File tree

257 files changed

+1572
-1561
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

257 files changed

+1572
-1561
lines changed

packages/compiler-rs/Cargo.toml

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
[package]
32
authors = ["zhiyuanzmj <260480378@qq.com>"]
43
edition = "2024"
@@ -12,9 +11,14 @@ crate-type = ["cdylib", "rlib"]
1211
default = ["napi"]
1312
napi = []
1413

15-
[dependencies]
14+
[workspace]
15+
resolver = "3"
16+
members = ["crates/*"]
17+
18+
[workspace.dependencies]
1619
napi = { version = "3.3.0", features = ["napi9"] }
1720
napi-derive = "3.3.0"
21+
1822
oxc_parser = "0.99.0"
1923
oxc_ast = "0.99.0"
2024
oxc_allocator = "0.99.0"
@@ -26,6 +30,25 @@ oxc_codegen = "0.99.0"
2630
phf = "0.13.1"
2731
indexmap = "2.12.0"
2832

33+
vdom = { path = "crates/vdom" }
34+
vapor = { path = "crates/vapor" }
35+
common = { path = "crates/common" }
36+
37+
[dependencies]
38+
napi = { workspace = true }
39+
napi-derive = { workspace = true }
40+
41+
oxc_parser = { workspace = true }
42+
oxc_ast = { workspace = true }
43+
oxc_allocator = { workspace = true }
44+
oxc_span = { workspace = true }
45+
oxc_traverse = { workspace = true }
46+
oxc_semantic = { workspace = true }
47+
oxc_codegen = { workspace = true }
48+
49+
common = { workspace = true }
50+
vapor = { workspace = true }
51+
2952
[dev-dependencies]
3053
insta = "1.43.2"
3154
criterion = "0.7.0"

packages/compiler-rs/benches/bench.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use compiler_rs::transform::transform;
1+
use compiler_rs::transform;
22
use criterion::{Criterion, criterion_group, criterion_main};
33

44
fn bench_compile(b: &mut Criterion) {

packages/compiler-rs/compiler-rs.wasi-browser.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,5 @@ const {
5656
},
5757
})
5858
export default __napiModule.exports
59-
export const compile = __napiModule.exports.compile
6059
export const ErrorCodes = __napiModule.exports.ErrorCodes
6160
export const transform = __napiModule.exports.transform

packages/compiler-rs/compiler-rs.wasi.cjs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,5 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
108108
},
109109
})
110110
module.exports = __napiModule.exports
111-
module.exports.compile = __napiModule.exports.compile
112111
module.exports.ErrorCodes = __napiModule.exports.ErrorCodes
113112
module.exports.transform = __napiModule.exports.transform
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
[package]
2+
name = "common"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[features]
7+
default = ["napi"]
8+
napi = []
9+
10+
[dependencies]
11+
oxc_span = { workspace = true }
12+
oxc_ast = { workspace = true }
13+
oxc_traverse = { workspace = true }
14+
oxc_allocator = { workspace = true }
15+
oxc_semantic = { workspace = true }
16+
17+
napi-derive = { workspace = true }
18+
napi = { workspace = true }
19+
phf = { workspace = true }

packages/compiler-rs/src/utils/check.rs renamed to packages/compiler-rs/crates/common/src/check.rs

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use oxc_span::GetSpan;
66
use oxc_traverse::{Ancestor, TraverseAncestry};
77
use phf::phf_set;
88

9-
use crate::{ir::index::SimpleExpressionNode, utils::expression::is_globally_allowed};
9+
use crate::expression::{SimpleExpressionNode, is_globally_allowed};
1010

1111
pub fn is_member_expression(exp: &SimpleExpressionNode) -> bool {
1212
let Some(ast) = &exp.ast else { return false };
@@ -638,3 +638,58 @@ fn is_in_desctructure_assignment(parent: &Ancestor, ancestry: &TraverseAncestry)
638638
}
639639
false
640640
}
641+
642+
// events
643+
static DELEGATED_EVENTS: phf::Set<&'static str> = phf_set! {
644+
"beforeinput",
645+
"click",
646+
"dblclick",
647+
"contextmenu",
648+
"focusin",
649+
"focusout",
650+
"input",
651+
"keydown",
652+
"keyup",
653+
"mousedown",
654+
"mousemove",
655+
"mouseout",
656+
"mouseover",
657+
"mouseup",
658+
"pointerdown",
659+
"pointermove",
660+
"pointerout",
661+
"pointerover",
662+
"pointerup",
663+
"touchend",
664+
"touchmove",
665+
"touchstart"
666+
};
667+
668+
pub fn is_delegated_events(s: &str) -> bool {
669+
DELEGATED_EVENTS.contains(s)
670+
}
671+
672+
pub fn is_event_option_modifier(modifier: &str) -> bool {
673+
matches!(modifier, "passive" | "once" | "capture")
674+
}
675+
676+
pub fn is_non_key_modifier(modifier: &str) -> bool {
677+
matches!(
678+
modifier,
679+
// event propagation management
680+
"stop" | "prevent" | "self" |
681+
// system modifiers + exact
682+
"ctrl" | "shift" | "alt" | "meta" | "exact" |
683+
// mouse
684+
"middle"
685+
)
686+
}
687+
688+
// left & right could be mouse or key modifiers based on event type
689+
pub fn maybe_key_modifier(modifier: &str) -> bool {
690+
matches!(modifier, "left" | "right")
691+
}
692+
693+
pub fn is_keyboard_event(key: &str) -> bool {
694+
matches!(key, "onkeyup" | "onkeydown" | "onkeypress")
695+
}

packages/compiler-rs/src/utils/directive.rs renamed to packages/compiler-rs/crates/common/src/directive.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
use napi::bindgen_prelude::{Either, Either3};
22
use oxc_ast::ast::{JSXAttribute, JSXAttributeItem, JSXAttributeName, JSXElement};
3-
use oxc_span::SPAN;
3+
use oxc_span::{SPAN, Span};
44

5-
use crate::{
6-
ir::index::{DirectiveNode, SimpleExpressionNode},
7-
transform::TransformContext,
8-
};
5+
use crate::expression::SimpleExpressionNode;
96

10-
pub fn resolve_directive<'a>(
11-
node: &'a mut JSXAttribute<'a>,
12-
context: &TransformContext<'a>,
13-
) -> DirectiveNode<'a> {
7+
#[derive(Debug)]
8+
pub struct DirectiveNode<'a> {
9+
// the normalized name without prefix or shorthands, e.g. "bind", "on"
10+
pub name: String,
11+
pub exp: Option<SimpleExpressionNode<'a>>,
12+
pub arg: Option<SimpleExpressionNode<'a>>,
13+
pub modifiers: Vec<SimpleExpressionNode<'a>>,
14+
pub loc: Span,
15+
}
16+
17+
pub fn resolve_directive<'a>(node: &'a mut JSXAttribute<'a>, source: &'a str) -> DirectiveNode<'a> {
1418
let mut arg_string = String::new();
1519
let mut name_string = match &node.name {
1620
JSXAttributeName::Identifier(name) => name.name.to_string(),
@@ -84,7 +88,7 @@ pub fn resolve_directive<'a>(
8488
let exp = node
8589
.value
8690
.as_mut()
87-
.map(|exp| SimpleExpressionNode::new(Either3::C(exp), context));
91+
.map(|exp| SimpleExpressionNode::new(Either3::C(exp), source));
8892

8993
let modifiers = modifiers
9094
.into_iter()

packages/compiler-rs/src/utils/expression.rs renamed to packages/compiler-rs/crates/common/src/expression.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ use oxc_ast::ast::{Expression, JSXAttributeValue, JSXChild};
44
use oxc_span::{GetSpan, SPAN, Span};
55
use phf::phf_set;
66

7-
use crate::{
8-
transform::TransformContext,
9-
utils::text::{get_text_like_value, resolve_jsx_text},
10-
};
7+
use crate::text::{get_text_like_value, resolve_jsx_text};
118

129
#[derive(Debug)]
1310
pub struct SimpleExpressionNode<'a> {
@@ -42,7 +39,7 @@ impl<'a> Default for SimpleExpressionNode<'a> {
4239
impl<'a> SimpleExpressionNode<'a> {
4340
pub fn new(
4441
node: Either3<&'a mut Expression<'a>, &'a mut JSXChild<'a>, &'a mut JSXAttributeValue<'a>>,
45-
context: &TransformContext<'a>,
42+
source: &'a str,
4643
) -> SimpleExpressionNode<'a> {
4744
let mut is_static = false;
4845
let mut ast = None;
@@ -51,27 +48,27 @@ impl<'a> SimpleExpressionNode<'a> {
5148
Either3::A(node) => {
5249
loc = node.span();
5350
ast = Some(node);
54-
loc.source_text(context.ir.borrow().source).to_string()
51+
loc.source_text(source).to_string()
5552
}
5653
Either3::B(node) => match node {
5754
JSXChild::ExpressionContainer(node) => {
5855
let expression = node.expression.to_expression_mut();
5956
loc = expression.span();
6057
ast = Some(expression);
61-
loc.source_text(context.ir.borrow().source).to_string()
58+
loc.source_text(source).to_string()
6259
}
6360
JSXChild::Text(node) => {
6461
is_static = true;
6562
resolve_jsx_text(node)
6663
}
6764
JSXChild::Element(node) => {
68-
context.ir.borrow().source[node.span.start as usize..node.span.end as usize].to_string()
65+
source[node.span.start as usize..node.span.end as usize].to_string()
6966
}
7067
JSXChild::Fragment(node) => {
71-
context.ir.borrow().source[node.span.start as usize..node.span.end as usize].to_string()
68+
source[node.span.start as usize..node.span.end as usize].to_string()
7269
}
7370
JSXChild::Spread(node) => {
74-
context.ir.borrow().source[node.span.start as usize..node.span.end as usize].to_string()
71+
source[node.span.start as usize..node.span.end as usize].to_string()
7572
}
7673
},
7774
Either3::C(node) => match node {
@@ -80,18 +77,18 @@ impl<'a> SimpleExpressionNode<'a> {
8077
is_static = matches!(expression, Expression::StringLiteral(_));
8178
loc = expression.span();
8279
ast = Some(expression);
83-
loc.source_text(context.ir.borrow().source).to_string()
80+
loc.source_text(source).to_string()
8481
}
8582
JSXAttributeValue::StringLiteral(node) => {
8683
is_static = true;
8784
loc = node.span;
8885
node.value.to_string()
8986
}
9087
JSXAttributeValue::Element(node) => {
91-
context.ir.borrow().source[node.span.start as usize..node.span.end as usize].to_string()
88+
source[node.span.start as usize..node.span.end as usize].to_string()
9289
}
9390
JSXAttributeValue::Fragment(node) => {
94-
context.ir.borrow().source[node.span.start as usize..node.span.end as usize].to_string()
91+
source[node.span.start as usize..node.span.end as usize].to_string()
9592
}
9693
},
9794
};

0 commit comments

Comments
 (0)