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(es/testing): Parse test code as a Program instead of a Module #9264

Merged
merged 18 commits into from
Oct 7, 2024

Conversation

levi-nz
Copy link
Contributor

@levi-nz levi-nz commented Jul 16, 2024

Description:
This PR addresses the issue described in #8713

BREAKING CHANGE:
This will break existing unit tests that use fold_module/visit_module/visit_mut_module if the visitor is intended to work for both modules and scripts, instead of using fold_program/visit_program/visit_mut_program. This will also break existing unit tests if they're testing with input code that gets parsed as a script in parse_program if the visitor expects a module (they will need to update their test! calls to add module as the first argument, or use a function like apply_module_transform)

Related issue (if exists):

@levi-nz levi-nz requested a review from a team as a code owner July 16, 2024 23:36
@levi-nz levi-nz changed the title feat(es/testing): Parse test code as aProgram instead of a Module feat(es/testing): Parse test code as a Program instead of a Module Jul 16, 2024
@levi-nz levi-nz marked this pull request as draft July 17, 2024 00:14
@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 17, 2024

Several swc tests rely on the parsed test code being a module due to their visitors, going to have to fix these. The good news is that this will fix any of these issues for people using parse_script. e.g. some of the simplifier tests fail on scripts but not on modules.

Copy link

codspeed-hq bot commented Jul 17, 2024

CodSpeed Performance Report

Merging #9264 will not alter performance

Comparing levi-nz:issue-8713 (df6db60) with main (8263da1)

Summary

✅ 194 untouched benchmarks

@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 17, 2024

I will probably clean up the internal API a bit because I think it's a bit messy, but essentially how testing works now:

  • Current test implementations will continue to work, unless the visitor(s) being tested is implemented wrongly. Since parse_program is the default now and not parse_module, any visitors using visit_module instead of visit_program won't work if the parsed AST ends up being a script (because visit_module won't be called, only visit_script will). Visitors should use visit_program if they want to handle both modules and scripts.
  • Callers can force the test implementation to use parse_module or parse_script instead of parse_program by calling test!(module, ...) and test!(script, ...) respectively.
  • Some tests need to use test!(module, ...) as mentioned above, since their visitors depend on the code being a module. A good example are the fixture tests in swc_ecma_transforms_module.

@kdy1 kdy1 added this to the Planned milestone Jul 17, 2024
@kdy1
Copy link
Member

kdy1 commented Jul 17, 2024

Sounds good, thank you!

@levi-nz levi-nz marked this pull request as ready for review July 18, 2024 02:23
@levi-nz
Copy link
Contributor Author

levi-nz commented Jul 18, 2024

Not sure what we think of the internal api, test!(module, ...), test!(script, ...), apply_module_transform etc.

@kdy1 kdy1 self-assigned this Jul 18, 2024
@@ -10,7 +10,7 @@ use swc_common::{chain, comments::SingleThreadedComments, Mark};
use swc_ecma_parser::{EsSyntax, Syntax, TsSyntax};
use swc_ecma_transforms_base::{assumptions::Assumptions, resolver};
use swc_ecma_transforms_proposal::{decorator_2022_03::decorator_2022_03, DecoratorVersion};
use swc_ecma_transforms_testing::{test_fixture, FixtureTestConfig};
use swc_ecma_transforms_testing::{test_module_fixture, FixtureTestConfig};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have FixtureTestConfig.module: Option<bool>, which defaults to None, and

  • Some(true): Module (and applies visitor after wrapping as Program)
  • Some(false): Script (and applies visitor after wrapping as Program)
  • None: Program

@@ -96,6 +96,7 @@ fn transformation(t: &Tester) -> impl Fold {

// transformation_declaration
test!(
module,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love this idea 👍

Copy link

changeset-bot bot commented Oct 7, 2024

🦋 Changeset detected

Latest commit: df6db60

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@kdy1 kdy1 requested a review from a team as a code owner October 7, 2024 23:05
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@kdy1 kdy1 merged commit 166b858 into swc-project:main Oct 7, 2024
24 checks passed
@levi-nz
Copy link
Contributor Author

levi-nz commented Oct 8, 2024

@kdy1 can you un-merge? Not finished yet. Forgot to mark as draft

@levi-nz
Copy link
Contributor Author

levi-nz commented Oct 8, 2024

I've pushed my changes (should be good to go now), but I'll need to rebase once you revert the commit.

kdy1 added a commit that referenced this pull request Oct 8, 2024
@kdy1
Copy link
Member

kdy1 commented Oct 8, 2024

@levi-nz Done. #9621

@kdy1 kdy1 modified the milestones: Planned, v1.7.31 Oct 8, 2024
@swc-project swc-project locked as resolved and limited conversation to collaborators Nov 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

test_transform should probably use parse_program instead of parse_module
2 participants