Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(traverse): CombinedTraverse type to compose Traverses #3219

Conversation

overlookmotel
Copy link
Collaborator

@overlookmotel overlookmotel commented May 10, 2024

Implements the CombinedTraverse type discussed in #3216 (comment). This type and the accompanying combine_traverses! macro allow composing multiple traverses/transforms with minimal boilerplate.

I am actually not sure if this is a good idea or not. The API is nice I think, but it may blow up compile times.

@overlookmotel overlookmotel marked this pull request as ready for review May 10, 2024 06:54
Copy link
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @overlookmotel and the rest of your teammates on Graphite Graphite

Copy link

codspeed-hq bot commented May 10, 2024

CodSpeed Performance Report

Merging #3219 will not alter performance

Comparing 05-10-feat_traverse_combinedtraverse_type_to_compose_traverse_s (16abdc7) with main (2064ae9)

Summary

✅ 27 untouched benchmarks

@Dunqing
Copy link
Member

Dunqing commented May 10, 2024

Can we combine multiple Traversers ( >2 )?

@overlookmotel
Copy link
Collaborator Author

Can we combine multiple Traversers ( >2 )?

Yes!

let all_five = combine_traverses!(
    MyTraverse1::new(),
    MyTraverse2::new(),
    MyTraverse3::new(),
    MyTraverse4::new(),
    MyTraverse5::new(), 
);

/// Macro to compose multiple traverses.
///
/// The traverses will run:
/// * `enter_*` methods in order.
/// * `exit_*` methods in reverse order.
///
/// Each `Traverse` does NOT run one after another on the whole AST in a series of passes.
/// Visitation is interleaved. The AST is only visited once.
///
/// i.e. with 3 `Traverse`s provided, execution order is:
///
/// * `Tr1::enter_program`
/// * `Tr2::enter_program`
/// * `Tr3::enter_program`
/// * `Tr1::enter_statements`
/// * `Tr2::enter_statements`
/// * `Tr3::enter_statements`
/// * ...and so on all the way down AST
/// * `Tr3::exit_statements`
/// * `Tr2::exit_statements`
/// * `Tr1::exit_statements`
/// * `Tr3::exit_program`
/// * `Tr2::exit_program`
/// * `Tr1::exit_program`
///
/// # Example
///
/// ```
/// use oxc_traverse::{combine_traverses, Traverse};
///
/// struct MyTraverse1;
/// impl MyTraverse1 { fn new() -> Self { Self } }
/// impl<'a> Traverse<'a> for MyTraverse1 {}
///
/// struct MyTraverse2;
/// impl MyTraverse2 { fn new() -> Self { Self } }
/// impl<'a> Traverse<'a> for MyTraverse2 {}
///
/// struct MyTraverse3;
/// impl MyTraverse3 { fn new() -> Self { Self } }
/// impl<'a> Traverse<'a> for MyTraverse3 {}
///
/// let all_three = combine_traverses!(
/// MyTraverse1::new(),
/// MyTraverse2::new(),
/// MyTraverse3::new(),
/// );
/// ```
///
/// # Expansion
///
/// `combine_traverses!` in above example expands to:
///
/// ```nocompile
/// let all_three = CombinedTraverse::new(
/// MyTraverse1::new(),
/// CombinedTraverse::new(
/// MyTraverse2::new(),
/// MyTraverse3::new(),
/// ),
/// );
/// ```
#[macro_export]
macro_rules! combine_traverses {
($t1:expr, $t2:expr $(,)?) => {
$crate::CombinedTraverse::new($t1, $t2)
};
($t1:expr, $t2:expr, $($rest:expr),+ $(,)?) => {
$crate::CombinedTraverse::new(
$t1,
$crate::combine_traverses!(
$t2,
$($rest),+
)
)
};
}

@Boshen
Copy link
Member

Boshen commented May 10, 2024

Crazy! Let's hop on a call every time we gonna add 3000 lines of code 😛

@overlookmotel
Copy link
Collaborator Author

overlookmotel commented May 10, 2024

Crazy!

Can I take that as "no way we're going to merge this nonsense"?

Well I did say "I am actually not sure if this is a good idea". It's just responding to Dunqing's request for a less verbose API in #3216.

And the joy of codegens is it only takes about 20 mins to generate 3000 lines of code! 😂

@overlookmotel overlookmotel force-pushed the 05-10-feat_traverse_combinedtraverse_type_to_compose_traverse_s branch from 2ff2eb8 to 2139fe7 Compare May 11, 2024 07:44
@overlookmotel overlookmotel force-pushed the 05-10-feat_traverse_combinedtraverse_type_to_compose_traverse_s branch from 2139fe7 to 16abdc7 Compare May 11, 2024 08:24
@Boshen
Copy link
Member

Boshen commented May 15, 2024

Let's refine the infra once all tests passes so we have the whole picture of the current infra.

@Boshen Boshen closed this May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants