-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
Copy patharrow_functions.rs
346 lines (312 loc) · 11.9 KB
/
arrow_functions.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//! ES2015 Arrow Functions
//!
//! This plugin transforms arrow functions (`() => {}`) to function expressions (`function () {}`).
//!
//! > This plugin is included in `preset-env`, in ES2015
//!
//! ## Example
//!
//! Input:
//! ```js
//! var a = () => {};
//! var a = b => b;
//!
//! const double = [1, 2, 3].map(num => num * 2);
//! console.log(double); // [2,4,6]
//!
//! var bob = {
//! _name: "Bob",
//! _friends: ["Sally", "Tom"],
//! printFriends() {
//! this._friends.forEach(f => console.log(this._name + " knows " + f));
//! },
//! };
//! console.log(bob.printFriends());
//! ```
//!
//! Output:
//! ```js
//! var a = function() {};
//! var a = function(b) {
//! return b;
//! };
//!
//! const double = [1, 2, 3].map(function(num) {
//! return num * 2;
//! });
//! console.log(double); // [2,4,6]
//!
//! var bob = {
//! _name: "Bob",
//! _friends: ["Sally", "Tom"],
//! printFriends() {
//! var _this = this;
//!
//! this._friends.forEach(function(f) {
//! return console.log(_this._name + " knows " + f);
//! });
//! },
//! };
//! console.log(bob.printFriends());
//! ```
//!
//! ## Implementation
//!
//! Implementation based on [@babel/plugin-transform-exponentiation-operator](https://babel.dev/docs/babel-plugin-transform-arrow-functions).
//!
//! ## References:
//!
//! * Babel plugin implementation: <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-arrow-functions>
//! * Arrow function specification: <https://tc39.es/ecma262/#sec-arrow-function-definitions>
use std::cell::Cell;
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_span::SPAN;
use oxc_syntax::{scope::ScopeFlags, symbol::SymbolFlags};
use oxc_traverse::{Traverse, TraverseCtx};
use serde::Deserialize;
use crate::{context::Ctx, helpers::bindings::BoundIdentifier};
#[derive(Debug, Default, Clone, Deserialize)]
pub struct ArrowFunctionsOptions {
/// This option enables the following:
/// * Wrap the generated function in .bind(this) and keeps uses of this inside the function as-is, instead of using a renamed this.
/// * Add a runtime check to ensure the functions are not instantiated.
/// * Add names to arrow functions.
#[serde(default)]
pub spec: bool,
}
pub struct ArrowFunctions<'a> {
ctx: Ctx<'a>,
_options: ArrowFunctionsOptions,
this_vars: std::vec::Vec<Option<BoundIdentifier<'a>>>,
/// Stack to keep track of whether we are inside an arrow function or not.
stacks: std::vec::Vec<bool>,
}
impl<'a> ArrowFunctions<'a> {
pub fn new(options: ArrowFunctionsOptions, ctx: Ctx<'a>) -> Self {
Self {
ctx,
_options: options,
// Reserve for the global scope
this_vars: vec![None],
stacks: vec![],
}
}
}
impl<'a> Traverse<'a> for ArrowFunctions<'a> {
/// Insert `var _this = this;` for the global scope.
fn exit_program(&mut self, program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
self.insert_this_var_statement_at_the_top_of_statements(&mut program.body);
}
fn enter_function(&mut self, func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
if func.body.is_some() {
self.this_vars.push(None);
}
}
/// ```ts
/// function a(){
/// () => console.log(this);
/// }
/// // to
/// function a(){
/// var _this = this;
/// (function() { return console.log(_this); });
/// }
/// ```
/// Insert the var _this = this; statement outside the arrow function
fn exit_function(&mut self, func: &mut Function<'a>, _ctx: &mut TraverseCtx<'a>) {
let Some(body) = func.body.as_mut() else {
return;
};
self.insert_this_var_statement_at_the_top_of_statements(&mut body.statements);
}
/// Change <this></this> to <_this></_this>, and mark it as found
fn enter_jsx_element_name(&mut self, name: &mut JSXElementName<'a>, ctx: &mut TraverseCtx<'a>) {
if !self.is_inside_arrow_function() {
return;
}
let ident = match name {
JSXElementName::Identifier(ident) => ident,
JSXElementName::MemberExpression(member_expr) => {
member_expr.get_object_identifier_mut()
}
JSXElementName::IdentifierReference(_) | JSXElementName::NamespacedName(_) => return,
};
if ident.name == "this" {
// We can't produce a proper identifier with a `ReferenceId` because `JSXIdentifier`
// lacks that field. https://github.com/oxc-project/oxc/issues/3528
// So generate a reference and just use its name.
// If JSX transform is enabled, that transform runs before this and will have converted
// this to a proper `ThisExpression`, and this visitor won't run.
// So only a problem if JSX transform is disabled.
let new_ident = self.get_this_name(ctx).create_read_reference(ctx);
ident.name = new_ident.name;
}
}
fn enter_expression(&mut self, expr: &mut Expression<'a>, _ctx: &mut TraverseCtx<'a>) {
match expr {
Expression::ArrowFunctionExpression(_) => {
self.stacks.push(true);
}
Expression::FunctionExpression(_) => self.stacks.push(false),
_ => {}
}
}
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
match expr {
Expression::ThisExpression(this_expr) => {
if !self.is_inside_arrow_function() {
return;
}
let ident =
self.get_this_name(ctx).create_spanned_read_reference(this_expr.span, ctx);
*expr = self.ctx.ast.expression_from_identifier_reference(ident);
}
Expression::ArrowFunctionExpression(arrow_function_expr) => {
*expr = self.transform_arrow_function_expression(arrow_function_expr, ctx);
self.stacks.pop();
}
Expression::FunctionExpression(_) => {
self.stacks.pop();
}
_ => {}
}
}
fn enter_declaration(&mut self, decl: &mut Declaration<'a>, _ctx: &mut TraverseCtx<'a>) {
if let Declaration::FunctionDeclaration(_) = decl {
self.stacks.push(false);
}
}
fn exit_declaration(&mut self, decl: &mut Declaration<'a>, _ctx: &mut TraverseCtx<'a>) {
if let Declaration::FunctionDeclaration(_) = decl {
self.stacks.pop();
}
}
fn enter_class(&mut self, _class: &mut Class<'a>, _ctx: &mut TraverseCtx<'a>) {
self.stacks.push(false);
}
fn exit_class(&mut self, _class: &mut Class<'a>, _ctx: &mut TraverseCtx<'a>) {
self.stacks.pop();
}
fn enter_variable_declarator(
&mut self,
node: &mut VariableDeclarator<'a>,
ctx: &mut TraverseCtx<'a>,
) {
if !matches!(node.init, Some(Expression::ArrowFunctionExpression(_))) {
return;
}
let Some(id) = node.id.get_binding_identifier() else { return };
*ctx.symbols_mut().get_flags_mut(id.symbol_id.get().unwrap()) &=
!SymbolFlags::ArrowFunction;
}
}
impl<'a> ArrowFunctions<'a> {
fn is_inside_arrow_function(&self) -> bool {
self.stacks.last().copied().unwrap_or(false)
}
fn get_this_name(&mut self, ctx: &mut TraverseCtx<'a>) -> BoundIdentifier<'a> {
let this_var = self.this_vars.last_mut().unwrap();
if this_var.is_none() {
let target_scope_id =
ctx.scopes().ancestors(ctx.current_scope_id()).skip(1).find(|scope_id| {
let scope_flags = ctx.scopes().get_flags(*scope_id);
scope_flags.intersects(ScopeFlags::Function | ScopeFlags::Top)
&& !scope_flags.contains(ScopeFlags::Arrow)
});
this_var.replace(BoundIdentifier::new_uid(
"this",
target_scope_id.unwrap(),
SymbolFlags::FunctionScopedVariable,
ctx,
));
}
this_var.as_ref().unwrap().clone()
}
fn transform_arrow_function_expression(
&mut self,
arrow_function_expr: &mut ArrowFunctionExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
// SAFETY: `ast.copy` is unsound! We need to fix.
let mut body = unsafe { self.ctx.ast.copy(&arrow_function_expr.body) };
if arrow_function_expr.expression {
let first_stmt = body.statements.remove(0);
if let Statement::ExpressionStatement(stmt) = first_stmt {
let return_statement = self.ctx.ast.statement_return(
stmt.span,
// SAFETY: `ast.copy` is unsound! We need to fix.
Some(unsafe { self.ctx.ast.copy(&stmt.expression) }),
);
body.statements.push(return_statement);
}
}
// There shouldn't need to be a conditional here. Every arrow function should have a scope ID.
// But at present TS transforms don't seem to set `scope_id` in some cases, so this test case
// fails if just unwrap `scope_id`:
// `typescript/tests/cases/compiler/classFieldSuperAccessible.ts`.
// ```ts
// class D {
// accessor b = () => {}
// }
// ```
// TODO: Change to `arrow_function_expr.scope_id.get().unwrap()` once scopes are correct
// in TS transforms.
let scope_id = arrow_function_expr.scope_id.get();
if let Some(scope_id) = scope_id {
let flags = ctx.scopes_mut().get_flags_mut(scope_id);
*flags &= !ScopeFlags::Arrow;
}
let new_function = Function {
r#type: FunctionType::FunctionExpression,
span: arrow_function_expr.span,
id: None,
generator: false,
r#async: arrow_function_expr.r#async,
declare: false,
this_param: None,
// SAFETY: `ast.copy` is unsound! We need to fix.
params: unsafe { self.ctx.ast.copy(&arrow_function_expr.params) },
body: Some(body),
// SAFETY: `ast.copy` is unsound! We need to fix.
type_parameters: unsafe { self.ctx.ast.copy(&arrow_function_expr.type_parameters) },
// SAFETY: `ast.copy` is unsound! We need to fix.
return_type: unsafe { self.ctx.ast.copy(&arrow_function_expr.return_type) },
scope_id: Cell::new(scope_id),
};
let expr = Expression::FunctionExpression(self.ctx.ast.alloc(new_function));
// Avoid creating a function declaration.
// `() => {};` => `(function () {});`
self.ctx.ast.expression_parenthesized(SPAN, expr)
}
/// Insert `var _this = this;` at the top of the statements.
fn insert_this_var_statement_at_the_top_of_statements(
&mut self,
statements: &mut Vec<'a, Statement<'a>>,
) {
if let Some(id) = &self.this_vars.pop().unwrap() {
let binding_pattern = self.ctx.ast.binding_pattern(
self.ctx
.ast
.binding_pattern_kind_from_binding_identifier(id.create_binding_identifier()),
Option::<TSTypeAnnotation>::None,
false,
);
let variable_declarator = self.ctx.ast.variable_declarator(
SPAN,
VariableDeclarationKind::Var,
binding_pattern,
Some(self.ctx.ast.expression_this(SPAN)),
false,
);
let stmt = self.ctx.ast.alloc_variable_declaration(
SPAN,
VariableDeclarationKind::Var,
self.ctx.ast.vec1(variable_declarator),
false,
);
let stmt = Statement::VariableDeclaration(stmt);
statements.insert(0, stmt);
}
}
}