swc-project / swc

Rust-based platform for the Web
https://swc.rs
Apache License 2.0
31.25k stars 1.23k forks source link

`test_transform` should probably use `parse_program` instead of `parse_module` #8713

Closed levi-nz closed 1 month ago

levi-nz commented 8 months ago

Describe the feature

The problem right now with test_transform is that, because it uses parse_module to parse code internally, visitors that use visit_script/visit_mut_script/fold_script will not work because these functions are never called due to the parsed code being a module (thanks to apply_transform): https://github.com/swc-project/swc/blob/b0c00b96f18c391bea89059130d08484e3ac3506/crates/swc_ecma_transforms_testing/src/lib.rs#L175

To fix this, we could make test_transform use parse_program instead, but this might introduce breaking changes. A Program can be either a Module or Script, but if we make test_transform use parse_program it might break existing unit tests (1st or 3rd party) that use visit_module instead of visit_program (or the other two functions). Some of the code related to this would also need to be changed, but that doesn't seem too big of an issue.

Overall, making test_transform would make people's lives a bit easier when making visitors that use specific functions like visit_script if they know they will never need to use the program or module functions, but I'm curious what the maintainers think about this as I'm not sure if I'm right.

Babel plugin or link to the feature description

No response

Additional context

No response

levi-nz commented 8 months ago

Also just came to thought, I believe visit_program (and others) will also not work in unit tests because a Module is not a Program, as far as I know. So to my understanding right now, only visit_module/visit_mut_module/fold_module can be called from test_transform, while visit_program and visit_script can never be called, due to the parsed code always being a Module.

swc-bot commented 1 week ago

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.