feat(es/testing): Parse test code as a `Program` instead of a `Module`
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):
- Closes #8713
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.
CodSpeed Performance Report
Merging #9264 will not alter performance
Comparing levi-nz:issue-8713 (df6db60) with main (8263da1)
Summary
✅ 194 untouched benchmarks
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
testimplementations will continue to work, unless the visitor(s) being tested is implemented wrongly. Sinceparse_programis the default now and notparse_module, any visitors usingvisit_moduleinstead ofvisit_programwon't work if the parsed AST ends up being a script (becausevisit_modulewon't be called, onlyvisit_scriptwill). Visitors should usevisit_programif they want to handle both modules and scripts. - Callers can force the test implementation to use
parse_moduleorparse_scriptinstead ofparse_programby callingtest!(module, ...)andtest!(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.
Sounds good, thank you!
Not sure what we think of the internal api, test!(module, ...), test!(script, ...), apply_module_transform etc.
🦋 Changeset detected
Latest commit: df6db609bb50a6ad3e0cb24c091001d742f7f686
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 can you un-merge? Not finished yet. Forgot to mark as draft
I've pushed my changes (should be good to go now), but I'll need to rebase once you revert the commit.
@levi-nz Done. https://github.com/swc-project/swc/pull/9621