-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
es2022_private_in_object.rs
110 lines (94 loc) · 3.63 KB
/
es2022_private_in_object.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::path::PathBuf;
use serde::Deserialize;
use swc_common::chain;
use swc_ecma_transforms_base::pass::noop;
use swc_ecma_transforms_compat::{
es2015::classes,
es2022::{class_properties, private_in_object},
};
use swc_ecma_transforms_testing::{parse_options, test_fixture, FixtureTestConfig};
use swc_ecma_visit::Fold;
#[derive(Debug, Clone, Deserialize)]
#[serde(deny_unknown_fields)]
struct TestOptions {
plugins: Vec<PluginConfig>,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
enum PluginConfig {
WithOption(String, #[serde(default)] serde_json::Value),
Name(String),
}
#[testing::fixture("tests/private-in-object/**/input.js")]
fn fixture(input: PathBuf) {
let parent = input.parent().unwrap();
let options: TestOptions = parse_options(parent);
let output = parent.join("output.js");
test_fixture(
Default::default(),
&|t| {
let mut pass: Box<dyn Fold> = Box::new(noop());
let mut class_props = false;
for plugin in &options.plugins {
let (name, _option) = match plugin {
PluginConfig::WithOption(name, config) => (name, config.clone()),
PluginConfig::Name(name) => (name, serde_json::Value::Null),
};
let loose = input.to_string_lossy().contains("private-loose");
match &**name {
"proposal-private-property-in-object" => {}
"proposal-class-properties" => {
if !class_props {
class_props = true;
pass = Box::new(chain!(
pass,
class_properties(
Some(t.comments.clone()),
class_properties::Config {
set_public_fields: loose,
constant_super: loose,
no_document_all: loose,
private_as_properties: loose
}
)
));
}
}
"proposal-private-methods" => {
if !class_props {
class_props = true;
pass = Box::new(chain!(
pass,
class_properties(
Some(t.comments.clone()),
class_properties::Config {
set_public_fields: loose,
constant_super: loose,
no_document_all: loose,
private_as_properties: loose
}
)
));
}
}
"transform-classes" => {
pass = Box::new(chain!(
pass,
classes(Some(t.comments.clone()), Default::default())
));
}
_ => {
panic!("unknown pass: {}", name)
}
}
}
pass = Box::new(chain!(pass, private_in_object()));
pass
},
&input,
&output,
FixtureTestConfig {
..Default::default()
},
)
}