forked from swc-project/swc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es2015_new_target.rs
202 lines (182 loc) · 4.76 KB
/
es2015_new_target.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use std::{fs::read_to_string, path::PathBuf};
use serde::Deserialize;
use swc_common::{chain, Mark};
use swc_ecma_transforms_base::pass::noop;
use swc_ecma_transforms_compat::{
es2015::{arrow, classes, new_target::new_target},
es2022::class_properties,
};
use swc_ecma_transforms_testing::{exec_tr, parse_options, test, test_fixture, Tester};
use swc_ecma_visit::Fold;
fn get_passes(t: &Tester, plugins: &[PluginConfig]) -> Box<dyn Fold> {
let mut pass: Box<dyn Fold> = Box::new(noop());
for plugin in plugins {
let (name, option) = match plugin {
PluginConfig::WithOption(name, config) => (name, config.clone()),
PluginConfig::Name(name) => (name, serde_json::Value::Null),
};
let loose = option
.as_object()
.and_then(|opt| opt.get("loose"))
.and_then(|loose| {
if let Some(true) = loose.as_bool() {
Some(())
} else {
None
}
})
.is_some();
match &**name {
"transform-new-target" => {}
"proposal-class-properties" => {
pass = Box::new(chain!(
pass,
class_properties(
Some(t.comments.clone()),
class_properties::Config {
constant_super: loose,
set_public_fields: loose,
private_as_properties: loose,
no_document_all: loose
}
)
));
}
"transform-arrow-functions" => {
pass = Box::new(chain!(pass, arrow(Mark::new())));
}
_ => {
panic!("unknown pass: {}", name)
}
}
}
pass = Box::new(chain!(pass, new_target()));
pass
}
#[testing::fixture("tests/new-target/**/exec.js")]
fn exec(input: PathBuf) {
let options: TestOptions = parse_options(&input);
let input = read_to_string(&input).unwrap();
exec_tr(
"new-target",
Default::default(),
|t| get_passes(t, &options.plugins),
&input,
);
}
#[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/new-target/**/input.js")]
fn fixture(input: PathBuf) {
let options: TestOptions = parse_options(&input);
let output = input.parent().unwrap().join("output.js");
test_fixture(
Default::default(),
&|t| get_passes(t, &options.plugins),
&input,
&output,
Default::default(),
)
}
test!(
::swc_ecma_parser::Syntax::default(),
|_| new_target(),
edge_12,
r#"function foo() {
const a = () => new.target
}"#,
r#"function foo() {
const a = () => this instanceof foo ? this.constructor : void 0
}"#
);
test!(
::swc_ecma_parser::Syntax::default(),
|_| new_target(),
issue_4193,
r#"const v0 = Symbol("Context#bar");
module.exports = {
get bar() {
if (new.target === "foo") {
return;
}
return new Proxy(this, v0);
}
};"#,
r#"const v0 = Symbol("Context#bar");
module.exports = {
get bar() {
if (void 0 === "foo") {
return;
}
return new Proxy(this, v0);
}
};"#
);
test!(
::swc_ecma_parser::Syntax::default(),
|_| new_target(),
edge_13,
r#"
class A {
foo() {
return () => new.target
}
constructor() {
() => new.target
}
}
"#,
r#"
class A {
foo() {
return ()=>void 0;
}
constructor(){
()=>this.constructor;
}
}
"#
);
test!(
::swc_ecma_parser::Syntax::default(),
|t| chain!(
classes(Some(t.comments.clone()), Default::default()),
new_target()
),
issue_6259,
r#"
(() => {
var SomeError = class extends Error {
constructor(issues) {
super();
const actualProto = new.target.prototype;
}
}
})();
"#,
r#"
(()=>{
var SomeError = function _target(Error) {
"use strict";
_inherits(SomeError, Error);
var _super = _createSuper(SomeError);
function SomeError(issues) {
_classCallCheck(this, SomeError);
var _this = _super.call(this);
const actualProto = (this instanceof SomeError ? this.constructor : void 0).prototype;
return _this;
}
return SomeError;
}(_wrapNativeSuper(Error));
})();
"#
);