Skip to content

Commit

Permalink
chore: port wyw-shaker
Browse files Browse the repository at this point in the history
  • Loading branch information
layershifter committed Dec 17, 2024
1 parent 05d3d72 commit ebfadee
Show file tree
Hide file tree
Showing 18 changed files with 3,705 additions and 7 deletions.
306 changes: 301 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,19 @@ rust-version = "1.83.0"
[workspace.dependencies]
wyw_macros = { version = "0.1.0", path = "crates/wyw_macros" }
wyw_processor = { version = "0.1.0", path = "crates/wyw_processor" }
wyw_shaker = { version = "0.1.0", path = "crates/wyw_shaker" }
wyw_traverse = { version = "0.1.0", path = "crates/wyw_traverse" }

indoc = { version= "2.0.5" }
itertools = { version = "0.13.0" }
napi = { version = "2.12.2", default-features = false, features = ["napi4"] }
napi-build = "2.0.1"
napi-derive = "2.12.2"
normalize-path = { version = "0.2.1" }
oxc = "0.38.0"
oxc_index = "1.0.0"
oxc_resolver = { version = "2.1.1", features = ["package_json_raw_json_api", "pnp", "yarn_pnp"] }
oxc_semantic = "0.38.0"
regex = "1.11.1"
serde_json = "1.0.133"
tempfile = "3.14.0"
3 changes: 3 additions & 0 deletions crates/wyw_processor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ edition.workspace = true
rust-version.workspace = true

[dependencies]
wyw_traverse = { workspace = true }

oxc = { workspace = true }
13 changes: 12 additions & 1 deletion crates/wyw_processor/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
pub trait Processor {
use std::fmt::Debug;

pub mod params;
pub mod replacement_value;

pub trait Processor: Debug {
fn id(&self) -> &str;

fn transform(&self) -> String;
}

pub struct ProcessorTarget {
pub specifier: String,
pub source: String,
pub processor: Box<dyn Processor>,
}
101 changes: 101 additions & 0 deletions crates/wyw_processor/src/params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use crate::Processor;
use oxc::span::{Atom, Span};
use std::borrow::Cow;
use std::fmt::Debug;
use std::path::PathBuf;
use wyw_traverse::local_identifier::LocalIdentifier;

#[derive(Debug)]
pub enum ConstValue<'a> {
BigInt(Span, Atom<'a>),
Boolean(Span, bool),
Null(Span),
Number(Span, f64),
String(Span, Atom<'a>),
Undefined(Span),
}

pub enum ExpressionValue<'a> {
ConstValue(ConstValue<'a>),
Function(Span),
Ident(Span, Atom<'a>),
Source(Span),
TemplateValue {
cooked: Option<Atom<'a>>,
raw: Atom<'a>,
span: Span,
},
}

impl<'a> Debug for ExpressionValue<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ExpressionValue::ConstValue(value) => write!(f, "{:?}", value),
ExpressionValue::Function(span) => write!(f, "Function({:?})", span),
ExpressionValue::Ident(span, ident) => {
write!(f, "Ident({:?}..{:?}, {:?})", span.start, span.end, ident)
}
ExpressionValue::Source(span) => write!(f, "Source({:?}..{:?})", span.start, span.end),
ExpressionValue::TemplateValue { span, raw, .. } => {
write!(
f,
"TemplateValue({:?}..{:?}, {:?})",
span.start, span.end, raw
)
}
}
}
}

pub enum Param<'a> {
Callee(Span, LocalIdentifier<'a>),
Call(Span, Vec<ExpressionValue<'a>>),
Member(Span, Atom<'a>),
Template(Span, Vec<ExpressionValue<'a>>),
}

impl<'a> Debug for Param<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Param::Callee(span, ident) => {
write!(f, "Callee({:?}..{:?}, {:?})", span.start, span.end, ident)
}
Param::Call(span, args) => write!(f, "Call({:?}..{:?}, {:?}))", span.start, span.end, args),
Param::Member(span, prop) => {
write!(f, "Member({:?}..{:?}, {:?}))", span.start, span.end, prop)
}
Param::Template(span, exprs) => write!(
f,
"Template({:?}..{:?}, {:?}))",
span.start, span.end, exprs
),
}
}
}

pub struct ProcessorParams<'a> {
pub idx: usize,
pub display_name: Cow<'a, str>,
pub params: Vec<Param<'a>>,
pub root: &'a PathBuf,
pub filename: &'a PathBuf,
}

#[derive(Debug)]
pub struct ProcessorCall<'a> {
pub span: Span,
pub processor: Box<dyn Processor>,
pub params: ProcessorParams<'a>,
}

impl<'a> Debug for ProcessorParams<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ProcessorParams")
.field("idx", &self.idx)
.field("display_name", &self.display_name)
.field("params", &self.params)
.finish()
}
}

pub type ProcessorCalls<'a> = Vec<ProcessorCall<'a>>;
19 changes: 19 additions & 0 deletions crates/wyw_processor/src/replacement_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use oxc::span::Span;

#[derive(Debug, PartialEq)]
pub enum ReplacementValue {
Del,
Span(Span),
Str(String),
Undefined,
}

impl ReplacementValue {
pub fn from_string(s: &str) -> Self {
if s == "undefined" {
Self::Undefined
} else {
Self::Str(s.to_string())
}
}
}
3 changes: 2 additions & 1 deletion crates/wyw_sample_processor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub struct TransformOptions {
// TODO: this should be generated by a macro

#[napi]
pub fn transform(filename: String, source_code: String, options: TransformOptions) -> String {
pub fn transform(_: String, _: String, _: TransformOptions) -> String {
let processors = HashMap::from([(TransformTargetProcessors::SampleTag, SampleTagProcessor {})]);

// TODO this is a stub implementation, will be moved to wyw-in-js-transform
Expand All @@ -39,6 +39,7 @@ pub fn transform(filename: String, source_code: String, options: TransformOption

// TODO: this is an actual impl, will stay in this crate

#[derive(Debug)]
struct SampleTagProcessor {}

impl Processor for SampleTagProcessor {
Expand Down
21 changes: 21 additions & 0 deletions crates/wyw_shaker/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "wyw_shaker"
version = "0.1.0"

edition.workspace = true
rust-version.workspace = true

[dependencies]
wyw_processor = { workspace = true }
wyw_traverse = { workspace = true }

indoc = { workspace = true }
itertools = { workspace = true }
normalize-path = { workspace = true }
oxc = { workspace = true }
oxc_index = { workspace = true }
oxc_resolver = { workspace = true }
oxc_semantic = { workspace = true }
regex = { workspace = true }
serde_json = { workspace = true }
tempfile = { workspace = true }
Loading

0 comments on commit ebfadee

Please sign in to comment.