Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(es/ast): Provide API like magicast #8167

Closed
wants to merge 81 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
81 commits
Select commit Hold shift + click to select a range
94c1d48
init
kdy1 Oct 20, 2023
76e4f3a
hmm
kdy1 Oct 20, 2023
75edcdb
more
kdy1 Oct 20, 2023
ee41d25
exprnode
kdy1 Oct 20, 2023
2ff2b42
Value<T>
kdy1 Oct 20, 2023
777d9da
import / ensure / proxy
kdy1 Oct 21, 2023
ceb4094
import apis
kdy1 Oct 21, 2023
8f9d479
CastableTo
kdy1 Oct 21, 2023
2ae2992
new_proxied
kdy1 Oct 21, 2023
aae3976
WellP
kdy1 Oct 21, 2023
a5cc3ad
node.rs
kdy1 Oct 22, 2023
325b608
Dpes
kdy1 Oct 22, 2023
ecec1a9
cargo lockfile
kdy1 Oct 22, 2023
39182ce
WIP
kdy1 Oct 22, 2023
e2098af
Data
kdy1 Oct 22, 2023
117c579
Data<T>
kdy1 Oct 22, 2023
1dbcef8
Rename
kdy1 Oct 22, 2023
8a39db7
modules
kdy1 Oct 22, 2023
8336e13
more
kdy1 Oct 22, 2023
0776b03
VecNode
kdy1 Oct 22, 2023
670df62
More work
kdy1 Oct 22, 2023
5bf5c7d
More
kdy1 Oct 22, 2023
ee45e19
More work
kdy1 Oct 22, 2023
5132b79
more
kdy1 Oct 22, 2023
24c2dc1
more
kdy1 Oct 22, 2023
73e2d7b
clone
kdy1 Oct 22, 2023
f6d8205
more work for consturction
kdy1 Oct 22, 2023
363ef1d
WIP
kdy1 Oct 22, 2023
fe5abb6
named
kdy1 Oct 22, 2023
21c5ec8
more
kdy1 Oct 22, 2023
5c1edd3
Refactor
kdy1 Oct 22, 2023
d5ed593
api
kdy1 Oct 22, 2023
d7ac41c
more
kdy1 Oct 22, 2023
5cff66c
mode
kdy1 Oct 22, 2023
527e54e
WIP: Opt
kdy1 Oct 22, 2023
818ece8
lint
kdy1 Oct 22, 2023
343a62c
Change API
kdy1 Oct 22, 2023
b94520c
some
kdy1 Oct 22, 2023
4d12d0f
with
kdy1 Oct 22, 2023
2fbf4b2
BaseImportNode
kdy1 Oct 22, 2023
3e15798
More work
kdy1 Oct 22, 2023
3b3835f
Dep
kdy1 Oct 22, 2023
e1e199a
cargo lockfile
kdy1 Oct 22, 2023
3a74d52
more
kdy1 Oct 22, 2023
f8a4baf
API
kdy1 Oct 22, 2023
f7b93da
Rename
kdy1 Oct 22, 2023
0f0ab37
more
kdy1 Oct 22, 2023
f06134a
lifetime
kdy1 Oct 22, 2023
24b8579
lifetime
kdy1 Oct 22, 2023
00418e5
Fix .map
kdy1 Oct 22, 2023
000d684
More fix
kdy1 Oct 22, 2023
8cc86d0
more fix
kdy1 Oct 22, 2023
fd9115b
more fix
kdy1 Oct 22, 2023
ec82749
fix
kdy1 Oct 22, 2023
1e37440
lint
kdy1 Oct 22, 2023
055da06
new()
kdy1 Oct 22, 2023
0bf264b
panic
kdy1 Oct 22, 2023
ba006c0
Add a test
kdy1 Oct 22, 2023
eb12f91
more
kdy1 Oct 22, 2023
ebe4652
index
kdy1 Oct 22, 2023
f742244
Clone + Optional
kdy1 Oct 22, 2023
13576ff
more
kdy1 Oct 22, 2023
a931324
Improve API
kdy1 Oct 22, 2023
36735e1
ensure
kdy1 Oct 22, 2023
72f4b05
Rename
kdy1 Oct 22, 2023
dbf5bfe
`map_opt`
kdy1 Oct 22, 2023
cd38f1a
more work
kdy1 Oct 22, 2023
920975e
Resolve more todo!
kdy1 Oct 22, 2023
9615554
Add macros.rs
kdy1 Oct 31, 2023
edd4436
Export default
kdy1 Oct 31, 2023
2a73735
a;pi
kdy1 Oct 31, 2023
e18b90c
doc
kdy1 Oct 31, 2023
08e8e06
Add `ProxyNode`
kdy1 Oct 31, 2023
444888c
node type
kdy1 Oct 31, 2023
71c78ca
more work for `ProxyNode`
kdy1 Oct 31, 2023
3f31177
more work
kdy1 Oct 31, 2023
c8cde9e
Rename
kdy1 Oct 31, 2023
416d18a
Rnemae
kdy1 Oct 31, 2023
20dbf71
more work
kdy1 Oct 31, 2023
25366d0
Remove `ProxyNode`
kdy1 Oct 31, 2023
2fcd916
node
kdy1 Oct 31, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"xtask",
"crates/swc_ecma_magicast",
"crates/swc_core",
"crates/swc_cli_impl",
"crates/dbg-swc",
Expand Down Expand Up @@ -41,4 +42,4 @@ debug = true
opt-level = 3

