diff --git a/.gitmodules b/.gitmodules index 59c8a1cc37c..51e5e3839b1 100644 --- a/.gitmodules +++ b/.gitmodules @@ -7,4 +7,7 @@ path = crates/swc_html_parser/tests/html5lib-tests url = https://github.com/html5lib/html5lib-tests.git shallow = true - ignore = dirty \ No newline at end of file + ignore = dirty +[submodule "crates/swc_ecma_transforms_proposal/tests/decorator-tests"] + path = crates/swc_ecma_transforms_proposal/tests/decorator-tests + url = https://github.com/evanw/decorator-tests diff --git a/.vscode/settings.json b/.vscode/settings.json index bb4fbedae3f..2998e97898b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -19,7 +19,8 @@ "git.autoRepositoryDetection": false, "git.ignoredRepositories": [ "crates/swc_ecma_parser/tests/test262-parser", - "crates/swc_html_parser/tests/html5lib-tests" + "crates/swc_html_parser/tests/html5lib-tests", + "crates/swc_ecma_transforms_proposal/tests/decorator-tests" ], "eslint.enable": false, "rust-analyzer.check.command": "clippy", @@ -35,4 +36,4 @@ "rkyv-impl", "debug" ] -} \ No newline at end of file +} diff --git a/Cargo.lock b/Cargo.lock index 80b4e87e59f..311822218ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4785,6 +4785,7 @@ dependencies = [ "swc_atoms", "swc_common", "swc_ecma_ast", + "swc_ecma_codegen", "swc_ecma_loader", "swc_ecma_parser", "swc_ecma_transforms_base", diff --git a/crates/swc_ecma_transforms_proposal/Cargo.toml b/crates/swc_ecma_transforms_proposal/Cargo.toml index cfe31294f9b..1fb0d4d45fd 100644 --- a/crates/swc_ecma_transforms_proposal/Cargo.toml +++ b/crates/swc_ecma_transforms_proposal/Cargo.toml @@ -35,6 +35,7 @@ swc_ecma_visit = { version = "0.99.1", path = "../swc_ecma_visit" } [dev-dependencies] serde_json = { workspace = true } +swc_ecma_codegen = { version = "0.149.1", path = "../swc_ecma_codegen" } swc_ecma_parser = { version = "0.144.1", path = "../swc_ecma_parser" } swc_ecma_transforms_compat = { version = "0.164.1", path = "../swc_ecma_transforms_compat" } swc_ecma_transforms_testing = { version = "0.141.1", path = "../swc_ecma_transforms_testing" } diff --git a/crates/swc_ecma_transforms_proposal/tests/.gitignore b/crates/swc_ecma_transforms_proposal/tests/.gitignore new file mode 100644 index 00000000000..bad9e806c54 --- /dev/null +++ b/crates/swc_ecma_transforms_proposal/tests/.gitignore @@ -0,0 +1 @@ +run-decorator-tests.js \ No newline at end of file diff --git a/crates/swc_ecma_transforms_proposal/tests/decorator-tests b/crates/swc_ecma_transforms_proposal/tests/decorator-tests new file mode 160000 index 00000000000..8e9c0b0fb3d --- /dev/null +++ b/crates/swc_ecma_transforms_proposal/tests/decorator-tests @@ -0,0 +1 @@ +Subproject commit 8e9c0b0fb3d548f378420aabbd351087efb5d5e5 diff --git a/crates/swc_ecma_transforms_proposal/tests/decorator_esbuild.rs b/crates/swc_ecma_transforms_proposal/tests/decorator_esbuild.rs new file mode 100644 index 00000000000..42c8d95b378 --- /dev/null +++ b/crates/swc_ecma_transforms_proposal/tests/decorator_esbuild.rs @@ -0,0 +1,81 @@ +use std::{fs, process::Command}; + +use swc_common::Mark; +use swc_ecma_ast::EsVersion; +use swc_ecma_codegen::to_code; +use swc_ecma_parser::parse_file_as_program; +use swc_ecma_transforms_base::{ + fixer::fixer, + helpers::{inject_helpers, Helpers, HELPERS}, + hygiene::hygiene, + resolver, +}; +use swc_ecma_transforms_proposal::decorator_2022_03::decorator_2022_03; +use swc_ecma_visit::VisitMutWith; +use testing::find_executable; + +#[test] +#[ignore = "TODO: Fix this test"] +fn execute() { + testing::run_test(false, |cm, handler| { + let node = find_executable("node").expect("node not found"); + + let fm = cm + .load_file("tests/decorator-tests/decorator-tests.js".as_ref()) + .expect("failed to load file"); + + let code = { + // Transpile with swc + let mut errors = vec![]; + + let program = parse_file_as_program( + &fm, + swc_ecma_parser::Syntax::Es(swc_ecma_parser::EsConfig { + decorators: true, + auto_accessors: true, + ..Default::default() + }), + EsVersion::EsNext, + None, + &mut errors, + ); + + let mut program = match program { + Ok(v) => v, + Err(e) => { + e.into_diagnostic(handler).emit(); + return Err(()); + } + }; + + for e in errors { + e.into_diagnostic(handler).emit(); + } + HELPERS.set(&Helpers::new(false), || { + let unresolved_mark = Mark::new(); + let top_level_mark = Mark::new(); + program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false)); + + program.visit_mut_with(&mut decorator_2022_03()); + + program.visit_mut_with(&mut inject_helpers(unresolved_mark)); + program.visit_mut_with(&mut hygiene()); + program.visit_mut_with(&mut fixer(None)); + }); + + to_code(&program) + }; + + fs::write("tests/run-decorator-tests.js", code).expect("failed to write file"); + + let status = Command::new(node) + .arg("tests/run-decorator-tests.js") + .status() + .expect("failed to execute process"); + + assert!(status.success()); + + Ok(()) + }) + .unwrap() +}