mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 13:51:19 +03:00
build(deps): Update syn
to v2
(#7557)
This commit is contained in:
parent
c9f2838071
commit
54410047fa
80
Cargo.lock
generated
80
Cargo.lock
generated
@ -96,9 +96,9 @@ checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
|
||||
|
||||
[[package]]
|
||||
name = "arrayvec"
|
||||
version = "0.7.2"
|
||||
version = "0.7.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
|
||||
checksum = "8868f09ff8cea88b079da74ae569d9b8c62a23c68c746240b704ee6f7525c89c"
|
||||
|
||||
[[package]]
|
||||
name = "ast_node"
|
||||
@ -108,7 +108,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -119,7 +119,7 @@ checksum = "b9ccdd8f2a161be9bd5c023df56f1b2a0bd1d83872ae53b71a84a12c9bf6e842"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.13",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1226,7 +1226,7 @@ dependencies = [
|
||||
"pmutil",
|
||||
"proc-macro2",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1297,7 +1297,7 @@ checksum = "89ca545a94061b6365f2c7355b4b32bd20df3ff95f02da9329b34ccc3bd6ee72"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.13",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -1657,15 +1657,15 @@ checksum = "30e22bd8629359895450b59ea7a776c850561b96a3b1d31321c1949d9e6c9146"
|
||||
|
||||
[[package]]
|
||||
name = "is-macro"
|
||||
version = "0.2.1"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1c068d4c6b922cd6284c609cfa6dec0e41615c9c5a1a4ba729a970d8daba05fb"
|
||||
checksum = "f4467ed1321b310c2625c5aa6c1b1ffc5de4d9e42668cf697a08fb033ee8265e"
|
||||
dependencies = [
|
||||
"Inflector",
|
||||
"pmutil",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2506,13 +2506,13 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "pmutil"
|
||||
version = "0.5.3"
|
||||
version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3894e5d549cccbe44afecf72922f277f603cd4bb0219c8342631ef18fffbe004"
|
||||
checksum = "52a40bc70c2c58040d2d8b167ba9a5ff59fc9dab7ad44771cfde3dcfde7a09c6"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -2606,9 +2606,9 @@ checksum = "dc375e1527247fe1a97d8b7156678dfe7c1af2fc075c9a4db3690ecd2a148068"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.56"
|
||||
version = "1.0.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2b63bdb0cd06f1f4dedf69b254734f9b45af66e4a031e42a7480257d9898b435"
|
||||
checksum = "dec2b086b7a862cf4de201096214fa870344cf922b2b30c167badb3af3195406"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
@ -2655,9 +2655,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.26"
|
||||
version = "1.0.28"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4424af4bf778aae2051a77b60283332f386554255d722233d09fbfc7e30da2fc"
|
||||
checksum = "1b9ab9c7eadfd8df19006f1cf1a4aed13540ed5cbc047010ece5826e10825488"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
@ -3248,9 +3248,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "st-map"
|
||||
version = "0.1.8"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f09d891835f076b0d4a58dd4478fb54d47aa3da1f7a4c6e89ad6c791357ab5ed"
|
||||
checksum = "f352d5d14be5a1f956d76ae0c8060c3487aaa2a080f10a4b4ff023c7c05a9047"
|
||||
dependencies = [
|
||||
"arrayvec",
|
||||
"static-map-macro",
|
||||
@ -3286,14 +3286,14 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "static-map-macro"
|
||||
version = "0.2.5"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b862d598fbc9f7085b017890e2e61433f501e7467f2c585323e1aa3c07ef8599"
|
||||
checksum = "7628ae0bd92555d3de4303da41a5c8b1c5363e892001325f34e4be9ed024d0d7"
|
||||
dependencies = [
|
||||
"pmutil",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -3386,7 +3386,7 @@ dependencies = [
|
||||
"quote",
|
||||
"serde",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -3611,7 +3611,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -3718,7 +3718,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -3902,7 +3902,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4094,7 +4094,7 @@ dependencies = [
|
||||
"swc_ecma_ast",
|
||||
"swc_ecma_parser",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4209,7 +4209,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4442,7 +4442,7 @@ dependencies = [
|
||||
"pmutil",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4563,7 +4563,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4641,7 +4641,7 @@ dependencies = [
|
||||
"pmutil",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4716,7 +4716,7 @@ version = "0.9.14"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4779,7 +4779,7 @@ version = "0.1.2"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4799,7 +4799,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4847,7 +4847,7 @@ dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"swc_macros_common",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -4887,9 +4887,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.13"
|
||||
version = "2.0.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c9da457c5285ac1f936ebd076af6dac17a61cfe7826f2076b4d015cf47bc8ec"
|
||||
checksum = "32d41677bcbe24c20c52e7c70b0d8db04134c5d1066bf98662e2871ad200ea3e"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
@ -4989,7 +4989,7 @@ dependencies = [
|
||||
"quote",
|
||||
"regex",
|
||||
"relative-path",
|
||||
"syn 1.0.109",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -5029,7 +5029,7 @@ checksum = "f9456a42c5b0d803c8cd86e73dd7cc9edd429499f37a3550d286d5e86720569f"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.13",
|
||||
"syn 2.0.18",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@ -5354,9 +5354,9 @@ checksum = "4285d92be83dfbc8950a2601178b89ed36f979ebf51bfcf7b272b17001184e6c"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.8"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e5464a87b239f13a63a501f2701565754bae92d243d4bb7eb12f6d57d2269bf4"
|
||||
checksum = "b15811caf2415fb889178633e7724bad2509101cde276048e013b9def5e51fa0"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-linebreak"
|
||||
|
@ -13,7 +13,7 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
|
||||
@ -21,4 +21,4 @@ swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
||||
[dependencies.syn]
|
||||
features = ["derive", "fold", "parsing", "printing", "visit-mut"]
|
||||
version = "1"
|
||||
version = "2"
|
||||
|
@ -7,17 +7,13 @@ use syn::{
|
||||
};
|
||||
|
||||
struct VariantAttr {
|
||||
_paren_token: token::Paren,
|
||||
tags: Punctuated<Lit, Token![,]>,
|
||||
}
|
||||
|
||||
impl Parse for VariantAttr {
|
||||
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
|
||||
let content;
|
||||
let _paren_token = parenthesized!(content in input);
|
||||
Ok(VariantAttr {
|
||||
_paren_token,
|
||||
tags: content.parse_terminated(Lit::parse)?,
|
||||
tags: input.call(Punctuated::parse_terminated)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -64,8 +60,13 @@ pub fn expand(
|
||||
if !is_attr_name(attr, "tag") {
|
||||
return None;
|
||||
}
|
||||
let tags =
|
||||
parse2(attr.tokens.clone()).expect("failed to parse #[tag] attribute");
|
||||
let tokens = match &attr.meta {
|
||||
Meta::List(meta) => meta.tokens.clone(),
|
||||
_ => {
|
||||
panic!("#[tag] attribute must be in form of #[tag(..)]")
|
||||
}
|
||||
};
|
||||
let tags = parse2(tokens).expect("failed to parse #[tag] attribute");
|
||||
|
||||
Some(tags)
|
||||
})
|
||||
@ -93,13 +94,15 @@ pub fn expand(
|
||||
|
||||
Arm {
|
||||
attrs: Default::default(),
|
||||
pat: q!(
|
||||
Vars {
|
||||
Variant: &variant.ident
|
||||
},
|
||||
(__TypeVariant::Variant)
|
||||
)
|
||||
.parse(),
|
||||
pat: Pat::Path(
|
||||
q!(
|
||||
Vars {
|
||||
Variant: &variant.ident
|
||||
},
|
||||
(__TypeVariant::Variant)
|
||||
)
|
||||
.parse(),
|
||||
),
|
||||
guard: Default::default(),
|
||||
fat_arrow_token: variant.ident.span().as_token(),
|
||||
body: q!(
|
||||
@ -136,8 +139,13 @@ pub fn expand(
|
||||
if !is_attr_name(attr, "tag") {
|
||||
return None;
|
||||
}
|
||||
let tags =
|
||||
parse2(attr.tokens.clone()).expect("failed to parse #[tag] attribute");
|
||||
let tokens = match &attr.meta {
|
||||
Meta::List(meta) => meta.tokens.clone(),
|
||||
_ => {
|
||||
panic!("#[tag] attribute must be in form of #[tag(..)]")
|
||||
}
|
||||
};
|
||||
let tags = parse2(tokens).expect("failed to parse #[tag] attribute");
|
||||
|
||||
Some(tags)
|
||||
})
|
||||
@ -176,20 +184,11 @@ pub fn expand(
|
||||
(
|
||||
Pat::Lit(PatLit {
|
||||
attrs: Default::default(),
|
||||
expr: Box::new(Expr::Lit(ExprLit {
|
||||
attrs: Default::default(),
|
||||
lit,
|
||||
})),
|
||||
lit,
|
||||
}),
|
||||
Pat::Lit(PatLit {
|
||||
attrs: Default::default(),
|
||||
expr: Box::new(Expr::Lit(ExprLit {
|
||||
attrs: Default::default(),
|
||||
lit: Lit::ByteStr(LitByteStr::new(
|
||||
s.as_bytes(),
|
||||
call_site(),
|
||||
)),
|
||||
})),
|
||||
lit: Lit::ByteStr(LitByteStr::new(s.as_bytes(), call_site())),
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
use pmutil::{smart_quote, Quote, ToTokensExt};
|
||||
use swc_macros_common::prelude::*;
|
||||
use syn::*;
|
||||
use syn::{parse::Parse, *};
|
||||
|
||||
struct MyField {
|
||||
/// Name of the field.
|
||||
@ -16,6 +16,18 @@ struct MyField {
|
||||
pub hi: bool,
|
||||
}
|
||||
|
||||
struct InputFieldAttr {
|
||||
kinds: Punctuated<Ident, Token![,]>,
|
||||
}
|
||||
|
||||
impl Parse for InputFieldAttr {
|
||||
fn parse(input: parse::ParseStream) -> Result<Self> {
|
||||
let kinds = input.call(Punctuated::parse_terminated)?;
|
||||
|
||||
Ok(Self { kinds })
|
||||
}
|
||||
}
|
||||
|
||||
impl MyField {
|
||||
fn from_field(f: &Field) -> Self {
|
||||
let mut lo = false;
|
||||
@ -26,21 +38,19 @@ impl MyField {
|
||||
continue;
|
||||
}
|
||||
|
||||
let meta = attr.parse_meta().unwrap();
|
||||
match meta {
|
||||
match &attr.meta {
|
||||
Meta::Path(..) => {}
|
||||
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"),
|
||||
let input = parse2::<InputFieldAttr>(list.tokens.clone())
|
||||
.expect("failed to parse as `InputFieldAttr`");
|
||||
|
||||
for kind in input.kinds {
|
||||
if kind == "lo" {
|
||||
lo = true
|
||||
} else if kind == "hi" {
|
||||
hi = true
|
||||
} else {
|
||||
panic!("Unknown span attribute: {:?}", kind)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -207,6 +217,10 @@ fn has_empty_span_attr(attrs: &[Attribute]) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
attr.tokens.is_empty()
|
||||
match &attr.meta {
|
||||
Meta::Path(..) => true,
|
||||
Meta::List(t) => t.tokens.is_empty(),
|
||||
_ => false,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -13,11 +13,11 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.3"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
||||
[dependencies.syn]
|
||||
features = ["derive", "fold", "parsing", "printing"]
|
||||
version = "1"
|
||||
version = "2"
|
||||
|
@ -18,7 +18,7 @@ dashmap = "5.1.0"
|
||||
once_cell = "1.12.0"
|
||||
semver = { version = "1.0.4", features = ["serde"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
st-map = "0.1.8"
|
||||
st-map = "0.2.0"
|
||||
tracing = { version = "0.1.32" }
|
||||
|
||||
from_variant = { version = "0.1.5", path = "../from_variant" }
|
||||
|
@ -13,14 +13,14 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
swc_macros_common = {version = "0.3.7", path = "../swc_macros_common"}
|
||||
|
||||
[dependencies.syn]
|
||||
features = ["full", "parsing", "printing", "extra-traits"]
|
||||
version = "1"
|
||||
version = "2"
|
||||
|
||||
[dev-dependencies]
|
||||
serde = "1"
|
||||
|
@ -5,7 +5,7 @@ extern crate proc_macro;
|
||||
use pmutil::{smart_quote, Quote};
|
||||
use quote::quote_spanned;
|
||||
use swc_macros_common::prelude::*;
|
||||
use syn::{self, *};
|
||||
use syn::{self, parse::Parse, *};
|
||||
|
||||
/// Creates `.as_str()` and then implements `Debug` and `Display` using it.
|
||||
///
|
||||
@ -132,52 +132,41 @@ fn make_from_str(i: &DeriveInput) -> ItemImpl {
|
||||
|
||||
let str_value = get_str_value(v.attrs());
|
||||
|
||||
let mut pat: Pat = Quote::new(def_site::<Span>())
|
||||
.quote_with(smart_quote!(Vars { str_value }, { str_value }))
|
||||
.parse();
|
||||
let mut pat: Pat = Pat::Lit(ExprLit {
|
||||
attrs: Default::default(),
|
||||
lit: Lit::Str(LitStr::new(&str_value, Span::call_site())),
|
||||
});
|
||||
|
||||
// Handle `string_enum(alias("foo"))`
|
||||
'outer: for attr in v
|
||||
for attr in v
|
||||
.attrs()
|
||||
.iter()
|
||||
.filter(|attr| is_attr_name(attr, "string_enum"))
|
||||
{
|
||||
let meta = attr.parse_meta().expect("failed to parse meta");
|
||||
if let Meta::List(meta) = &meta {
|
||||
for meta in &meta.nested {
|
||||
//
|
||||
if let NestedMeta::Meta(Meta::List(meta)) = meta {
|
||||
if meta.path.is_ident("alias") {
|
||||
let mut cases = Punctuated::default();
|
||||
if let Meta::List(meta) = &attr.meta {
|
||||
let mut cases = Punctuated::default();
|
||||
|
||||
cases.push(pat);
|
||||
cases.push(pat);
|
||||
|
||||
for lit in &meta.nested {
|
||||
cases.push(Pat::Lit(PatLit {
|
||||
attrs: Default::default(),
|
||||
expr: Box::new(Expr::Lit(ExprLit {
|
||||
attrs: Default::default(),
|
||||
lit: match lit {
|
||||
NestedMeta::Meta(_) => todo!(),
|
||||
NestedMeta::Lit(v) => v.clone(),
|
||||
},
|
||||
})),
|
||||
}))
|
||||
}
|
||||
|
||||
pat = Pat::Or(PatOr {
|
||||
attrs: Default::default(),
|
||||
leading_vert: None,
|
||||
cases,
|
||||
});
|
||||
|
||||
continue 'outer;
|
||||
}
|
||||
}
|
||||
for item in parse2::<FieldAttr>(meta.tokens.clone())
|
||||
.expect("failed to parse `#[string_enum]`")
|
||||
.aliases
|
||||
{
|
||||
cases.push(Pat::Lit(PatLit {
|
||||
attrs: Default::default(),
|
||||
lit: Lit::Str(item.alias),
|
||||
}));
|
||||
}
|
||||
|
||||
pat = Pat::Or(PatOr {
|
||||
attrs: Default::default(),
|
||||
leading_vert: None,
|
||||
cases,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
|
||||
panic!("Unsupported meta: {:#?}", meta);
|
||||
panic!("Unsupported meta: {:#?}", attr.meta);
|
||||
}
|
||||
|
||||
let body = match *v.data() {
|
||||
@ -266,11 +255,17 @@ fn make_as_str(i: &DeriveInput) -> ItemImpl {
|
||||
path: qual_name,
|
||||
attrs: Default::default(),
|
||||
})),
|
||||
_ => Box::new(
|
||||
Quote::new(def_site::<Span>())
|
||||
.quote_with(smart_quote!(Vars { qual_name }, { qual_name { .. } }))
|
||||
.parse(),
|
||||
),
|
||||
_ => Box::new(Pat::Struct(PatStruct {
|
||||
attrs: Default::default(),
|
||||
qself: None,
|
||||
path: qual_name,
|
||||
brace_token: Default::default(),
|
||||
fields: Default::default(),
|
||||
rest: Some(PatRest {
|
||||
attrs: Default::default(),
|
||||
dot2_token: def_site(),
|
||||
}),
|
||||
})),
|
||||
};
|
||||
|
||||
Arm {
|
||||
@ -381,3 +376,38 @@ fn make_deserialize(i: &DeriveInput) -> ItemImpl {
|
||||
.parse::<ItemImpl>()
|
||||
.with_generics(i.generics.clone())
|
||||
}
|
||||
|
||||
struct FieldAttr {
|
||||
aliases: Punctuated<FieldAttrItem, Token![,]>,
|
||||
}
|
||||
|
||||
impl Parse for FieldAttr {
|
||||
fn parse(input: parse::ParseStream) -> Result<Self> {
|
||||
Ok(Self {
|
||||
aliases: input.call(Punctuated::parse_terminated)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// `alias("text")` in `#[string_enum(alias("text"))]`.
|
||||
struct FieldAttrItem {
|
||||
alias: LitStr,
|
||||
}
|
||||
|
||||
impl Parse for FieldAttrItem {
|
||||
fn parse(input: parse::ParseStream) -> Result<Self> {
|
||||
let name: Ident = input.parse()?;
|
||||
|
||||
assert!(
|
||||
name == "alias",
|
||||
"#[derive(StringEnum) only supports `#[string_enum(alias(\"text\"))]]"
|
||||
);
|
||||
|
||||
let alias;
|
||||
parenthesized!(alias in input);
|
||||
|
||||
Ok(Self {
|
||||
alias: alias.parse()?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -29,7 +29,7 @@ anyhow = "1"
|
||||
crc = "2.1.0"
|
||||
dashmap = { version = "5.1.0", optional = true }
|
||||
indexmap = "1.6.1"
|
||||
is-macro = "0.2.0"
|
||||
is-macro = "0.3.0"
|
||||
once_cell = "1.10.0"
|
||||
parking_lot = "0.12.0"
|
||||
petgraph = "0.6"
|
||||
|
@ -13,9 +13,9 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.3"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = "1"
|
||||
syn = "2"
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
@ -50,14 +50,14 @@ fn call_merge_for_fields(obj: &dyn ToTokens, fields: &Fields) -> Vec<Stmt> {
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(idx, f)| call_merge(obj, idx, f))
|
||||
.map(|expr| Stmt::Semi(expr, fs.brace_token.span.as_token()))
|
||||
.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::Semi(expr, fs.paren_token.span.as_token()))
|
||||
.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"),
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ rkyv-impl = ["__rkyv", "rkyv", "swc_atoms/rkyv-impl", "swc_common/rkyv-impl"]
|
||||
serde-impl = ["serde"]
|
||||
|
||||
[dependencies]
|
||||
is-macro = "0.2.0"
|
||||
is-macro = "0.3.0"
|
||||
rkyv = { version = "=0.7.42", optional = true, features = [
|
||||
"strict",
|
||||
"validation",
|
||||
|
@ -13,9 +13,9 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["fold"] }
|
||||
syn = { version = "2", features = ["fold"] }
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
@ -3,20 +3,20 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use pmutil::{smart_quote, Quote, ToTokensExt};
|
||||
use syn::{FnArg, ImplItemMethod, Type, TypeReference};
|
||||
use syn::{FnArg, ImplItemFn, Type, TypeReference};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn emitter(
|
||||
_attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let item: ImplItemMethod = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item: ImplItemFn = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item = expand(item);
|
||||
|
||||
item.dump().into()
|
||||
}
|
||||
|
||||
fn expand(i: ImplItemMethod) -> ImplItemMethod {
|
||||
fn expand(i: ImplItemFn) -> ImplItemFn {
|
||||
let mtd_name = i.sig.ident.clone();
|
||||
assert!(
|
||||
format!("{}", i.sig.ident).starts_with("emit_"),
|
||||
@ -82,5 +82,5 @@ fn (&mut self, node: Node) -> Result;
|
||||
.parse()
|
||||
};
|
||||
|
||||
ImplItemMethod { block, ..i }
|
||||
ImplItemFn { block, ..i }
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ arbitrary = { version = "1", optional = true, features = ["derive"] }
|
||||
bitflags = "2.1.0"
|
||||
# bytecheck version should be in sync with rkyv version. Do not bump individually.
|
||||
bytecheck = { version = "0.6.10", optional = true }
|
||||
is-macro = "0.2.1"
|
||||
is-macro = "0.3.0"
|
||||
num-bigint = { version = "0.4", features = ["serde"] }
|
||||
rkyv = { package = "rkyv", version = "=0.7.42", optional = true, features = [
|
||||
"strict",
|
||||
|
@ -13,14 +13,14 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
||||
[dependencies.syn]
|
||||
features = ["fold"]
|
||||
version = "1"
|
||||
version = "2"
|
||||
|
||||
[dependencies.quote]
|
||||
version = "1"
|
||||
|
@ -9,14 +9,14 @@ mod fold;
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn emitter(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
||||
let item: ImplItemMethod = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item = fold::InjectSelf { parser: None }.fold_impl_item_method(item);
|
||||
let item: ImplItemFn = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item = fold::InjectSelf { parser: None }.fold_impl_item_fn(item);
|
||||
let item = expand(item);
|
||||
|
||||
print("emitter", item.dump())
|
||||
}
|
||||
|
||||
fn expand(i: ImplItemMethod) -> ImplItemMethod {
|
||||
fn expand(i: ImplItemFn) -> ImplItemFn {
|
||||
let mtd_name = i.sig.ident.clone();
|
||||
assert!(
|
||||
format!("{}", i.sig.ident).starts_with("emit_"),
|
||||
@ -83,5 +83,5 @@ fn (&mut self, node: Node) -> Result;
|
||||
.parse()
|
||||
};
|
||||
|
||||
ImplItemMethod { block, ..i }
|
||||
ImplItemFn { block, ..i }
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ once_cell = "1.10.0"
|
||||
semver = { version = "1.0.4", features = ["serde"] }
|
||||
serde = { version = "1", features = ["derive"], optional = true }
|
||||
serde_json = "1"
|
||||
st-map = "0.1.8"
|
||||
st-map = "0.2.0"
|
||||
|
||||
preset_env_base = { version = "0.4.2", path = "../preset_env_base" }
|
||||
string_enum = { version = "0.4.0", path = "../string_enum" }
|
||||
|
@ -14,10 +14,10 @@ proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = "1"
|
||||
syn = "2"
|
||||
|
||||
swc_atoms = { version = "0.5.6", path = "../swc_atoms" }
|
||||
swc_common = { version = "0.31.12", path = "../swc_common" }
|
||||
|
@ -194,7 +194,7 @@ where
|
||||
let mut stmts = vec![var_stmt];
|
||||
|
||||
for item in self {
|
||||
stmts.push(syn::Stmt::Semi(
|
||||
stmts.push(syn::Stmt::Expr(
|
||||
q!(
|
||||
Vars {
|
||||
item: item.to_code(cx)
|
||||
@ -202,11 +202,11 @@ where
|
||||
{ items.push(item) }
|
||||
)
|
||||
.parse(),
|
||||
Default::default(),
|
||||
Some(Default::default()),
|
||||
));
|
||||
}
|
||||
|
||||
stmts.push(syn::Stmt::Expr(q!(Vars {}, { items }).parse()));
|
||||
stmts.push(syn::Stmt::Expr(q!(Vars {}, { items }).parse(), None));
|
||||
|
||||
syn::Expr::Block(ExprBlock {
|
||||
attrs: Default::default(),
|
||||
|
@ -33,6 +33,7 @@ impl Builder {
|
||||
fields: self.fields,
|
||||
dot2_token: Default::default(),
|
||||
rest: Default::default(),
|
||||
qself: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ pub fn internal_quote(input: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||
brace_token: Default::default(),
|
||||
stmts: stmts
|
||||
.into_iter()
|
||||
.chain(once(syn::Stmt::Expr(expr_for_ast_creation)))
|
||||
.chain(once(syn::Stmt::Expr(expr_for_ast_creation, None)))
|
||||
.collect(),
|
||||
},
|
||||
})
|
||||
|
@ -23,7 +23,7 @@ concurrent = [
|
||||
ahash = "0.7.4"
|
||||
arrayvec = "0.7.2"
|
||||
indexmap = "1.6.1"
|
||||
is-macro = "0.2.0"
|
||||
is-macro = "0.3.0"
|
||||
num-bigint = "0.4"
|
||||
rayon = { version = "1.5.1", optional = true }
|
||||
serde = { version = "1.0.118", features = ["derive"] }
|
||||
|
@ -13,9 +13,9 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["fold", "parsing", "full"] }
|
||||
syn = { version = "2", features = ["fold", "parsing", "full"] }
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
@ -1,7 +1,7 @@
|
||||
use pmutil::q;
|
||||
use proc_macro2::TokenStream;
|
||||
use swc_macros_common::call_site;
|
||||
use syn::{FnArg, Ident, ImplItem, ImplItemMethod, ItemImpl, Pat, Path, Stmt};
|
||||
use syn::{FnArg, Ident, ImplItem, ImplItemFn, ItemImpl, Pat, Path, Stmt};
|
||||
|
||||
use crate::common::Mode;
|
||||
|
||||
@ -16,7 +16,7 @@ pub fn expand(attr: TokenStream, item: ItemImpl) -> ItemImpl {
|
||||
items: items
|
||||
.into_iter()
|
||||
.map(|item| match item {
|
||||
ImplItem::Method(m) => ImplItem::Method(expander.patch_method(m)),
|
||||
ImplItem::Fn(m) => ImplItem::Fn(expander.patch_method(m)),
|
||||
_ => item,
|
||||
})
|
||||
.collect(),
|
||||
@ -26,7 +26,7 @@ pub fn expand(attr: TokenStream, item: ItemImpl) -> ItemImpl {
|
||||
|
||||
fn detect_mode(i: &ItemImpl) -> Mode {
|
||||
if i.items.iter().any(|item| match item {
|
||||
ImplItem::Method(m) => m.sig.ident.to_string().starts_with("fold"),
|
||||
ImplItem::Fn(m) => m.sig.ident.to_string().starts_with("fold"),
|
||||
_ => false,
|
||||
}) {
|
||||
return Mode::Fold;
|
||||
@ -56,7 +56,7 @@ impl Expander {
|
||||
|
||||
for (name, ty) in list {
|
||||
let has = items.iter().any(|item| match item {
|
||||
ImplItem::Method(i) => i.sig.ident.to_string().ends_with(name),
|
||||
ImplItem::Fn(i) => i.sig.ident.to_string().ends_with(name),
|
||||
_ => false,
|
||||
});
|
||||
if has {
|
||||
@ -96,7 +96,7 @@ impl Expander {
|
||||
}
|
||||
|
||||
/// Add fast path to a method
|
||||
fn patch_method(&self, mut m: ImplItemMethod) -> ImplItemMethod {
|
||||
fn patch_method(&self, mut m: ImplItemFn) -> ImplItemFn {
|
||||
let ty_arg = m
|
||||
.sig
|
||||
.inputs
|
||||
|
@ -1,6 +1,6 @@
|
||||
use pmutil::q;
|
||||
use proc_macro2::{Span, TokenStream};
|
||||
use syn::{Expr, Ident, ImplItem, ImplItemMethod, ItemImpl, Meta, Type};
|
||||
use syn::{Expr, Ident, ImplItem, ImplItemFn, ItemImpl, Meta, Type};
|
||||
|
||||
use crate::common::Mode;
|
||||
|
||||
@ -25,13 +25,13 @@ pub fn expand(attr: TokenStream, mut item: ItemImpl) -> ItemImpl {
|
||||
.map(|v| v.path().is_ident("explode"))
|
||||
.unwrap_or(false);
|
||||
|
||||
item.items.push(ImplItem::Method(make_par_visit_method(
|
||||
item.items.push(ImplItem::Fn(make_par_visit_method(
|
||||
mode,
|
||||
"module_items",
|
||||
explode,
|
||||
100,
|
||||
)));
|
||||
item.items.push(ImplItem::Method(make_par_visit_method(
|
||||
item.items.push(ImplItem::Fn(make_par_visit_method(
|
||||
mode, "stmts", explode, 100,
|
||||
)));
|
||||
|
||||
@ -85,12 +85,7 @@ fn explode_hook_method_name(explode: bool, suffix: &str) -> Option<Ident> {
|
||||
}
|
||||
}
|
||||
|
||||
fn make_par_visit_method(
|
||||
mode: Mode,
|
||||
suffix: &str,
|
||||
explode: bool,
|
||||
threshold: usize,
|
||||
) -> ImplItemMethod {
|
||||
fn make_par_visit_method(mode: Mode, suffix: &str, explode: bool, threshold: usize) -> ImplItemFn {
|
||||
let method_name = Ident::new(&format!("{}_{}", mode.prefix(), suffix), Span::call_site());
|
||||
let hook = post_visit_hook(mode, suffix);
|
||||
let explode_method_name = explode_hook_method_name(explode, suffix);
|
||||
|
@ -18,7 +18,7 @@ ahash = "0.7.4"
|
||||
anyhow = "1.0.41"
|
||||
bitflags = "2.1.0"
|
||||
indexmap = "1.6.1"
|
||||
is-macro = "0.2.0"
|
||||
is-macro = "0.3.0"
|
||||
path-clean = "0.1.0"
|
||||
pathdiff = "0.2.0"
|
||||
regex = "1"
|
||||
|
@ -13,7 +13,7 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.3"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1.0.24"
|
||||
quote = "1.0.7"
|
||||
syn = { version = "1", features = ["full"] }
|
||||
syn = { version = "2", features = ["full"] }
|
||||
|
@ -2,8 +2,8 @@ use pmutil::{q, IdentExt, SpanExt};
|
||||
use proc_macro2::Span;
|
||||
use syn::{
|
||||
parse, punctuated::Punctuated, spanned::Spanned, Arm, BinOp, Block, Data, DeriveInput, Expr,
|
||||
ExprBinary, ExprBlock, Field, FieldPat, Fields, Ident, Index, Member, Pat, PatIdent, PatStruct,
|
||||
PatTuple, Path, Stmt, Token,
|
||||
ExprBinary, ExprBlock, Field, FieldPat, Fields, Ident, Index, Member, Pat, PatIdent, PatRest,
|
||||
PatStruct, PatTuple, Path, Stmt, Token,
|
||||
};
|
||||
|
||||
/// Derives `swc_common::TypeEq`.
|
||||
@ -18,7 +18,7 @@ pub fn derive_type_eq(item: proc_macro::TokenStream) -> proc_macro::TokenStream
|
||||
ignore_field: Box::new(|field| {
|
||||
// Search for `#[not_type]`.
|
||||
for attr in &field.attrs {
|
||||
if attr.path.is_ident("not_type") {
|
||||
if attr.path().is_ident("not_type") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -121,21 +121,20 @@ impl Deriver {
|
||||
.enumerate()
|
||||
.filter(|(_, f)| !(self.ignore_field)(f))
|
||||
{
|
||||
let method_name = if field
|
||||
.attrs
|
||||
.iter()
|
||||
.any(|attr| attr.path.is_ident("not_spanned") || attr.path.is_ident("use_eq"))
|
||||
{
|
||||
Ident::new("eq", Span::call_site())
|
||||
} else if field
|
||||
.attrs
|
||||
.iter()
|
||||
.any(|attr| attr.path.is_ident("use_eq_ignore_span"))
|
||||
{
|
||||
Ident::new("eq_ignore_span", Span::call_site())
|
||||
} else {
|
||||
self.method_name.clone()
|
||||
};
|
||||
let method_name =
|
||||
if field.attrs.iter().any(|attr| {
|
||||
attr.path().is_ident("not_spanned") || attr.path().is_ident("use_eq")
|
||||
}) {
|
||||
Ident::new("eq", Span::call_site())
|
||||
} else if field
|
||||
.attrs
|
||||
.iter()
|
||||
.any(|attr| attr.path().is_ident("use_eq_ignore_span"))
|
||||
{
|
||||
Ident::new("eq_ignore_span", Span::call_site())
|
||||
} else {
|
||||
self.method_name.clone()
|
||||
};
|
||||
|
||||
let base = field
|
||||
.ident
|
||||
@ -201,17 +200,25 @@ impl Deriver {
|
||||
let mut elems = Punctuated::default();
|
||||
elems.push(Pat::Struct(PatStruct {
|
||||
attrs: Default::default(),
|
||||
qself: None,
|
||||
path: pat_path.clone(),
|
||||
brace_token: Span::call_site().as_token(),
|
||||
fields: l_pat_fields,
|
||||
dot2_token: Some(Span::call_site().as_token()),
|
||||
rest: Some(PatRest {
|
||||
attrs: Default::default(),
|
||||
dot2_token: Span::call_site().as_token(),
|
||||
}),
|
||||
}));
|
||||
elems.push(Pat::Struct(PatStruct {
|
||||
attrs: Default::default(),
|
||||
qself: None,
|
||||
path: pat_path,
|
||||
brace_token: Span::call_site().as_token(),
|
||||
fields: r_pat_fields,
|
||||
dot2_token: Some(Span::call_site().as_token()),
|
||||
rest: Some(PatRest {
|
||||
attrs: Default::default(),
|
||||
dot2_token: Span::call_site().as_token(),
|
||||
}),
|
||||
}));
|
||||
elems
|
||||
},
|
||||
@ -223,7 +230,7 @@ impl Deriver {
|
||||
label: Default::default(),
|
||||
block: Block {
|
||||
brace_token: Span::call_site().as_token(),
|
||||
stmts: vec![Stmt::Expr(expr)],
|
||||
stmts: vec![Stmt::Expr(expr, None)],
|
||||
},
|
||||
})),
|
||||
comma: Default::default(),
|
||||
|
@ -20,7 +20,7 @@ rkyv-impl = ["__rkyv", "rkyv", "swc_atoms/rkyv-impl", "swc_common/rkyv-impl"]
|
||||
serde-impl = ["serde"]
|
||||
|
||||
[dependencies]
|
||||
is-macro = "0.2.0"
|
||||
is-macro = "0.3.0"
|
||||
rkyv = { version = "=0.7.42", optional = true, features = [
|
||||
"strict",
|
||||
"validation",
|
||||
|
@ -16,9 +16,9 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["fold"] }
|
||||
syn = { version = "2", features = ["fold"] }
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
@ -3,20 +3,20 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use pmutil::{smart_quote, Quote, ToTokensExt};
|
||||
use syn::{FnArg, ImplItemMethod, Type, TypeReference};
|
||||
use syn::{FnArg, ImplItemFn, Type, TypeReference};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn emitter(
|
||||
_attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let item: ImplItemMethod = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item: ImplItemFn = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item = expand(item);
|
||||
|
||||
item.dump().into()
|
||||
}
|
||||
|
||||
fn expand(i: ImplItemMethod) -> ImplItemMethod {
|
||||
fn expand(i: ImplItemFn) -> ImplItemFn {
|
||||
let mtd_name = i.sig.ident.clone();
|
||||
assert!(
|
||||
format!("{}", i.sig.ident).starts_with("emit_"),
|
||||
@ -82,5 +82,5 @@ fn (&mut self, node: Node) -> Result;
|
||||
.parse()
|
||||
};
|
||||
|
||||
ImplItemMethod { block, ..i }
|
||||
ImplItemFn { block, ..i }
|
||||
}
|
||||
|
@ -12,10 +12,10 @@ version = "0.3.7"
|
||||
bench = false
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
|
||||
[dependencies.syn]
|
||||
features = ["derive", "visit", "parsing", "full", "printing", "extra-traits"]
|
||||
version = "1"
|
||||
version = "2"
|
||||
|
@ -204,11 +204,12 @@ impl<'a> VariantBinder<'a> {
|
||||
.collect();
|
||||
// EnumName::VariantName { fields }
|
||||
let pat = Pat::Struct(PatStruct {
|
||||
path,
|
||||
fields,
|
||||
brace_token,
|
||||
dot2_token: None,
|
||||
attrs: Default::default(),
|
||||
qself: None,
|
||||
path,
|
||||
brace_token,
|
||||
fields,
|
||||
rest: None,
|
||||
});
|
||||
(pat, bindings)
|
||||
}
|
||||
@ -248,13 +249,11 @@ impl<'a> VariantBinder<'a> {
|
||||
.collect();
|
||||
// EnumName::VariantName ( fields )
|
||||
let pat = Pat::TupleStruct(PatTupleStruct {
|
||||
path,
|
||||
pat: PatTuple {
|
||||
elems: pats,
|
||||
paren_token,
|
||||
attrs: Default::default(),
|
||||
},
|
||||
attrs: Default::default(),
|
||||
qself: None,
|
||||
path,
|
||||
paren_token,
|
||||
elems: pats,
|
||||
});
|
||||
(pat, bindings)
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ extern crate proc_macro;
|
||||
|
||||
#[cfg(procmacro2_semver_exempt)]
|
||||
use pmutil::SpanExt;
|
||||
use pmutil::{q, synom_ext::FromSpan, Quote, SpanExt};
|
||||
use pmutil::{prelude::*, synom_ext::FromSpan, Quote, SpanExt};
|
||||
use proc_macro2::Span;
|
||||
use quote::ToTokens;
|
||||
use syn::*;
|
||||
@ -44,28 +44,21 @@ pub fn print(attr: &'static str, tokens: proc_macro2::TokenStream) -> proc_macro
|
||||
}
|
||||
|
||||
pub fn is_attr_name(attr: &Attribute, name: &str) -> bool {
|
||||
match *attr {
|
||||
Attribute {
|
||||
path:
|
||||
Path {
|
||||
leading_colon: None,
|
||||
ref segments,
|
||||
},
|
||||
..
|
||||
} if segments.len() == 1 => segments.first().unwrap().ident == name,
|
||||
_ => false,
|
||||
}
|
||||
attr.path().is_ident(name)
|
||||
}
|
||||
|
||||
/// Returns `None` if `attr` is not a doc attribute.
|
||||
pub fn doc_str(attr: &Attribute) -> Option<String> {
|
||||
fn parse_tts(attr: &Attribute) -> String {
|
||||
let meta = attr.parse_meta().ok();
|
||||
match meta {
|
||||
Some(Meta::NameValue(MetaNameValue {
|
||||
lit: Lit::Str(s), ..
|
||||
})) => s.value(),
|
||||
_ => panic!("failed to parse {}", attr.tokens),
|
||||
match &attr.meta {
|
||||
Meta::NameValue(MetaNameValue {
|
||||
value:
|
||||
Expr::Lit(ExprLit {
|
||||
lit: Lit::Str(s), ..
|
||||
}),
|
||||
..
|
||||
}) => s.value(),
|
||||
_ => panic!("failed to parse {:?}", attr.meta),
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,13 +71,7 @@ pub fn doc_str(attr: &Attribute) -> Option<String> {
|
||||
|
||||
/// Creates a doc comment.
|
||||
pub fn make_doc_attr(s: &str) -> Attribute {
|
||||
Attribute {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: Ident::new("doc", def_site()).into(),
|
||||
tokens: q!(Vars { s },{ = s }).into(),
|
||||
}
|
||||
comment(s)
|
||||
}
|
||||
|
||||
pub fn access_field(obj: &dyn ToTokens, idx: usize, f: &Field) -> Expr {
|
||||
|
@ -21,7 +21,7 @@ swc_v2 = ["serde-impl"]
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
dashmap = "5.1.0"
|
||||
is-macro = "0.2.0"
|
||||
is-macro = "0.3.0"
|
||||
once_cell = "1.10.0"
|
||||
regex = "1"
|
||||
serde = { version = "1", features = ["derive"], optional = true }
|
||||
|
@ -15,4 +15,4 @@ proc-macro = true
|
||||
[dependencies]
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["full"] }
|
||||
syn = { version = "2", features = ["full"] }
|
||||
|
@ -9,7 +9,8 @@ repository = "https://github.com/swc-project/swc.git"
|
||||
version = "0.97.3"
|
||||
|
||||
[lib]
|
||||
bench = false
|
||||
bench = false
|
||||
doctest = false
|
||||
|
||||
[features]
|
||||
default = ["filesystem_cache", "plugin_transform_host_native"]
|
||||
|
@ -8,13 +8,13 @@ use swc_core::{
|
||||
pub fn process_transform(program: Program, metadata: TransformPluginProgramMetadata) -> Program {
|
||||
for i in 1..50000 {
|
||||
let j: u32 = i;
|
||||
println!("i {} j {}", i, j);
|
||||
// println!("i {} j {}", i, j);
|
||||
let res = metadata.source_map.span_to_snippet(Span::new(
|
||||
BytePos(j),
|
||||
BytePos(j + 1000),
|
||||
SyntaxContext::empty(),
|
||||
));
|
||||
let _ = dbg!(res);
|
||||
// let _ = dbg!(res);
|
||||
}
|
||||
|
||||
program
|
||||
|
@ -15,4 +15,4 @@ proc-macro = true
|
||||
[dependencies]
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = {version = "1", features = ["full"]}
|
||||
syn = {version = "2", features = ["full"]}
|
||||
|
@ -18,14 +18,13 @@ pub fn swc_trace(
|
||||
|
||||
item.items.iter_mut().for_each(|item| {
|
||||
// We only handle methods
|
||||
if let ImplItem::Method(m) = item {
|
||||
if let ImplItem::Fn(m) = item {
|
||||
// #[tracing::instrument(level = "info", skip_all)]
|
||||
let attr = Attribute {
|
||||
pound_token: Default::default(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: Default::default(),
|
||||
path: parse_quote!(tracing::instrument),
|
||||
tokens: parse_quote!((level = "info", skip_all)),
|
||||
meta: parse_quote!(tracing::instrument(level = "info", skip_all)),
|
||||
};
|
||||
m.attrs.push(attr);
|
||||
}
|
||||
|
@ -15,9 +15,9 @@ proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
Inflector = "0.11.4"
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["parsing", "full"] }
|
||||
syn = { version = "2", features = ["parsing", "full"] }
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
@ -7,13 +7,13 @@ use pmutil::{q, Quote, SpanExt};
|
||||
use proc_macro2::Ident;
|
||||
use swc_macros_common::{call_site, def_site, make_doc_attr};
|
||||
use syn::{
|
||||
parse_quote::parse, punctuated::Punctuated, spanned::Spanned, Arm, AttrStyle, Attribute, Block,
|
||||
Expr, ExprBlock, ExprCall, ExprMatch, ExprMethodCall, ExprPath, ExprUnary, Field, FieldValue,
|
||||
Fields, FieldsUnnamed, FnArg, GenericArgument, GenericParam, Generics, ImplItem,
|
||||
ImplItemMethod, Index, Item, ItemEnum, ItemImpl, ItemMod, ItemStruct, ItemTrait, ItemUse,
|
||||
Lifetime, LifetimeDef, Member, Pat, PatIdent, PatTuple, PatTupleStruct, PatType, PatWild, Path,
|
||||
PathArguments, Receiver, ReturnType, Signature, Stmt, Token, TraitItem, TraitItemMethod, Type,
|
||||
TypePath, TypeReference, UnOp, UseTree, Variant, VisPublic, Visibility,
|
||||
parse_macro_input, parse_quote, punctuated::Punctuated, spanned::Spanned, Arm, AttrStyle,
|
||||
Attribute, Block, Expr, ExprBlock, ExprCall, ExprMatch, ExprMethodCall, ExprPath, ExprUnary,
|
||||
Field, FieldMutability, FieldPat, Fields, FieldsUnnamed, FnArg, GenericArgument, GenericParam,
|
||||
Generics, ImplItem, ImplItemFn, Index, Item, ItemEnum, ItemImpl, ItemMod, ItemStruct,
|
||||
ItemTrait, ItemUse, Lifetime, LifetimeParam, Member, Pat, PatIdent, PatStruct, PatTupleStruct,
|
||||
PatType, PatWild, Path, PathArguments, ReturnType, Signature, Stmt, Token, TraitItem,
|
||||
TraitItemFn, Type, TypePath, TypeReference, UnOp, UseTree, Variant, Visibility,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
@ -94,7 +94,7 @@ impl Mode {
|
||||
/// - create `Visit`, `VisitAll`, `VisitMut`, `Fold`
|
||||
#[proc_macro]
|
||||
pub fn define(tts: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
let block: Block = parse(tts.into());
|
||||
let block = parse_macro_input!(tts as Block);
|
||||
|
||||
let mut q = Quote::new_call_site();
|
||||
q.push_tokens(&q!({
|
||||
@ -156,13 +156,12 @@ pub fn define(tts: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
||||
attrs: vec![make_doc_attr(
|
||||
"This module contains enums representing fields of each types",
|
||||
)],
|
||||
vis: Visibility::Public(VisPublic {
|
||||
pub_token: def_site(),
|
||||
}),
|
||||
vis: Visibility::Public(def_site()),
|
||||
mod_token: def_site(),
|
||||
ident: Ident::new("fields", call_site()),
|
||||
content: Some((def_site(), field_module_body)),
|
||||
semi: None,
|
||||
unsafety: None,
|
||||
});
|
||||
|
||||
proc_macro2::TokenStream::from(q).into()
|
||||
@ -178,6 +177,7 @@ fn make_field_enum_variant_from_named_field(type_name: &Ident, f: &Field) -> Var
|
||||
ident: None,
|
||||
colon_token: None,
|
||||
ty: q!({ usize }).parse(),
|
||||
mutability: FieldMutability::None,
|
||||
});
|
||||
|
||||
Fields::Unnamed(FieldsUnnamed {
|
||||
@ -263,8 +263,9 @@ fn make_field_enum(item: &Item) -> Vec<Item> {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ derive }).parse(),
|
||||
tokens: q!({ (Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash) }).into(),
|
||||
meta: parse_quote!(derive(
|
||||
Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash
|
||||
)),
|
||||
});
|
||||
|
||||
attrs.push(make_doc_attr(&format!(
|
||||
@ -276,21 +277,15 @@ fn make_field_enum(item: &Item) -> Vec<Item> {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ cfg_attr }).parse(),
|
||||
tokens: q!({
|
||||
(
|
||||
feature = "serde",
|
||||
derive(serde::Serialize, serde::Deserialize),
|
||||
)
|
||||
})
|
||||
.into(),
|
||||
meta: parse_quote!(cfg_attr(
|
||||
feature = "serde",
|
||||
derive(serde::Serialize, serde::Deserialize),
|
||||
)),
|
||||
});
|
||||
|
||||
items.push(Item::Enum(ItemEnum {
|
||||
attrs,
|
||||
vis: Visibility::Public(VisPublic {
|
||||
pub_token: def_site(),
|
||||
}),
|
||||
vis: Visibility::Public(def_site()),
|
||||
enum_token: def_site(),
|
||||
ident: name.clone(),
|
||||
generics: Default::default(),
|
||||
@ -302,11 +297,9 @@ fn make_field_enum(item: &Item) -> Vec<Item> {
|
||||
{
|
||||
let mut methods = vec![];
|
||||
|
||||
methods.push(ImplItem::Method(ImplItemMethod {
|
||||
attrs: vec![make_doc_attr("This is not consdered as a public API")],
|
||||
vis: Visibility::Public(VisPublic {
|
||||
pub_token: def_site(),
|
||||
}),
|
||||
methods.push(ImplItem::Fn(ImplItemFn {
|
||||
attrs: vec![make_doc_attr("This is not considered as a public API")],
|
||||
vis: Visibility::Public(def_site()),
|
||||
defaultness: Default::default(),
|
||||
sig: Signature {
|
||||
constness: Default::default(),
|
||||
@ -319,12 +312,7 @@ fn make_field_enum(item: &Item) -> Vec<Item> {
|
||||
paren_token: name.span().as_token(),
|
||||
inputs: {
|
||||
let mut v = Punctuated::new();
|
||||
v.push(FnArg::Receiver(Receiver {
|
||||
attrs: Default::default(),
|
||||
reference: Some((def_site(), None)),
|
||||
mutability: Some(def_site()),
|
||||
self_token: def_site(),
|
||||
}));
|
||||
v.push(FnArg::Receiver(parse_quote!(&mut self)));
|
||||
v.push(FnArg::Typed(PatType {
|
||||
attrs: Default::default(),
|
||||
colon_token: def_site(),
|
||||
@ -362,6 +350,7 @@ fn make_field_enum(item: &Item) -> Vec<Item> {
|
||||
attrs: Default::default(),
|
||||
pat: Pat::TupleStruct(PatTupleStruct {
|
||||
attrs: Default::default(),
|
||||
qself: None,
|
||||
path: q!(
|
||||
Vars {
|
||||
VariantName: &variant_name
|
||||
@ -369,16 +358,19 @@ fn make_field_enum(item: &Item) -> Vec<Item> {
|
||||
{ Self::VariantName }
|
||||
)
|
||||
.parse(),
|
||||
pat: PatTuple {
|
||||
attrs: Default::default(),
|
||||
paren_token: name.span().as_token(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
paren_token: name.span().as_token(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
|
||||
v.push(q!({ idx }).parse());
|
||||
v.push(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
by_ref: None,
|
||||
mutability: None,
|
||||
ident: Ident::new("idx", name.span()),
|
||||
subpat: None,
|
||||
}));
|
||||
|
||||
v
|
||||
},
|
||||
v
|
||||
},
|
||||
}),
|
||||
guard: Default::default(),
|
||||
@ -409,7 +401,7 @@ fn make_field_enum(item: &Item) -> Vec<Item> {
|
||||
|
||||
Block {
|
||||
brace_token: def_site(),
|
||||
stmts: vec![Stmt::Expr(expr)],
|
||||
stmts: vec![Stmt::Expr(expr, None)],
|
||||
}
|
||||
},
|
||||
}));
|
||||
@ -476,6 +468,7 @@ fn make_ast_enum(stmts: &[Stmt], is_ref: bool) -> Item {
|
||||
path: name.clone().into(),
|
||||
})),
|
||||
}),
|
||||
mutability: FieldMutability::None,
|
||||
});
|
||||
}
|
||||
|
||||
@ -494,6 +487,7 @@ fn make_ast_enum(stmts: &[Stmt], is_ref: bool) -> Item {
|
||||
)
|
||||
.parse(),
|
||||
}),
|
||||
mutability: FieldMutability::None,
|
||||
});
|
||||
|
||||
Fields::Unnamed(FieldsUnnamed {
|
||||
@ -515,45 +509,36 @@ fn make_ast_enum(stmts: &[Stmt], is_ref: bool) -> Item {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ derive }).parse(),
|
||||
tokens: q!({ (Debug, Copy, Clone, PartialEq) }).into(),
|
||||
meta: parse_quote!(derive(Debug, Copy, Clone, PartialEq)),
|
||||
});
|
||||
if !is_ref {
|
||||
attrs.push(Attribute {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ derive }).parse(),
|
||||
tokens: q!({ (Eq, PartialOrd, Ord, Hash) }).into(),
|
||||
meta: parse_quote!(derive(Eq, PartialOrd, Ord, Hash)),
|
||||
});
|
||||
|
||||
attrs.push(Attribute {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ cfg_attr }).parse(),
|
||||
tokens: q!({
|
||||
(
|
||||
feature = "serde",
|
||||
derive(serde::Serialize, serde::Deserialize),
|
||||
)
|
||||
})
|
||||
.into(),
|
||||
meta: parse_quote!(cfg_attr(
|
||||
feature = "serde",
|
||||
derive(serde::Serialize, serde::Deserialize),
|
||||
)),
|
||||
});
|
||||
}
|
||||
attrs.push(Attribute {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ allow }).parse(),
|
||||
tokens: q!({ (clippy::derive_partial_eq_without_eq) }).into(),
|
||||
meta: parse_quote!(allow(clippy::derive_partial_eq_without_eq)),
|
||||
});
|
||||
|
||||
Item::Enum(ItemEnum {
|
||||
attrs,
|
||||
vis: Visibility::Public(VisPublic {
|
||||
pub_token: def_site(),
|
||||
}),
|
||||
vis: Visibility::Public(def_site()),
|
||||
enum_token: def_site(),
|
||||
ident: if is_ref {
|
||||
Ident::new("AstParentNodeRef", call_site())
|
||||
@ -562,7 +547,7 @@ fn make_ast_enum(stmts: &[Stmt], is_ref: bool) -> Item {
|
||||
},
|
||||
generics: if is_ref {
|
||||
let mut g = Punctuated::new();
|
||||
g.push(GenericParam::Lifetime(LifetimeDef {
|
||||
g.push(GenericParam::Lifetime(LifetimeParam {
|
||||
attrs: Default::default(),
|
||||
lifetime: Lifetime {
|
||||
apostrophe: call_site(),
|
||||
@ -592,7 +577,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl {
|
||||
path: Ident::new("AstParentKind", call_site()).into(),
|
||||
});
|
||||
|
||||
let set_index_item = ImplItem::Method(ImplItemMethod {
|
||||
let set_index_item = ImplItem::Fn(ImplItemFn {
|
||||
attrs: Default::default(),
|
||||
vis: Visibility::Inherited,
|
||||
defaultness: Default::default(),
|
||||
@ -607,12 +592,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl {
|
||||
paren_token: def_site(),
|
||||
inputs: {
|
||||
let mut v = Punctuated::new();
|
||||
v.push(FnArg::Receiver(Receiver {
|
||||
attrs: Default::default(),
|
||||
reference: Some((def_site(), None)),
|
||||
mutability: Some(def_site()),
|
||||
self_token: def_site(),
|
||||
}));
|
||||
v.push(FnArg::Receiver(parse_quote!(&mut self)));
|
||||
v.push(FnArg::Typed(PatType {
|
||||
attrs: Default::default(),
|
||||
colon_token: def_site(),
|
||||
@ -659,21 +639,19 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl {
|
||||
pat: Pat::TupleStruct(PatTupleStruct {
|
||||
attrs: Default::default(),
|
||||
path: q!(Vars { name }, { Self::name }).parse(),
|
||||
pat: PatTuple {
|
||||
attrs: Default::default(),
|
||||
paren_token: def_site(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
v.push(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
by_ref: Default::default(),
|
||||
mutability: Default::default(),
|
||||
ident: Ident::new("v", name.span()),
|
||||
subpat: Default::default(),
|
||||
}));
|
||||
qself: None,
|
||||
paren_token: def_site(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
v.push(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
by_ref: Default::default(),
|
||||
mutability: Default::default(),
|
||||
ident: Ident::new("v", name.span()),
|
||||
subpat: Default::default(),
|
||||
}));
|
||||
|
||||
v
|
||||
},
|
||||
v
|
||||
},
|
||||
}),
|
||||
guard: Default::default(),
|
||||
@ -691,7 +669,7 @@ fn make_impl_parent_kind(stmts: &[Stmt]) -> ItemImpl {
|
||||
arms,
|
||||
});
|
||||
|
||||
vec![Stmt::Expr(match_expr)]
|
||||
vec![Stmt::Expr(match_expr, None)]
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -715,11 +693,9 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option<ItemImpl> {
|
||||
path: Ident::new("AstParentKind", call_site()).into(),
|
||||
});
|
||||
|
||||
let kind_item = ImplItem::Method(ImplItemMethod {
|
||||
let kind_item = ImplItem::Fn(ImplItemFn {
|
||||
attrs: Default::default(),
|
||||
vis: Visibility::Public(VisPublic {
|
||||
pub_token: def_site(),
|
||||
}),
|
||||
vis: Visibility::Public(def_site()),
|
||||
defaultness: Default::default(),
|
||||
sig: Signature {
|
||||
constness: Default::default(),
|
||||
@ -732,12 +708,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option<ItemImpl> {
|
||||
paren_token: def_site(),
|
||||
inputs: {
|
||||
let mut v = Punctuated::new();
|
||||
v.push(FnArg::Receiver(Receiver {
|
||||
attrs: Default::default(),
|
||||
reference: Some((def_site(), None)),
|
||||
mutability: None,
|
||||
self_token: def_site(),
|
||||
}));
|
||||
v.push(FnArg::Receiver(parse_quote!(&self)));
|
||||
|
||||
v
|
||||
},
|
||||
@ -772,28 +743,26 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option<ItemImpl> {
|
||||
let pat = Pat::TupleStruct(PatTupleStruct {
|
||||
attrs: Default::default(),
|
||||
path: q!(Vars { Name: &name }, (Self::Name)).parse(),
|
||||
pat: PatTuple {
|
||||
attrs: Default::default(),
|
||||
paren_token: def_site(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
qself: None,
|
||||
paren_token: def_site(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
|
||||
// Ignore node ref itself
|
||||
v.push(Pat::Wild(PatWild {
|
||||
attrs: Default::default(),
|
||||
underscore_token: stmt.span().as_token(),
|
||||
}));
|
||||
// Ignore node ref itself
|
||||
v.push(Pat::Wild(PatWild {
|
||||
attrs: Default::default(),
|
||||
underscore_token: stmt.span().as_token(),
|
||||
}));
|
||||
|
||||
v.push(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
ident: field_kind.clone(),
|
||||
subpat: None,
|
||||
by_ref: Default::default(),
|
||||
mutability: Default::default(),
|
||||
}));
|
||||
v.push(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
ident: field_kind.clone(),
|
||||
subpat: None,
|
||||
by_ref: Default::default(),
|
||||
mutability: Default::default(),
|
||||
}));
|
||||
|
||||
v
|
||||
},
|
||||
v
|
||||
},
|
||||
});
|
||||
|
||||
@ -838,12 +807,12 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option<ItemImpl> {
|
||||
arms,
|
||||
});
|
||||
|
||||
vec![Stmt::Expr(expr)]
|
||||
vec![Stmt::Expr(expr, None)]
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
let set_index_item = ImplItem::Method(ImplItemMethod {
|
||||
let set_index_item = ImplItem::Fn(ImplItemFn {
|
||||
attrs: Default::default(),
|
||||
vis: Visibility::Inherited,
|
||||
defaultness: Default::default(),
|
||||
@ -858,12 +827,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option<ItemImpl> {
|
||||
paren_token: def_site(),
|
||||
inputs: {
|
||||
let mut v = Punctuated::new();
|
||||
v.push(FnArg::Receiver(Receiver {
|
||||
attrs: Default::default(),
|
||||
reference: Some((def_site(), None)),
|
||||
mutability: Some(def_site()),
|
||||
self_token: def_site(),
|
||||
}));
|
||||
v.push(FnArg::Receiver(parse_quote!(&mut self)));
|
||||
v.push(FnArg::Typed(PatType {
|
||||
attrs: Default::default(),
|
||||
colon_token: def_site(),
|
||||
@ -909,29 +873,27 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option<ItemImpl> {
|
||||
|
||||
let pat = Pat::TupleStruct(PatTupleStruct {
|
||||
attrs: Default::default(),
|
||||
qself: None,
|
||||
path: q!(Vars { Name: &name }, (Self::Name)).parse(),
|
||||
pat: PatTuple {
|
||||
attrs: Default::default(),
|
||||
paren_token: def_site(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
paren_token: def_site(),
|
||||
elems: {
|
||||
let mut v = Punctuated::new();
|
||||
|
||||
// Ignore node ref itself
|
||||
v.push(Pat::Wild(PatWild {
|
||||
attrs: Default::default(),
|
||||
underscore_token: stmt.span().as_token(),
|
||||
}));
|
||||
// Ignore node ref itself
|
||||
v.push(Pat::Wild(PatWild {
|
||||
attrs: Default::default(),
|
||||
underscore_token: stmt.span().as_token(),
|
||||
}));
|
||||
|
||||
v.push(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
ident: field_kind.clone(),
|
||||
subpat: None,
|
||||
by_ref: Default::default(),
|
||||
mutability: Default::default(),
|
||||
}));
|
||||
v.push(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
ident: field_kind.clone(),
|
||||
subpat: None,
|
||||
by_ref: Default::default(),
|
||||
mutability: Default::default(),
|
||||
}));
|
||||
|
||||
v
|
||||
},
|
||||
v
|
||||
},
|
||||
});
|
||||
|
||||
@ -953,7 +915,7 @@ fn make_impl_kind_for_node_ref(stmts: &[Stmt]) -> Option<ItemImpl> {
|
||||
arms,
|
||||
});
|
||||
|
||||
vec![Stmt::Expr(match_expr)]
|
||||
vec![Stmt::Expr(match_expr, None)]
|
||||
},
|
||||
},
|
||||
});
|
||||
@ -1021,7 +983,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
continue;
|
||||
}
|
||||
|
||||
methods.push(TraitItemMethod {
|
||||
methods.push(TraitItemFn {
|
||||
attrs: vec![],
|
||||
sig,
|
||||
default: Some(create_method_body(mode, ty)),
|
||||
@ -1046,7 +1008,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
}
|
||||
};
|
||||
|
||||
ref_methods.push(ImplItemMethod {
|
||||
ref_methods.push(ImplItemFn {
|
||||
attrs: vec![],
|
||||
vis: Visibility::Inherited,
|
||||
defaultness: None,
|
||||
@ -1058,7 +1020,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
{
|
||||
// Either
|
||||
|
||||
either_methods.push(ImplItemMethod {
|
||||
either_methods.push(ImplItemFn {
|
||||
attrs: vec![],
|
||||
vis: Visibility::Inherited,
|
||||
defaultness: None,
|
||||
@ -1091,7 +1053,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
{
|
||||
// Optional
|
||||
|
||||
optional_methods.push(ImplItemMethod {
|
||||
optional_methods.push(ImplItemFn {
|
||||
attrs: vec![],
|
||||
vis: Visibility::Inherited,
|
||||
defaultness: None,
|
||||
@ -1150,7 +1112,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
{
|
||||
// Visit <-> VisitAll using swc_visit::All
|
||||
|
||||
visit_all_methods.push(ImplItemMethod {
|
||||
visit_all_methods.push(ImplItemFn {
|
||||
attrs: vec![],
|
||||
vis: Visibility::Inherited,
|
||||
defaultness: None,
|
||||
@ -1172,8 +1134,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ allow }).parse(),
|
||||
tokens: q!({ (unused_variables) }).parse(),
|
||||
meta: parse_quote!(allow(non_shorthand_field_patterns, unused_variables)),
|
||||
});
|
||||
|
||||
let mut fn_name = v.sig.ident.clone();
|
||||
@ -1234,7 +1195,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
///
|
||||
/// This is the default implementation of a method of
|
||||
/// [Fold].
|
||||
#[allow(unused_variables)]
|
||||
#[allow(non_shorthand_field_patterns, unused_variables)]
|
||||
pub fn fn_name<V: ?Sized + Trait>(_visitor: &mut V, n: Type) -> Type {
|
||||
default_body
|
||||
}
|
||||
@ -1253,7 +1214,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
///
|
||||
/// This is the default implementation of a method of
|
||||
/// [VisitMut].
|
||||
#[allow(unused_variables)]
|
||||
#[allow(non_shorthand_field_patterns, unused_variables)]
|
||||
pub fn fn_name<V: ?Sized + Trait>(_visitor: &mut V, n: Type) {
|
||||
default_body
|
||||
}
|
||||
@ -1272,7 +1233,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
///
|
||||
/// This is the default implementation of a method of
|
||||
/// [Visit].
|
||||
#[allow(unused_variables)]
|
||||
#[allow(non_shorthand_field_patterns, unused_variables)]
|
||||
pub fn fn_name<V: ?Sized + Trait>(_visitor: &mut V, n: Type) {
|
||||
default_body
|
||||
}
|
||||
@ -1289,7 +1250,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
{
|
||||
#[cfg(any(feature = "path", docsrs))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "path")))]
|
||||
#[allow(unused_variables)]
|
||||
#[allow(non_shorthand_field_patterns, unused_variables)]
|
||||
fn fn_name<V: ?Sized + Trait>(
|
||||
_visitor: &mut V,
|
||||
n: Type,
|
||||
@ -1310,7 +1271,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
{
|
||||
#[cfg(any(feature = "path", docsrs))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "path")))]
|
||||
#[allow(unused_variables)]
|
||||
#[allow(non_shorthand_field_patterns, unused_variables)]
|
||||
fn fn_name<V: ?Sized + Trait>(
|
||||
_visitor: &mut V,
|
||||
n: Type,
|
||||
@ -1332,7 +1293,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
{
|
||||
#[cfg(any(feature = "path", docsrs))]
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "path")))]
|
||||
#[allow(unused_variables)]
|
||||
#[allow(non_shorthand_field_patterns, unused_variables)]
|
||||
fn fn_name<'ast, 'r, V: ?Sized + Trait>(
|
||||
_visitor: &mut V,
|
||||
n: Type,
|
||||
@ -1358,9 +1319,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
|
||||
tokens.push_tokens(&ItemTrait {
|
||||
attrs,
|
||||
vis: Visibility::Public(VisPublic {
|
||||
pub_token: def_site(),
|
||||
}),
|
||||
vis: Visibility::Public(def_site()),
|
||||
unsafety: None,
|
||||
auto_token: None,
|
||||
trait_token: def_site(),
|
||||
@ -1369,7 +1328,8 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
colon_token: None,
|
||||
supertraits: Default::default(),
|
||||
brace_token: def_site(),
|
||||
items: methods.into_iter().map(TraitItem::Method).collect(),
|
||||
items: methods.into_iter().map(TraitItem::Fn).collect(),
|
||||
restriction: None,
|
||||
});
|
||||
|
||||
{
|
||||
@ -1386,7 +1346,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
.parse::<ItemImpl>();
|
||||
|
||||
item.items
|
||||
.extend(ref_methods.clone().into_iter().map(ImplItem::Method));
|
||||
.extend(ref_methods.clone().into_iter().map(ImplItem::Fn));
|
||||
|
||||
if let Some(VisitorVariant::WithPath) = mode.visitor_variant() {
|
||||
item.attrs.extend(feature_path_attrs())
|
||||
@ -1407,8 +1367,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
)
|
||||
.parse::<ItemImpl>();
|
||||
|
||||
item.items
|
||||
.extend(ref_methods.into_iter().map(ImplItem::Method));
|
||||
item.items.extend(ref_methods.into_iter().map(ImplItem::Fn));
|
||||
|
||||
if let Some(VisitorVariant::WithPath) = mode.visitor_variant() {
|
||||
item.attrs.extend(feature_path_attrs())
|
||||
@ -1430,7 +1389,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
.parse::<ItemImpl>();
|
||||
|
||||
item.items
|
||||
.extend(optional_methods.into_iter().map(ImplItem::Method));
|
||||
.extend(optional_methods.into_iter().map(ImplItem::Fn));
|
||||
|
||||
if let Some(VisitorVariant::WithPath) = mode.visitor_variant() {
|
||||
item.attrs.extend(feature_path_attrs())
|
||||
@ -1457,7 +1416,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
.parse::<ItemImpl>();
|
||||
|
||||
item.items
|
||||
.extend(either_methods.into_iter().map(ImplItem::Method));
|
||||
.extend(either_methods.into_iter().map(ImplItem::Fn));
|
||||
|
||||
if let Some(VisitorVariant::WithPath) = mode.visitor_variant() {
|
||||
item.attrs.extend(feature_path_attrs())
|
||||
@ -1474,7 +1433,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
.parse::<ItemImpl>();
|
||||
|
||||
item.items
|
||||
.extend(visit_all_methods.into_iter().map(ImplItem::Method));
|
||||
.extend(visit_all_methods.into_iter().map(ImplItem::Fn));
|
||||
|
||||
tokens.push_tokens(&item);
|
||||
tokens.push_tokens(&q!({
|
||||
@ -1530,7 +1489,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
|
||||
/// Visit children nodes with v and ast path appended
|
||||
/// [AstParentNodeRef] describing `self`. The ast path will
|
||||
/// be resotred when this method returns.
|
||||
/// be restored when this method returns.
|
||||
///
|
||||
/// This is the default implementaton of a handler method in
|
||||
/// [VisitAstPath].
|
||||
@ -1636,7 +1595,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
|
||||
/// Visit children nodes with v and ast path appended
|
||||
/// [AstKind] of `self`. The ast path will
|
||||
/// be resotred when this method returns.
|
||||
/// be restored when this method returns.
|
||||
///
|
||||
/// This is the default implementaton of a handler method in
|
||||
/// [FoldAstPath].
|
||||
@ -1704,7 +1663,7 @@ fn make(mode: Mode, stmts: &[Stmt]) -> Quote {
|
||||
fn visit_mut_with_path(&mut self, v: &mut V, ast_path: &mut AstKindPath);
|
||||
|
||||
/// Visit children nodes with v and ast path appended
|
||||
/// [AstKind] of `self`. The ast path will be resotred when
|
||||
/// [AstKind] of `self`. The ast path will be restored when
|
||||
/// this method returns.
|
||||
///
|
||||
/// This is the default implementaton of a handler method in
|
||||
@ -2280,7 +2239,7 @@ fn make_arm_from_struct(
|
||||
use_ast_path: bool,
|
||||
) -> Arm {
|
||||
let mut stmts = vec![];
|
||||
let mut fields: Punctuated<FieldValue, Token![,]> = Default::default();
|
||||
let mut fields: Punctuated<FieldPat, Token![,]> = Default::default();
|
||||
|
||||
for (i, field) in variant.iter().enumerate() {
|
||||
let ty = &field.ty;
|
||||
@ -2314,7 +2273,7 @@ fn make_arm_from_struct(
|
||||
let expr = visit_expr(mode, ty, &q!({ _visitor }).parse(), expr, ast_path);
|
||||
stmts.push(match mode {
|
||||
Mode::VisitAll | Mode::Visit { .. } | Mode::VisitMut { .. } => {
|
||||
Stmt::Semi(expr, call_site())
|
||||
Stmt::Expr(expr, Some(call_site()))
|
||||
}
|
||||
Mode::Fold { .. } => q!(
|
||||
Vars {
|
||||
@ -2329,27 +2288,25 @@ fn make_arm_from_struct(
|
||||
});
|
||||
}
|
||||
|
||||
if field.ident.is_some() {
|
||||
fields.push(
|
||||
q!(
|
||||
Vars {
|
||||
field: &binding_ident
|
||||
},
|
||||
{ field }
|
||||
)
|
||||
.parse(),
|
||||
);
|
||||
} else {
|
||||
fields.push(FieldValue {
|
||||
attrs: vec![],
|
||||
member: Member::Unnamed(Index {
|
||||
fields.push(FieldPat {
|
||||
attrs: vec![],
|
||||
member: if field.ident.is_none() {
|
||||
Member::Unnamed(Index {
|
||||
index: i as _,
|
||||
span: path.span(),
|
||||
}),
|
||||
colon_token: Some(def_site()),
|
||||
expr: q!(Vars { binding_ident }, { binding_ident }).parse(),
|
||||
});
|
||||
}
|
||||
})
|
||||
} else {
|
||||
Member::Named(field.ident.clone().unwrap())
|
||||
},
|
||||
colon_token: Some(def_site()),
|
||||
pat: Box::new(Pat::Ident(PatIdent {
|
||||
attrs: Default::default(),
|
||||
by_ref: None,
|
||||
mutability: None,
|
||||
ident: binding_ident,
|
||||
subpat: None,
|
||||
})),
|
||||
});
|
||||
}
|
||||
|
||||
match mode {
|
||||
@ -2379,7 +2336,14 @@ fn make_arm_from_struct(
|
||||
|
||||
Arm {
|
||||
attrs: vec![],
|
||||
pat: q!(Vars { Path: path, fields }, { Path { fields } }).parse(),
|
||||
pat: Pat::Struct(PatStruct {
|
||||
attrs: vec![],
|
||||
qself: None,
|
||||
path: path.clone(),
|
||||
brace_token: def_site(),
|
||||
fields,
|
||||
rest: None,
|
||||
}),
|
||||
guard: None,
|
||||
fat_arrow_token: def_site(),
|
||||
body: Box::new(Expr::Block(ExprBlock {
|
||||
@ -2454,12 +2418,12 @@ fn method_sig_from_ident(mode: Mode, v: &Ident) -> Signature {
|
||||
}
|
||||
|
||||
/// Returns None if it's skipped.
|
||||
fn make_method(mode: Mode, e: &Item, types: &mut Vec<Type>) -> Option<TraitItemMethod> {
|
||||
fn make_method(mode: Mode, e: &Item, types: &mut Vec<Type>) -> Option<TraitItemFn> {
|
||||
let mut attrs = vec![];
|
||||
|
||||
{
|
||||
attrs.push(make_doc_attr(
|
||||
"This method can be overriden to customize the visitor behavior.",
|
||||
"This method can be overridden to customize the visitor behavior.",
|
||||
));
|
||||
attrs.push(make_doc_attr(""));
|
||||
}
|
||||
@ -2510,7 +2474,7 @@ fn make_method(mode: Mode, e: &Item, types: &mut Vec<Type>) -> Option<TraitItemM
|
||||
|
||||
let sig = method_sig_from_ident(mode, type_name);
|
||||
|
||||
TraitItemMethod {
|
||||
TraitItemFn {
|
||||
attrs,
|
||||
sig,
|
||||
default: Some(block),
|
||||
@ -2565,17 +2529,20 @@ fn make_method(mode: Mode, e: &Item, types: &mut Vec<Type>) -> Option<TraitItemM
|
||||
|
||||
Block {
|
||||
brace_token: def_site(),
|
||||
stmts: vec![Stmt::Expr(Expr::Match(ExprMatch {
|
||||
attrs: vec![],
|
||||
match_token: def_site(),
|
||||
expr: q!((n)).parse(),
|
||||
brace_token: def_site(),
|
||||
arms,
|
||||
}))],
|
||||
stmts: vec![Stmt::Expr(
|
||||
Expr::Match(ExprMatch {
|
||||
attrs: vec![],
|
||||
match_token: def_site(),
|
||||
expr: q!((n)).parse(),
|
||||
brace_token: def_site(),
|
||||
arms,
|
||||
}),
|
||||
None,
|
||||
)],
|
||||
}
|
||||
};
|
||||
|
||||
TraitItemMethod {
|
||||
TraitItemFn {
|
||||
attrs,
|
||||
sig: method_sig_from_ident(mode, type_name),
|
||||
default: Some(block),
|
||||
@ -2833,7 +2800,7 @@ fn create_method_body(mode: Mode, ty: &Type) -> Block {
|
||||
|
||||
return Block {
|
||||
brace_token: Default::default(),
|
||||
stmts: vec![Stmt::Expr(visit)],
|
||||
stmts: vec![Stmt::Expr(visit, None)],
|
||||
};
|
||||
}
|
||||
Mode::VisitMut { .. } => {
|
||||
@ -3296,15 +3263,13 @@ fn feature_path_attrs() -> Vec<Attribute> {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ cfg }).parse(),
|
||||
tokens: q!({ (any(feature = "path", docsrs)) }).into(),
|
||||
meta: parse_quote!(cfg(any(feature = "path", docsrs))),
|
||||
},
|
||||
Attribute {
|
||||
pound_token: def_site(),
|
||||
style: AttrStyle::Outer,
|
||||
bracket_token: def_site(),
|
||||
path: q!({ cfg_attr }).parse(),
|
||||
tokens: q!({ (docsrs, doc(cfg(feature = "path"))) }).into(),
|
||||
meta: parse_quote!(cfg_attr(docsrs, doc(cfg(feature = "path")))),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
@ -18,7 +18,7 @@ bench = false
|
||||
serde-impl = ["serde"]
|
||||
|
||||
[dependencies]
|
||||
is-macro = "0.2.0"
|
||||
is-macro = "0.3.0"
|
||||
serde = { version = "1.0.127", features = ["derive"], optional = true }
|
||||
|
||||
string_enum = { version = "0.4.0", path = "../string_enum/" }
|
||||
|
@ -16,9 +16,9 @@ bench = false
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["fold"] }
|
||||
syn = { version = "2", features = ["fold"] }
|
||||
|
||||
swc_macros_common = { version = "0.3.7", path = "../swc_macros_common" }
|
||||
|
@ -3,20 +3,20 @@
|
||||
extern crate proc_macro;
|
||||
|
||||
use pmutil::{smart_quote, Quote, ToTokensExt};
|
||||
use syn::{FnArg, ImplItemMethod, Type, TypeReference};
|
||||
use syn::{FnArg, ImplItemFn, Type, TypeReference};
|
||||
|
||||
#[proc_macro_attribute]
|
||||
pub fn emitter(
|
||||
_attr: proc_macro::TokenStream,
|
||||
item: proc_macro::TokenStream,
|
||||
) -> proc_macro::TokenStream {
|
||||
let item: ImplItemMethod = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item: ImplItemFn = syn::parse(item).expect("failed to parse input as an item");
|
||||
let item = expand(item);
|
||||
|
||||
item.dump().into()
|
||||
}
|
||||
|
||||
fn expand(i: ImplItemMethod) -> ImplItemMethod {
|
||||
fn expand(i: ImplItemFn) -> ImplItemFn {
|
||||
let mtd_name = i.sig.ident.clone();
|
||||
assert!(
|
||||
format!("{}", i.sig.ident).starts_with("emit_"),
|
||||
@ -82,5 +82,5 @@ fn (&mut self, node: Node) -> Result;
|
||||
.parse()
|
||||
};
|
||||
|
||||
ImplItemMethod { block, ..i }
|
||||
ImplItemFn { block, ..i }
|
||||
}
|
||||
|
@ -19,10 +19,10 @@ proc-macro = true
|
||||
[dependencies]
|
||||
anyhow = "1"
|
||||
glob = "0.3"
|
||||
pmutil = "0.5.1"
|
||||
pmutil = "0.6.1"
|
||||
proc-macro2 = "1.0.24"
|
||||
quote = "1"
|
||||
regex = "1"
|
||||
relative-path = "1.3.2"
|
||||
syn = {version = "1", features = ["fold", "parsing", "full", "extra-traits"]}
|
||||
syn = {version = "2", features = ["fold", "parsing", "full", "extra-traits"]}
|
||||
once_cell = "1.10.0"
|
||||
|
@ -12,7 +12,9 @@ use regex::Regex;
|
||||
use relative_path::RelativePath;
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
Ident, Lit, LitStr, Meta, NestedMeta, Token,
|
||||
parse2,
|
||||
punctuated::Punctuated,
|
||||
Ident, LitStr, Meta, Token,
|
||||
};
|
||||
|
||||
pub struct Config {
|
||||
@ -45,24 +47,18 @@ impl Parse for Config {
|
||||
}};
|
||||
}
|
||||
|
||||
if list.nested.is_empty() {
|
||||
if list.tokens.is_empty() {
|
||||
fail!("empty exclude()")
|
||||
}
|
||||
|
||||
for token in list.nested.iter() {
|
||||
match token {
|
||||
NestedMeta::Meta(_) => fail!(),
|
||||
NestedMeta::Lit(lit) => {
|
||||
let lit = match lit {
|
||||
Lit::Str(v) => v.value(),
|
||||
_ => fail!(),
|
||||
};
|
||||
c.exclude_patterns
|
||||
.push(Regex::new(&lit).unwrap_or_else(|err| {
|
||||
fail!(format!("failed to parse regex: {}\n{}", lit, err))
|
||||
}));
|
||||
}
|
||||
}
|
||||
let input = parse2::<InputParen>(list.tokens.clone())
|
||||
.expect("failed to parse token as `InputParen`");
|
||||
|
||||
for lit in input.input {
|
||||
c.exclude_patterns
|
||||
.push(Regex::new(&lit.value()).unwrap_or_else(|err| {
|
||||
fail!(format!("failed to parse regex: {}\n{}", lit.value(), err))
|
||||
}));
|
||||
}
|
||||
|
||||
return;
|
||||
@ -188,3 +184,15 @@ pub fn expand(callee: &Ident, attr: Config) -> Result<Vec<Quote>, Error> {
|
||||
|
||||
Ok(test_fns)
|
||||
}
|
||||
|
||||
struct InputParen {
|
||||
input: Punctuated<LitStr, Token![,]>,
|
||||
}
|
||||
|
||||
impl Parse for InputParen {
|
||||
fn parse(input: ParseStream) -> syn::Result<Self> {
|
||||
Ok(Self {
|
||||
input: input.call(Punctuated::parse_terminated)?,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user