Skip to content

Commit

Permalink
test(es/decorators): Add tests written by evanw (#8967)
Browse files Browse the repository at this point in the history
**Description:**

Add https://github.com/evanw/decorator-tests as partially ignored tests so we can work on decorator fixes gradually.
  • Loading branch information
kdy1 committed May 23, 2024
1 parent 0d1c3b9 commit 95472e4
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 3 deletions.
5 changes: 4 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@
path = crates/swc_html_parser/tests/html5lib-tests
url = https://github.com/html5lib/html5lib-tests.git
shallow = true
ignore = dirty
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
5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -35,4 +36,4 @@
"rkyv-impl",
"debug"
]
}
}
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_proposal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_proposal/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
run-decorator-tests.js
1 change: 1 addition & 0 deletions crates/swc_ecma_transforms_proposal/tests/decorator-tests
Submodule decorator-tests added at 8e9c0b
81 changes: 81 additions & 0 deletions crates/swc_ecma_transforms_proposal/tests/decorator_esbuild.rs
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit 95472e4

Please sign in to comment.