Skip to content

Commit

Permalink
feat(es/testing): Parse test code as a Program instead of a Module (
Browse files Browse the repository at this point in the history
#9264)

**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:**

 - Closes #8713
  • Loading branch information
levi-nz authored Oct 7, 2024
1 parent 8263da1 commit 166b858
Show file tree
Hide file tree
Showing 16 changed files with 425 additions and 89 deletions.
5 changes: 5 additions & 0 deletions .changeset/nine-trees-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
swc_ecma_transforms_testing: breaking
---

feat(es/testing): Parse test code as a `Program` instead of a `Module`
17 changes: 17 additions & 0 deletions crates/swc_ecma_transforms/tests/decorators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ fn transformation(t: &Tester) -> impl Fold {

// transformation_declaration
test!(
module,
syntax(false),
|t| transformation(t),
transformation_declaration,
Expand All @@ -101,6 +102,7 @@ class A {}
);
// transformation_initialize_after_super_multiple
test!(
module,
syntax(false),
|t| transformation(t),
transformation_initialize_after_super_multiple,
Expand Down Expand Up @@ -134,6 +136,7 @@ export default @dec() class {}
);
// transformation_initialize_after_super_statement
test!(
module,
syntax(false),
|t| transformation(t),
transformation_initialize_after_super_statement,
Expand Down Expand Up @@ -211,6 +214,7 @@ expect(A).toBe(C);
);
// misc_method_name_not_shadow
test!(
module,
syntax(false),
|t| tr(t),
misc_method_name_not_shadow,
Expand Down Expand Up @@ -320,6 +324,7 @@ expect(() => {
);
// duplicated_keys_computed_keys_same_value
test!(
module,
syntax(false),
|t| tr(t),
duplicated_keys_computed_keys_same_value,
Expand Down Expand Up @@ -395,6 +400,7 @@ expect(log).toEqual(numsFrom0to9);
);
// transformation_initializer_after_super_bug_8808
test!(
module,
syntax(false),
|t| transformation(t),
transformation_initiailzer_after_super_bug_8808,
Expand Down Expand Up @@ -434,6 +440,7 @@ expect(A.prototype.method()).toBe(2);
);
// transformation_arguments
test!(
module,
syntax(false),
|t| transformation(t),
transformation_arguments,
Expand Down Expand Up @@ -532,6 +539,7 @@ expect(calls).toBe(1);
// ordering
// transformation_initialize_after_super_expression
test!(
module,
syntax(false),
|t| transformation(t),
transformation_initialize_after_super_expression,
Expand Down Expand Up @@ -903,6 +911,7 @@ expect(Object.getOwnPropertyDescriptor(A.prototype, "foo")).toEqual({
);
// transformation_extends
test!(
module,
syntax(false),
|t| transformation(t),
transformation_extends,
Expand All @@ -914,6 +923,7 @@ test!(
// finishers
// transformation_extends_await
test!(
module,
syntax(false),
|t| transformation(t),
transformation_extends_await,
Expand All @@ -926,6 +936,7 @@ async function g() {
);
// transformation_extends_yield
test!(
module,
syntax(false),
|t| transformation(t),
transformation_extends_yield,
Expand Down Expand Up @@ -1092,6 +1103,7 @@ expect(i).toBe(2);
);
// transformation_initialize_after_super_bug_8931
test!(
module,
syntax(false),
|t| transformation(t),
transformation_initialize_after_super_bug_8931,
Expand Down Expand Up @@ -1318,6 +1330,7 @@ expect(Foo.prototype.bar).toBe(value2);
);
// transformation_expression
test!(
module,
syntax(false),
|t| transformation(t),
transformation_expression,
Expand Down Expand Up @@ -1354,6 +1367,7 @@ expect(A.method()).toBe(2);
// element_descriptors
// duplicated_keys_computed_keys_same_ast
test!(
module,
syntax(false),
|t| tr(t),
duplicated_keys_computed_keys_same_ast,
Expand Down Expand Up @@ -1503,6 +1517,7 @@ expect(desc.set()).toBe(2);
// misc
// transformation_extends_exec
test_exec!(
module,
syntax(false),
|t| tr(t),
transformation_extends_exec,
Expand Down Expand Up @@ -1549,6 +1564,7 @@ expect(() => {
);
// duplicated_keys_computed_keys_same_value_exec
test_exec!(
module,
syntax(false),
|t| tr(t),
duplicated_keys_computed_keys_same_value_exec,
Expand Down Expand Up @@ -3882,6 +3898,7 @@ eval: function () {
);

test!(
module,
ts(),
|_| decorators(Default::default()),
issue_846_1,
Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_transforms_module/tests/amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use swc_ecma_parser::{Syntax, TsSyntax};
use swc_ecma_transforms_base::{feature::FeatureFlag, resolver};
use swc_ecma_transforms_compat::es2015::for_of;
use swc_ecma_transforms_module::amd::{self, amd};
use swc_ecma_transforms_testing::{test, test_fixture};
use swc_ecma_transforms_testing::{test, test_module_fixture};
use swc_ecma_transforms_typescript::typescript;
use swc_ecma_visit::Fold;

Expand Down Expand Up @@ -60,7 +60,7 @@ fn esm_to_amd(input: PathBuf) {
Err(..) => Default::default(),
};

test_fixture(
test_module_fixture(
if is_ts { ts_syntax() } else { syntax() },
&|t| tr(config.clone(), is_ts, t.comments.clone()),
&input,
Expand All @@ -70,6 +70,7 @@ fn esm_to_amd(input: PathBuf) {
}

test!(
module,
syntax(),
|t| chain!(
for_of(for_of::Config {
Expand Down
5 changes: 3 additions & 2 deletions crates/swc_ecma_transforms_module/tests/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use swc_ecma_parser::{Syntax, TsSyntax};
use swc_ecma_transforms_base::{feature::FeatureFlag, resolver};
use swc_ecma_transforms_compat::es2015::for_of;
use swc_ecma_transforms_module::common_js::{self, common_js};
use swc_ecma_transforms_testing::{test, test_fixture, FixtureTestConfig};
use swc_ecma_transforms_testing::{test, test_module_fixture, FixtureTestConfig};
use swc_ecma_transforms_typescript::typescript;
use swc_ecma_visit::Fold;

Expand Down Expand Up @@ -52,7 +52,7 @@ fn esm_to_cjs(input: PathBuf) {
Err(..) => Default::default(),
};

test_fixture(
test_module_fixture(
if is_ts { ts_syntax() } else { syntax() },
&|_| tr(config.clone(), is_ts),
&input,
Expand All @@ -65,6 +65,7 @@ fn esm_to_cjs(input: PathBuf) {
}

test!(
module,
syntax(),
|_| chain!(
for_of(for_of::Config {
Expand Down
6 changes: 3 additions & 3 deletions crates/swc_ecma_transforms_module/tests/path_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use swc_ecma_transforms_module::{
path::{ImportResolver, NodeImportResolver},
rewriter::import_rewriter,
};
use swc_ecma_transforms_testing::test_fixture;
use swc_ecma_transforms_testing::test_module_fixture;
use testing::run_test2;

type TestProvider = NodeImportResolver<NodeModulesResolver>;
Expand Down Expand Up @@ -43,7 +43,7 @@ fn issue_4730() {
let input_dir = dir.join("input");
let output_dir = dir.join("output");

test_fixture(
test_module_fixture(
Syntax::default(),
&|_| {
let mut paths = IndexMap::new();
Expand Down Expand Up @@ -136,7 +136,7 @@ fn fixture(input_dir: PathBuf) {
.canonicalize()
.unwrap();
dbg!(&base_dir);
test_fixture(
test_module_fixture(
Syntax::default(),
&|_| {
let rules = config.paths.clone().into_iter().collect();
Expand Down
10 changes: 8 additions & 2 deletions crates/swc_ecma_transforms_module/tests/system_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use swc_common::{chain, Mark};
use swc_ecma_parser::Syntax;
use swc_ecma_transforms_base::resolver;
use swc_ecma_transforms_module::system_js::{system_js, Config};
use swc_ecma_transforms_testing::{test, test_fixture, Tester};
use swc_ecma_transforms_testing::{test, test_module_fixture, Tester};
use swc_ecma_visit::Fold;

fn syntax() -> Syntax {
Expand All @@ -23,13 +23,15 @@ fn tr(_tester: &mut Tester<'_>, config: Config) -> impl Fold {
}

test!(
module,
syntax(),
|tester| tr(tester, Default::default()),
allow_continuous_assignment,
r#"var e = {}; e.a = e.b = e.c = e.d = e.e = e.f = e.g = e.h = e.i = e.j = e.k = e.l = e.m = e.n = e.o = e.p = e.q = e.r = e.s = e.t = e.u = e.v = e.w = e.x = e.y = e.z = e.A = e.B = e.C = e.D = e.E = e.F = e.G = e.H = e.I = e.J = e.K = e.L = e.M = e.N = e.O = e.P = e.Q = e.R = e.S = void 0;"#
);

test!(
module,
syntax(),
|tester| tr(
tester,
Expand All @@ -43,6 +45,7 @@ test!(
);

test!(
module,
syntax(),
|tester| tr(
tester,
Expand All @@ -60,6 +63,7 @@ test!(
);

test!(
module,
syntax(),
|tester| tr(
tester,
Expand All @@ -82,6 +86,7 @@ test!(
);

test!(
module,
syntax(),
|tester| tr(
tester,
Expand All @@ -99,6 +104,7 @@ test!(
);

test!(
module,
syntax(),
|tester| tr(tester, Default::default()),
imports,
Expand All @@ -117,7 +123,7 @@ fn fixture(input: PathBuf) {

let output = dir.join("output.mjs");

test_fixture(
test_module_fixture(
syntax(),
&|tester| tr(tester, Default::default()),
&input,
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_module/tests/umd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use swc_common::{chain, Mark};
use swc_ecma_parser::{Syntax, TsSyntax};
use swc_ecma_transforms_base::{feature::FeatureFlag, resolver};
use swc_ecma_transforms_module::umd::{umd, Config};
use swc_ecma_transforms_testing::{test_fixture, Tester};
use swc_ecma_transforms_testing::{test_module_fixture, Tester};
use swc_ecma_transforms_typescript::typescript;
use swc_ecma_visit::Fold;

Expand Down Expand Up @@ -58,7 +58,7 @@ fn esm_to_umd(input: PathBuf) {
Err(..) => Default::default(),
};

test_fixture(
test_module_fixture(
if is_ts { ts_syntax() } else { syntax() },
&|tester| tr(tester, config.clone(), is_ts),
&input,
Expand Down
15 changes: 10 additions & 5 deletions crates/swc_ecma_transforms_optimization/src/inline_globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl VisitMut for InlineGlobals {
#[cfg(test)]
mod tests {
use swc_ecma_transforms_testing::{test, Tester};
use swc_ecma_utils::DropSpan;
use swc_ecma_utils::{DropSpan, StmtOrModuleItem};

use super::*;

Expand All @@ -220,17 +220,22 @@ mod tests {
(*v).into()
};

let mut v = tester
let v = tester
.apply_transform(
as_folder(DropSpan),
"global.js",
::swc_ecma_parser::Syntax::default(),
&v,
)
.unwrap();
assert_eq!(v.body.len(), 1);
let v = match v.body.pop().unwrap() {
ModuleItem::Stmt(Stmt::Expr(ExprStmt { expr, .. })) => *expr,

let v = match v {
Program::Module(mut m) => m.body.pop().and_then(|x| x.into_stmt().ok()),
Program::Script(mut s) => s.body.pop(),
};
assert!(v.is_some());
let v = match v.unwrap() {
Stmt::Expr(ExprStmt { expr, .. }) => *expr,
_ => unreachable!(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,19 @@ impl VisitMut for Inlining<'_> {
self.visit_with_child(ScopeKind::Cond, &mut stmt.alt);
}

fn visit_mut_module_items(&mut self, items: &mut Vec<ModuleItem>) {
fn visit_mut_program(&mut self, program: &mut Program) {
let _tracing = span!(Level::ERROR, "inlining", pass = self.pass).entered();

let old_phase = self.phase;

self.phase = Phase::Analysis;
items.visit_mut_children_with(self);
program.visit_mut_children_with(self);

tracing::trace!("Switching to Inlining phase");

// Inline
self.phase = Phase::Inlining;
items.visit_mut_children_with(self);
program.visit_mut_children_with(self);

self.phase = old_phase;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/swc_ecma_transforms_proposal/tests/decorator_evanw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::{fs, path::PathBuf};

use swc_ecma_parser::{EsSyntax, Syntax};
use swc_ecma_transforms_proposal::decorator_2022_03::decorator_2022_03;
use swc_ecma_transforms_testing::exec_tr;
use swc_ecma_transforms_testing::exec_module_tr;
use swc_ecma_visit::as_folder;

const HELPERS: &str = r###"
Expand Down Expand Up @@ -57,7 +57,7 @@ fn fixture(input: PathBuf) {
{code}"
);

exec_tr(
exec_module_tr(
&input.file_name().unwrap().to_string_lossy(),
Syntax::Es(EsSyntax {
decorators: true,
Expand Down
Loading

0 comments on commit 166b858

Please sign in to comment.