Vendor things

This commit is contained in:
John Doty 2024-03-08 11:03:01 -08:00
parent 5deceec006
commit 977e3c17e5
19434 changed files with 10682014 additions and 0 deletions

View file

@ -0,0 +1,10 @@
extern crate proc_macro;
mod merge;
#[proc_macro_derive(Merge)]
pub fn derive_merge(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = syn::parse(input).expect("failed to parse input as DeriveInput");
self::merge::expand(input).into()
}

View file

@ -0,0 +1,64 @@
use pmutil::{q, SpanExt};
use proc_macro2::TokenStream;
use quote::ToTokens;
use swc_macros_common::{access_field, join_stmts};
use syn::{DeriveInput, Expr, Field, Fields, Stmt};
pub fn expand(input: DeriveInput) -> TokenStream {
match &input.data {
syn::Data::Struct(s) => {
let body = call_merge_for_fields(&q!({ self }), &s.fields);
let body = join_stmts(&body);
q!(
Vars {
Type: &input.ident,
body
},
{
#[automatically_derived]
impl swc_config::merge::Merge for Type {
fn merge(&mut self, _other: Self) {
body
}
}
}
)
.into()
}
syn::Data::Enum(_) => unimplemented!("derive(Merge) does not support an enum"),
syn::Data::Union(_) => unimplemented!("derive(Merge) does not support a union"),
}
}
fn call_merge_for_fields(obj: &dyn ToTokens, fields: &Fields) -> Vec<Stmt> {
fn call_merge(obj: &dyn ToTokens, idx: usize, f: &Field) -> Expr {
let r = q!({ _other });
q!(
Vars {
l: access_field(obj, idx, f),
r: access_field(&r, idx, f),
},
{ swc_config::merge::Merge::merge(&mut l, r) }
)
.parse()
}
match fields {
Fields::Named(fs) => fs
.named
.iter()
.enumerate()
.map(|(idx, f)| call_merge(obj, idx, f))
.map(|expr| Stmt::Expr(expr, Some(fs.brace_token.span.join().as_token())))
.collect(),
Fields::Unnamed(fs) => fs
.unnamed
.iter()
.enumerate()
.map(|(idx, f)| call_merge(obj, idx, f))
.map(|expr| Stmt::Expr(expr, Some(fs.paren_token.span.join().as_token())))
.collect(),
Fields::Unit => unimplemented!("derive(Merge) does not support a unit struct"),
}
}