Skip to content

Commit d93ec90

Browse files
committed
refactor(visit): Use separate crate for hooks (#11243)
1 parent 3a141ed commit d93ec90

File tree

11 files changed

+8660
-9536
lines changed

11 files changed

+8660
-9536
lines changed

.tokeignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
**/generated.rs

Cargo.lock

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

crates/swc_ecma_hooks/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
authors = ["강동윤 <[email protected]>"]
3+
description = "Composable hooks for swc ecmascript visitors"
4+
documentation = "https://rustdoc.swc.rs/swc_ecma_hooks/"
5+
edition = { workspace = true }
6+
license = { workspace = true }
7+
name = "swc_ecma_hooks"
8+
repository = { workspace = true }
9+
version = "0.1.0"
10+
11+
[package.metadata.docs.rs]
12+
all-features = true
13+
rustdoc-args = ["--cfg", "docsrs"]
14+
15+
[lib]
16+
bench = false
17+
18+
[dependencies]
19+
swc_atoms = { version = "9.0.0", path = "../swc_atoms" }
20+
swc_common = { version = "17.0.0", path = "../swc_common" }
21+
swc_ecma_ast = { version = "18.0.0", path = "../swc_ecma_ast" }
22+
swc_ecma_visit = { version = "18.0.0", path = "../swc_ecma_visit" }

crates/swc_ecma_hooks/src/generated.rs

Lines changed: 8463 additions & 0 deletions
Large diffs are not rendered by default.

crates/swc_ecma_hooks/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//! Composable hooks for SWC ECMAScript visitors.
2+
//!
3+
//! This crate provides a hook-based API for composing AST visitors,
4+
//! allowing you to build complex visitors by combining simple hooks.
5+
6+
#![cfg_attr(docsrs, feature(doc_cfg))]
7+
#![deny(clippy::all)]
8+
#![allow(clippy::ptr_arg)]
9+
10+
pub use swc_ecma_ast;
11+
pub use swc_ecma_visit;
12+
13+
pub use crate::generated::*;
14+
mod generated;

crates/swc_ecma_regexp_visit/src/generated.rs

Lines changed: 0 additions & 1068 deletions
Large diffs are not rendered by default.

crates/swc_ecma_visit/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,6 @@ swc_atoms = { version = "9.0.0", path = "../swc_atoms" }
3434
swc_common = { version = "17.0.0", path = "../swc_common" }
3535
swc_ecma_ast = { version = "18.0.0", path = "../swc_ecma_ast" }
3636
swc_visit = { version = "2.0.1", path = "../swc_visit" }
37+
38+
[dev-dependencies]
39+
swc_ecma_hooks = { version = "0.1.0", path = "../swc_ecma_hooks" }

crates/swc_ecma_visit/src/generated.rs

Lines changed: 0 additions & 8458 deletions
Large diffs are not rendered by default.

crates/swc_ecma_visit/tests/compose.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use swc_atoms::atom;
22
use swc_common::{Span, DUMMY_SP};
33
use swc_ecma_ast::*;
4-
use swc_ecma_visit::{CompositeHook, VisitMutHook, VisitMutWith, VisitMutWithHook};
4+
use swc_ecma_hooks::{CompositeHook, VisitMutHook, VisitMutWithHook};
5+
use swc_ecma_visit::VisitMutWith;
56

67
#[derive(Debug, Default)]
78
struct Hook {

tools/generate-code/src/generators/visitor.rs

Lines changed: 56 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,6 @@ pub fn generate(crate_name_str: &str, node_types: &[&Item], excluded_types: &[St
9696
}
9797
}
9898

99-
// Generate VisitMutHook trait and related types
100-
output
101-
.items
102-
.extend(generate_visit_mut_hook_trait(&all_types));
103-
output.items.extend(generate_composite_hook(&all_types));
104-
output
105-
.items
106-
.extend(generate_visit_mut_with_hook(&all_types));
107-
10899
output.items.push(parse_quote!(
109100
#[cfg(any(docsrs, feature = "path"))]
110101
pub type AstKindPath = swc_visit::AstKindPath<AstParentKind>;
@@ -118,6 +109,59 @@ pub fn generate(crate_name_str: &str, node_types: &[&Item], excluded_types: &[St
118109
output
119110
}
120111

112+
#[cfg(test)]
113+
pub fn generate_hooks(
114+
_crate_name_str: &str,
115+
node_types: &[&Item],
116+
excluded_types: &[String],
117+
) -> File {
118+
let mut output = File {
119+
shebang: None,
120+
attrs: Vec::new(),
121+
items: Vec::new(),
122+
};
123+
let mut all_types = all_field_types(node_types).into_iter().collect::<Vec<_>>();
124+
125+
if !excluded_types.is_empty() {
126+
all_types.retain(|ty| {
127+
!excluded_types
128+
.iter()
129+
.any(|type_name| ty.contains_type(type_name))
130+
});
131+
}
132+
133+
all_types.sort_by_cached_key(|v| v.method_name());
134+
135+
output.attrs.push(parse_quote!(
136+
//! This file is generated by `tools/generate-code`. DO NOT MODIFY.
137+
));
138+
output.attrs.push(parse_quote!(
139+
#![allow(unused_variables)]
140+
));
141+
output.attrs.push(parse_quote!(
142+
#![allow(clippy::all)]
143+
));
144+
145+
output.items.push(parse_quote!(
146+
use swc_ecma_ast::*;
147+
));
148+
149+
output.items.push(parse_quote!(
150+
use swc_ecma_visit::*;
151+
));
152+
153+
// Generate VisitMutHook trait and related types
154+
output
155+
.items
156+
.extend(generate_visit_mut_hook_trait(&all_types));
157+
output.items.extend(generate_composite_hook(&all_types));
158+
output
159+
.items
160+
.extend(generate_visit_mut_with_hook(&all_types));
161+
162+
output
163+
}
164+
121165
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
122166
enum FieldType {
123167
Normal(String),
@@ -1721,6 +1765,7 @@ fn define_fields(crate_name: &Ident, node_types: &[&Item]) -> Vec<Item> {
17211765

17221766
/// Generates the VisitMutHook trait with enter_xxx and exit_xxx methods for
17231767
/// each AST type
1768+
#[cfg(test)]
17241769
fn generate_visit_mut_hook_trait(all_types: &[FieldType]) -> Vec<Item> {
17251770
let mut items = Vec::<Item>::new();
17261771
let mut trait_methods = Vec::<TraitItem>::new();
@@ -1777,6 +1822,7 @@ fn generate_visit_mut_hook_trait(all_types: &[FieldType]) -> Vec<Item> {
17771822
}
17781823

17791824
/// Generates the CompositeHook<A, B> struct and its VisitMutHook implementation
1825+
#[cfg(test)]
17801826
fn generate_composite_hook(all_types: &[FieldType]) -> Vec<Item> {
17811827
let mut items = Vec::<Item>::new();
17821828
let mut impl_methods = Vec::<TraitItem>::new();
@@ -1842,6 +1888,7 @@ fn generate_composite_hook(all_types: &[FieldType]) -> Vec<Item> {
18421888
}
18431889

18441890
/// Generates the VisitMutWithHook<H> adapter that implements VisitMut
1891+
#[cfg(test)]
18451892
fn generate_visit_mut_with_hook(all_types: &[FieldType]) -> Vec<Item> {
18461893
let mut items = Vec::<Item>::new();
18471894
let mut impl_methods = Vec::<TraitItem>::new();

0 commit comments

Comments
 (0)