refactor(macros/ast-node): Drop darling to reduce compile time (#7214)

This commit is contained in:
Donny/강동윤 2023-04-05 14:15:50 +09:00 committed by GitHub
parent a22a8a70ed
commit 3f61638cbf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 50 deletions

44
Cargo.lock generated
View File

@ -98,7 +98,6 @@ checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
name = "ast_node"
version = "0.9.1"
dependencies = [
"darling 0.13.1",
"pmutil",
"proc-macro2",
"quote",
@ -762,38 +761,14 @@ dependencies = [
"syn",
]
[[package]]
name = "darling"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0d720b8683f8dd83c65155f0530560cba68cd2bf395f6513a483caee57ff7f4"
dependencies = [
"darling_core 0.13.1",
"darling_macro 0.13.1",
]
[[package]]
name = "darling"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4529658bdda7fd6769b8614be250cdcfc3aeb0ee72fe66f9e41e5e5eb73eac02"
dependencies = [
"darling_core 0.14.1",
"darling_macro 0.14.1",
]
[[package]]
name = "darling_core"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a340f241d2ceed1deb47ae36c4144b2707ec7dd0b649f894cb39bb595986324"
dependencies = [
"fnv",
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn",
"darling_core",
"darling_macro",
]
[[package]]
@ -809,24 +784,13 @@ dependencies = [
"syn",
]
[[package]]
name = "darling_macro"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72c41b3b7352feb3211a0d743dc5700a4e3b60f51bd2b368892d1e0f9a95f44b"
dependencies = [
"darling_core 0.13.1",
"quote",
"syn",
]
[[package]]
name = "darling_macro"
version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ddfc69c5bfcbd2fc09a0f38451d2daf0e372e367986a83906d1b0dbc88134fb5"
dependencies = [
"darling_core 0.14.1",
"darling_core",
"quote",
"syn",
]
@ -997,7 +961,7 @@ version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03e7b551eba279bf0fa88b83a46330168c1560a52a94f5126f892f0b364ab3e0"
dependencies = [
"darling 0.14.1",
"darling",
"proc-macro2",
"quote",
"syn",

View File

@ -13,13 +13,12 @@ bench = false
proc-macro = true
[dependencies]
darling = "0.13"
pmutil = "0.5.1"
proc-macro2 = "1"
quote = "1"
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
[dependencies.syn]
features = ["derive", "fold", "parsing", "printing", "visit-mut"]
version = "1"
[dependencies.syn]
features = ["derive", "fold", "parsing", "printing", "visit-mut"]
version = "1"

View File

@ -1,12 +1,9 @@
#![allow(dead_code)]
use darling::FromField;
use pmutil::{smart_quote, Quote, ToTokensExt};
use swc_macros_common::prelude::*;
use syn::*;
#[derive(FromField)]
#[darling(attributes(span))]
struct MyField {
/// Name of the field.
pub ident: Option<Ident>,
@ -14,13 +11,52 @@ struct MyField {
pub ty: Type,
/// `#[span(lo)]`
#[darling(default)]
pub lo: bool,
/// `#[span(hi)]`
#[darling(default)]
pub hi: bool,
}
impl MyField {
fn from_field(f: &Field) -> Self {
let mut lo = false;
let mut hi = false;
for attr in &f.attrs {
if !is_attr_name(attr, "span") {
continue;
}
let meta = attr.parse_meta().unwrap();
match meta {
Meta::List(list) => {
for nested in list.nested {
match nested {
NestedMeta::Meta(Meta::Path(ident)) => {
if ident.is_ident("lo") {
lo = true;
} else if ident.is_ident("hi") {
hi = true;
} else {
panic!("Unknown span attribute: {:?}", ident)
}
}
_ => panic!("Unknown span attribute"),
}
}
}
_ => panic!("Unknown span attribute"),
}
}
Self {
ident: f.ident.clone(),
ty: f.ty.clone(),
lo,
hi,
}
}
}
pub fn derive(input: DeriveInput) -> ItemImpl {
let arms = Binder::new_from(&input)
.variants()
@ -141,7 +177,7 @@ fn make_body_for_variant(v: &VariantBinder<'_>, bindings: Vec<BindedField<'_>>)
let fields: Vec<_> = bindings
.iter()
.map(|b| (b, MyField::from_field(b.field()).unwrap()))
.map(|b| (b, MyField::from_field(b.field())))
.collect();
// TODO: Only one field should be `#[span(lo)]`.