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 #9623

Merged
merged 24 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/nine-trees-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_transforms_testing: breaking
swc_ecma_transforms_optimization: patch
---

feat(es/testing): Parse test code as a `Program` instead of a `Module`
15 changes: 15 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 @@ -3882,6 +3896,7 @@ eval: function () {
);

test!(
module,
ts(),
|_| decorators(Default::default()),
issue_846_1,
Expand Down
8 changes: 6 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_fixture, FixtureTestConfig};
use swc_ecma_transforms_typescript::typescript;
use swc_ecma_visit::Fold;

Expand Down Expand Up @@ -65,11 +65,15 @@ fn esm_to_amd(input: PathBuf) {
&|t| tr(config.clone(), is_ts, t.comments.clone()),
&input,
&output,
Default::default(),
FixtureTestConfig {
module: Some(true),
..Default::default()
},
);
}

test!(
module,
syntax(),
|t| chain!(
for_of(for_of::Config {
Expand Down
2 changes: 2 additions & 0 deletions crates/swc_ecma_transforms_module/tests/common_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ fn esm_to_cjs(input: PathBuf) {
&output,
FixtureTestConfig {
sourcemap: false,
module: Some(true),
..Default::default()
},
);
}

test!(
module,
syntax(),
|_| chain!(
for_of(for_of::Config {
Expand Down
12 changes: 9 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_fixture, FixtureTestConfig};
use testing::run_test2;

type TestProvider = NodeImportResolver<NodeModulesResolver>;
Expand Down Expand Up @@ -81,7 +81,10 @@ fn issue_4730() {
},
&input_dir.join("src").join("index.js"),
&output_dir.join("index.js"),
Default::default(),
FixtureTestConfig {
module: Some(true),
..Default::default()
},
);
}

Expand Down Expand Up @@ -147,6 +150,9 @@ fn fixture(input_dir: PathBuf) {
},
&index_path,
&output_dir.join("index.ts"),
Default::default(),
FixtureTestConfig {
module: Some(true),
..Default::default()
},
);
}
13 changes: 11 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_fixture, FixtureTestConfig, 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 @@ -122,6 +128,9 @@ fn fixture(input: PathBuf) {
&|tester| tr(tester, Default::default()),
&input,
&output,
Default::default(),
FixtureTestConfig {
module: Some(true),
..Default::default()
},
);
}
7 changes: 5 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_fixture, FixtureTestConfig, Tester};
use swc_ecma_transforms_typescript::typescript;
use swc_ecma_visit::Fold;

Expand Down Expand Up @@ -63,6 +63,9 @@ fn esm_to_umd(input: PathBuf) {
&|tester| tr(tester, config.clone(), is_ts),
&input,
&output,
Default::default(),
FixtureTestConfig {
module: Some(true),
..Default::default()
},
);
}
16 changes: 11 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,23 @@ mod tests {
(*v).into()
};

let mut v = tester
let v = tester
.apply_transform(
as_folder(DropSpan),
"global.js",
::swc_ecma_parser::Syntax::default(),
None,
&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 @@ -9,6 +9,7 @@ macro_rules! test_stmt {
($l:expr, $r:expr) => {
swc_ecma_transforms_testing::test_transform(
::swc_ecma_parser::Syntax::default(),
None,
|_| {
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
Expand All @@ -30,7 +31,6 @@ macro_rules! test_stmt {
},
$l,
$r,
true,
)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use super::SimplifyExpr;
fn fold(src: &str, expected: &str) {
test_transform(
::swc_ecma_parser::Syntax::default(),
None,
|_| {
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
Expand All @@ -32,7 +33,6 @@ fn fold(src: &str, expected: &str) {
},
src,
expected,
true,
)
}

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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use swc_ecma_transforms_typescript::strip;
fn test(src: &str, expected: &str) {
test_transform(
::swc_ecma_parser::Syntax::default(),
None,
|_| {
let unresolved_mark = Mark::new();
let top_level_mark = Mark::new();
Expand All @@ -35,7 +36,6 @@ fn test(src: &str, expected: &str) {
},
src,
expected,
true,
)
}

Expand Down
Loading
Loading