[profile.dev.package."*"]
opt-level = 3
opt-level = 3
15 changes: 15 additions & 0 deletions crates/swc_ecma_magicast/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "swc_ecma_magicast"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
swc_atoms = { version = "0.6.0", path = "../swc_atoms" }
swc_common = { version = "0.33.0", path = "../swc_common" }
swc_ecma_ast = { version = "0.110.0", path = "../swc_ecma_ast" }
swc_ecma_utils = { version = "0.124.7", path = "../swc_ecma_utils" }

[dev-dependencies]
swc_ecma_parser = { path = "../swc_ecma_parser" }
69 changes: 69 additions & 0 deletions crates/swc_ecma_magicast/src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use std::{cell::RefCell, rc::Rc};

use swc_ecma_ast::Program;

/// TODO: Make this generic over the root.
pub(crate) struct Data<'a, T: 'static>(
Rc<RefCell<Program>>,
Rc<dyn 'a + Fn(&Program) -> &T>,
Rc<dyn 'a + Fn(&mut Program) -> &mut T>,
);

impl<T: 'static> Clone for Data<'_, T> {
fn clone(&self) -> Self {
Data(self.0.clone(), self.1.clone(), self.2.clone())
}
}

impl<'a, T: 'static> Data<'a, T> {
pub fn new_root(root: Program) -> Data<'a, Program> {
Data(
Rc::new(RefCell::new(root)),
Rc::new(|root| root),
Rc::new(|root| root),
)
}

pub fn map<N>(
&'a self,
f1: impl 'a + Fn(&T) -> &N,
f2: impl 'a + Fn(&mut T) -> &mut N,
) -> Data<'a, N>
where
N: 'static,
{
Data(
self.0.clone(),
Rc::new(move |data| f1((self.1)(data))),
Rc::new(move |data| f2((self.2)(data))),
)
}

pub fn map_opt<N>(
&'a self,
f1: impl 'a + Fn(&T) -> Option<&N>,
f2: impl 'a + Fn(&mut T) -> Option<&mut N>,
) -> Option<Data<'a, N>>
where
N: 'static,
{
{
let data = self.0.borrow();
// Check it using actual data
f1((self.1)(&data))?;
}
Some(Data(
self.0.clone(),
Rc::new(move |data| f1((self.1)(data)).unwrap()),
Rc::new(move |data| f2((self.2)(data)).unwrap()),
))
}

pub fn with<Ret>(&self, f: impl FnOnce(&T) -> Ret) -> Ret {
f((self.1)(&mut self.0.borrow()))
}

pub fn with_mut<Ret>(&self, f: impl FnOnce(&mut T) -> Ret) -> Ret {
f((self.2)(&mut self.0.borrow_mut()))
}
}
36 changes: 36 additions & 0 deletions crates/swc_ecma_magicast/src/expr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use swc_ecma_ast::Expr;

use crate::{data::Data, AstProxyNode, VecNode};

pub struct ArrayNode<'a> {
elems: VecNode<SpreadNode<ExprNode<'a>>>,
}

pub struct ObjectNode {}

pub trait ExprLike {}

#[derive(Clone)]
pub struct ExprNode<'a>(pub(crate) Data<'a, Box<Expr>>);

impl AstProxyNode for ExprNode<'_> {}

impl<'a> ExprNode<'a> {
pub fn cast<T>(&self) -> T
where
T: ExprLike,
{
todo!()
}
}

#[derive(Clone)]
pub struct SpreadNode<T>
where
T: AstProxyNode,
{
has_spread: bool,
node: T,
}

impl<T> AstProxyNode for SpreadNode<T> where T: AstProxyNode {}
44 changes: 44 additions & 0 deletions crates/swc_ecma_magicast/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::ops::{Deref, Shl};

use swc_atoms::Atom;
use swc_ecma_ast::Program;

use crate::data::Data;
pub use crate::{expr::*, module::*, option::OptionalNode, traits::*, vec::VecNode};
#[macro_use]
mod macros;
mod data;
mod expr;
mod module;
mod option;
mod traits;
mod vec;

#[derive(Clone)]
pub struct ProgramNode {
data: Data<'static, Program>,
}

impl ProgramNode {
pub fn new(root: Program) -> Self {
Self {
data: Data::<Program>::new_root(root),
}
}

/// This will panic if the program is not a module.
pub fn as_module(&self) -> ModuleNode {
let data = self.data.map(
|program| program.as_module().unwrap(),
|program| program.as_mut_module().unwrap(),
);
ModuleNode(data)
}
}

impl AstProxyNode for ProgramNode {}

#[derive(Clone)]
pub struct BindingRef {
sym: Atom,
}
1 change: 1 addition & 0 deletions crates/swc_ecma_magicast/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@