Skip to content

Commit

Permalink
feat(visit): Add path-aware variants (#5073)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 authored Jul 5, 2022
1 parent d2284f5 commit 204d742
Show file tree
Hide file tree
Showing 12 changed files with 1,428 additions and 213 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@

SWC (stands for `Speedy Web Compiler`) is a super-fast TypeScript / JavaScript compiler written in Rust. It's a library for Rust and JavaScript at the same time. If you are using SWC from Rust, see [rustdoc](https://rustdoc.swc.rs/swc/) and for most users, your entry point for using the library will be [parser](https://rustdoc.swc.rs/swc_ecma_parser/).

Also, SWC tries to ensure that

> If you select the latest version of each crates, it will work
for rust users. Without such guarantee, using SWC would be too hard as SWC is a large, modular project and typically you have to use many modules.

---

If you are using SWC from JavaScript, please refer to [docs on the website](https://swc.rs/docs/installation/).

# Documentation
Expand Down
9 changes: 8 additions & 1 deletion crates/swc_css_visit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ name = "swc_css_visit"
repository = "https://github.com/swc-project/swc.git"
version = "0.94.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lib]
bench = false

[features]
path = []

[dependencies]
swc_atoms = {version = "0.2.7", path = "../swc_atoms"}
swc_common = { version = "0.20.0", path = "../swc_common"}
swc_common = {version = "0.20.0", path = "../swc_common"}
swc_css_ast = {version = "0.95.0", path = "../swc_css_ast"}
swc_visit = {version = "0.3.0", path = "../swc_visit"}
7 changes: 6 additions & 1 deletion crates/swc_ecma_visit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,21 @@ name = "swc_ecma_visit"
repository = "https://github.com/swc-project/swc.git"
version = "0.67.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lib]
bench = false

[features]
debug = []
path = []

[dependencies]
num-bigint = {version = "0.4", features = ["serde"]}
swc_atoms = {version = "0.2", path = "../swc_atoms"}
swc_common = { version = "0.20.0", path = "../swc_common"}
swc_common = {version = "0.20.0", path = "../swc_common"}
swc_ecma_ast = {version = "0.81.0", path = "../swc_ecma_ast"}
swc_visit = {version = "0.3.0", path = "../swc_visit"}
tracing = "0.1.32"
9 changes: 8 additions & 1 deletion crates/swc_html_visit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,18 @@ name = "swc_html_visit"
repository = "https://github.com/swc-project/swc.git"
version = "0.13.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[lib]
bench = false

[features]
path = []

[dependencies]
swc_atoms = {version = "0.2.7", path = "../swc_atoms"}
swc_common = { version = "0.20.0", path = "../swc_common"}
swc_common = {version = "0.20.0", path = "../swc_common"}
swc_html_ast = {version = "0.13.0", path = "../swc_html_ast"}
swc_visit = {version = "0.3.0", path = "../swc_visit"}
1 change: 1 addition & 0 deletions crates/swc_visit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/expanded.rs
108 changes: 108 additions & 0 deletions crates/swc_visit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@
//! You can use your `Visit` implementation like `node.visit_with(&Invalid{
//! span: DUMMY_SP, }, &mut visitor`. I think API is mis-designed, but it works
//! and there are really lots of code using `Visit` already.
//!
//!
//!
//! # Cargo features
//!
//! You should add
//! ```toml
//! [features]
//! path = []
//! ```
//!
//! If you want to allow using path-aware visitor.
pub use either::Either;
pub use swc_visit_macros::define;
Expand Down Expand Up @@ -191,3 +203,99 @@ where
self.second.reset();
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AstKindPath<K>
where
K: Copy,
{
path: Vec<K>,
}

impl<K> std::ops::Deref for AstKindPath<K>
where
K: Copy,
{
type Target = Vec<K>;

fn deref(&self) -> &Self::Target {
&self.path
}
}

impl<K> Default for AstKindPath<K>
where
K: Copy,
{
fn default() -> Self {
Self {
path: Default::default(),
}
}
}

impl<K> AstKindPath<K>
where
K: Copy,
{
pub fn new(path: Vec<K>) -> Self {
Self { path }
}

pub fn with<Ret>(&mut self, path: K, op: impl FnOnce(&mut Self) -> Ret) -> Ret {
self.path.push(path);
let ret = op(self);
self.path.pop();
ret
}
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AstNodePath<N>
where
N: Copy,
{
path: Vec<N>,
}

impl<N> std::ops::Deref for AstNodePath<N>
where
N: Copy,
{
type Target = Vec<N>;

fn deref(&self) -> &Self::Target {
&self.path
}
}

impl<N> Default for AstNodePath<N>
where
N: Copy,
{
fn default() -> Self {
Self {
path: Default::default(),
}
}
}

impl<N> AstNodePath<N>
where
N: Copy,
{
pub fn new(path: Vec<N>) -> Self {
Self { path }
}

pub fn with<F, Ret>(&mut self, node: N, op: F) -> Ret
where
F: for<'aa> FnOnce(&'aa mut AstNodePath<N>) -> Ret,
{
self.path.push(node);
let ret = op(self);
self.path.pop();

ret
}
}
2 changes: 2 additions & 0 deletions crates/swc_visit/tests/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::sync::Arc;

use swc_visit::define;

#[derive(Debug, PartialEq)]
pub struct Item {
pub item: Option<Arc<Item>>,
pub ref_to_enum: Option<Arc<Enum>>,
}
#[derive(Debug, PartialEq)]
pub enum Enum {
Item(Arc<Item>),
Items(Arc<Vec<Item>>),
Expand Down
8 changes: 8 additions & 0 deletions crates/swc_visit/tests/fold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@ pub trait Node: Any {}

impl<T: ?Sized> Node for T where T: Any {}

#[derive(Debug, PartialEq)]
pub struct Item {
// pub field: usize,
// pub inner: Option<Box<Item>>,
pub opt_vec: Option<Vec<Item>>,
pub vec_opt: Vec<Option<Item>>,

pub value: f64,
}
#[derive(Debug, PartialEq)]
pub enum Enum {
Item(Item),
Boxed(Box<Enum>),
}

define!({
Expand All @@ -25,8 +30,11 @@ define!({
// pub inner: Option<Box<Item>>,
pub opt_vec: Option<Vec<Item>>,
pub vec_opt: Vec<Option<Item>>,

pub value: f64,
}
pub enum Enum {
Item(Item),
Boxed(Box<Enum>),
}
});
2 changes: 2 additions & 0 deletions crates/swc_visit/tests/opt_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ pub trait Node: Any {}

impl<T: ?Sized> Node for T where T: Any {}

#[derive(Debug, PartialEq)]
pub struct Item {
pub opt_vec1: Option<Vec<Item>>,
pub opt_vec2: Option<Vec<Enum>>,
}
#[derive(Debug, PartialEq)]
pub enum Enum {
Item(Item),
}
Expand Down
9 changes: 2 additions & 7 deletions crates/swc_visit/tests/vec_opt.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
#![allow(clippy::ptr_arg)]

use std::any::Any;

use swc_visit::define;

/// Visitable nodes.
pub trait Node: Any {}

impl<T: ?Sized> Node for T where T: Any {}

#[derive(Debug, PartialEq)]
pub struct Item {
pub vec_opt1: Vec<Option<Item>>,
pub vec_opt2: Vec<Option<Enum>>,
}
#[derive(Debug, PartialEq)]
pub enum Enum {
Item(Item),
}
Expand Down
Loading

1 comment on commit 204d742

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 204d742 Previous: 2a29b50 Ratio
es/full/minify/libraries/antd 1645568957 ns/iter (± 20887065) 1709850932 ns/iter (± 19143363) 0.96
es/full/minify/libraries/d3 409586322 ns/iter (± 10587627) 452432463 ns/iter (± 8575740) 0.91
es/full/minify/libraries/echarts 1613964429 ns/iter (± 23749483) 1768362844 ns/iter (± 40313249) 0.91
es/full/minify/libraries/jquery 88593617 ns/iter (± 2852172) 107269738 ns/iter (± 8163001) 0.83
es/full/minify/libraries/lodash 123223614 ns/iter (± 8104909) 128038376 ns/iter (± 7559327) 0.96
es/full/minify/libraries/moment 57572524 ns/iter (± 2470404) 60446458 ns/iter (± 2332435) 0.95
es/full/minify/libraries/react 18979194 ns/iter (± 601944) 20090679 ns/iter (± 871301) 0.94
es/full/minify/libraries/terser 596416211 ns/iter (± 9483327) 639509804 ns/iter (± 10616103) 0.93
es/full/minify/libraries/three 538408911 ns/iter (± 9380599) 589347237 ns/iter (± 9485970) 0.91
es/full/minify/libraries/typescript 3402142739 ns/iter (± 64459217) 3548924747 ns/iter (± 92142353) 0.96
es/full/minify/libraries/victory 704827816 ns/iter (± 10928979) 728756729 ns/iter (± 7150920) 0.97
es/full/minify/libraries/vue 131182834 ns/iter (± 4165899) 147946322 ns/iter (± 10505545) 0.89
es/full/codegen/es3 31180 ns/iter (± 636) 30999 ns/iter (± 495) 1.01
es/full/codegen/es5 31251 ns/iter (± 779) 30980 ns/iter (± 337) 1.01
es/full/codegen/es2015 31202 ns/iter (± 1753) 30970 ns/iter (± 787) 1.01
es/full/codegen/es2016 31286 ns/iter (± 1127) 30716 ns/iter (± 427) 1.02
es/full/codegen/es2017 31214 ns/iter (± 1544) 30911 ns/iter (± 545) 1.01
es/full/codegen/es2018 31321 ns/iter (± 729) 30868 ns/iter (± 673) 1.01
es/full/codegen/es2019 31305 ns/iter (± 909) 30690 ns/iter (± 1103) 1.02
es/full/codegen/es2020 31681 ns/iter (± 346) 30764 ns/iter (± 1121) 1.03
es/full/all/es3 187827533 ns/iter (± 4162476) 187922163 ns/iter (± 8874832) 1.00
es/full/all/es5 177806990 ns/iter (± 4975691) 178486964 ns/iter (± 10751446) 1.00
es/full/all/es2015 144996657 ns/iter (± 6308711) 151532604 ns/iter (± 6414266) 0.96
es/full/all/es2016 144627665 ns/iter (± 4798638) 153911911 ns/iter (± 7050760) 0.94
es/full/all/es2017 144693521 ns/iter (± 4574964) 149999546 ns/iter (± 4935623) 0.96
es/full/all/es2018 145268118 ns/iter (± 4670133) 145324516 ns/iter (± 6069998) 1.00
es/full/all/es2019 143915531 ns/iter (± 4705254) 138313292 ns/iter (± 3764771) 1.04
es/full/all/es2020 131923862 ns/iter (± 3104832) 137475945 ns/iter (± 6069616) 0.96
es/full/parser 761028 ns/iter (± 56598) 757426 ns/iter (± 16589) 1.00
es/full/base/fixer 28793 ns/iter (± 2972) 28626 ns/iter (± 825) 1.01
es/full/base/resolver_and_hygiene 87979 ns/iter (± 1405) 88676 ns/iter (± 4548) 0.99
serialization of ast node 215 ns/iter (± 3) 214 ns/iter (± 3) 1.00
serialization of serde 228 ns/iter (± 5) 223 ns/iter (± 6) 1.02

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.