mirror of
https://github.com/swc-project/swc.git
synced 2024-12-23 21:54:36 +03:00
Implement #[fold(ignore)]
This commit is contained in:
parent
57944ab70e
commit
0be4a5ef40
@ -1,5 +1,18 @@
|
|||||||
|
use darling::FromField;
|
||||||
use swc_macros_common::prelude::*;
|
use swc_macros_common::prelude::*;
|
||||||
|
|
||||||
|
#[derive(Debug, FromField)]
|
||||||
|
#[darling(attributes(fold))]
|
||||||
|
struct FieldAttrs {
|
||||||
|
///
|
||||||
|
#[darling(default)]
|
||||||
|
pub ignore: bool,
|
||||||
|
|
||||||
|
/// Should we add bound for the field's type?
|
||||||
|
#[darling(default)]
|
||||||
|
pub bound: bool,
|
||||||
|
}
|
||||||
|
|
||||||
pub fn derive(input: DeriveInput) -> ItemImpl {
|
pub fn derive(input: DeriveInput) -> ItemImpl {
|
||||||
let mut derive_generics = Derive::new(&input);
|
let mut derive_generics = Derive::new(&input);
|
||||||
|
|
||||||
@ -157,6 +170,11 @@ pub fn derive(input: DeriveInput) -> ItemImpl {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn should_skip_field(field: &Field) -> bool {
|
fn should_skip_field(field: &Field) -> bool {
|
||||||
|
let attrs = FieldAttrs::from_field(field).expect("#[derive(Fold)]: failed to parse attribute");
|
||||||
|
if attrs.ignore {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
let ty_str = field.ty.dump().to_string();
|
let ty_str = field.ty.dump().to_string();
|
||||||
match &*ty_str {
|
match &*ty_str {
|
||||||
"bool" | "usize" | "u128" | "u64" | "u32" | "u16" | "u8" | "isize" | "i128" | "i64"
|
"bool" | "usize" | "u128" | "u64" | "u32" | "u16" | "u8" | "isize" | "i128" | "i64"
|
||||||
|
56
macros/ast_node/tests/fold_ignore.rs
Normal file
56
macros/ast_node/tests/fold_ignore.rs
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
#![feature(specialization, proc_macro)]
|
||||||
|
|
||||||
|
extern crate swc_common;
|
||||||
|
extern crate swc_macros;
|
||||||
|
use swc_common::{Fold, FoldWith};
|
||||||
|
struct MyFolder;
|
||||||
|
|
||||||
|
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
|
||||||
|
pub struct PanicOnFold;
|
||||||
|
impl<F> FoldWith<F> for PanicOnFold {
|
||||||
|
fn fold_children(self, _: &mut F) -> Self {
|
||||||
|
unreachable!("this should not be called")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ignore_struct_named_field() {
|
||||||
|
#[derive(Fold, Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||||
|
struct Foo {
|
||||||
|
#[fold(ignore)]
|
||||||
|
pub named: PanicOnFold,
|
||||||
|
}
|
||||||
|
Foo::default().fold_with(&mut MyFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ignore_struct_unnamed_field() {
|
||||||
|
#[derive(Fold, Debug, Clone, Copy, Default, PartialEq, Eq)]
|
||||||
|
struct Bar(#[fold(ignore)] PanicOnFold);
|
||||||
|
Bar::default().fold_with(&mut MyFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ignore_enum_unnamed_field() {
|
||||||
|
#[derive(Fold, Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum A {
|
||||||
|
Field(#[fold(ignore)] PanicOnFold),
|
||||||
|
}
|
||||||
|
|
||||||
|
A::Field(Default::default()).fold_with(&mut MyFolder);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ignore_enum_named_field() {
|
||||||
|
#[derive(Fold, Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
enum A {
|
||||||
|
Field {
|
||||||
|
#[fold(ignore)]
|
||||||
|
named: PanicOnFold,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
A::Field {
|
||||||
|
named: Default::default(),
|
||||||
|
}.fold_with(&mut MyFolder);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user