Skip to content

Commit 95472e4

Browse files
authored
test(es/decorators): Add tests written by evanw (swc-project#8967)
**Description:** Add https://github.com/evanw/decorator-tests as partially ignored tests so we can work on decorator fixes gradually.
1 parent 0d1c3b9 commit 95472e4

File tree

7 files changed

+92
-3
lines changed

7 files changed

+92
-3
lines changed

.gitmodules

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,7 @@
77
path = crates/swc_html_parser/tests/html5lib-tests
88
url = https://github.com/html5lib/html5lib-tests.git
99
shallow = true
10-
ignore = dirty
10+
ignore = dirty
11+
[submodule "crates/swc_ecma_transforms_proposal/tests/decorator-tests"]
12+
path = crates/swc_ecma_transforms_proposal/tests/decorator-tests
13+
url = https://github.com/evanw/decorator-tests

.vscode/settings.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"git.autoRepositoryDetection": false,
2020
"git.ignoredRepositories": [
2121
"crates/swc_ecma_parser/tests/test262-parser",
22-
"crates/swc_html_parser/tests/html5lib-tests"
22+
"crates/swc_html_parser/tests/html5lib-tests",
23+
"crates/swc_ecma_transforms_proposal/tests/decorator-tests"
2324
],
2425
"eslint.enable": false,
2526
"rust-analyzer.check.command": "clippy",
@@ -35,4 +36,4 @@
3536
"rkyv-impl",
3637
"debug"
3738
]
38-
}
39+
}

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/swc_ecma_transforms_proposal/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ swc_ecma_visit = { version = "0.99.1", path = "../swc_ecma_visit" }
3535
[dev-dependencies]
3636
serde_json = { workspace = true }
3737

38+
swc_ecma_codegen = { version = "0.149.1", path = "../swc_ecma_codegen" }
3839
swc_ecma_parser = { version = "0.144.1", path = "../swc_ecma_parser" }
3940
swc_ecma_transforms_compat = { version = "0.164.1", path = "../swc_ecma_transforms_compat" }
4041
swc_ecma_transforms_testing = { version = "0.141.1", path = "../swc_ecma_transforms_testing" }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
run-decorator-tests.js
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use std::{fs, process::Command};
2+
3+
use swc_common::Mark;
4+
use swc_ecma_ast::EsVersion;
5+
use swc_ecma_codegen::to_code;
6+
use swc_ecma_parser::parse_file_as_program;
7+
use swc_ecma_transforms_base::{
8+
fixer::fixer,
9+
helpers::{inject_helpers, Helpers, HELPERS},
10+
hygiene::hygiene,
11+
resolver,
12+
};
13+
use swc_ecma_transforms_proposal::decorator_2022_03::decorator_2022_03;
14+
use swc_ecma_visit::VisitMutWith;
15+
use testing::find_executable;
16+
17+
#[test]
18+
#[ignore = "TODO: Fix this test"]
19+
fn execute() {
20+
testing::run_test(false, |cm, handler| {
21+
let node = find_executable("node").expect("node not found");
22+
23+
let fm = cm
24+
.load_file("tests/decorator-tests/decorator-tests.js".as_ref())
25+
.expect("failed to load file");
26+
27+
let code = {
28+
// Transpile with swc
29+
let mut errors = vec![];
30+
31+
let program = parse_file_as_program(
32+
&fm,
33+
swc_ecma_parser::Syntax::Es(swc_ecma_parser::EsConfig {
34+
decorators: true,
35+
auto_accessors: true,
36+
..Default::default()
37+
}),
38+
EsVersion::EsNext,
39+
None,
40+
&mut errors,
41+
);
42+
43+
let mut program = match program {
44+
Ok(v) => v,
45+
Err(e) => {
46+
e.into_diagnostic(handler).emit();
47+
return Err(());
48+
}
49+
};
50+
51+
for e in errors {
52+
e.into_diagnostic(handler).emit();
53+
}
54+
HELPERS.set(&Helpers::new(false), || {
55+
let unresolved_mark = Mark::new();
56+
let top_level_mark = Mark::new();
57+
program.visit_mut_with(&mut resolver(unresolved_mark, top_level_mark, false));
58+
59+
program.visit_mut_with(&mut decorator_2022_03());
60+
61+
program.visit_mut_with(&mut inject_helpers(unresolved_mark));
62+
program.visit_mut_with(&mut hygiene());
63+
program.visit_mut_with(&mut fixer(None));
64+
});
65+
66+
to_code(&program)
67+
};
68+
69+
fs::write("tests/run-decorator-tests.js", code).expect("failed to write file");
70+
71+
let status = Command::new(node)
72+
.arg("tests/run-decorator-tests.js")
73+
.status()
74+
.expect("failed to execute process");
75+
76+
assert!(status.success());
77+
78+
Ok(())
79+
})
80+
.unwrap()
81+
}

0 commit comments

Comments
 (0)