-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
Copy pathasync_to_generator.rs
702 lines (647 loc) · 26.4 KB
/
async_to_generator.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//! ES2017: Async / Await \[WIP\]
//!
//! This plugin transforms async functions to generator functions and wraps them in the asyncToGenerator helper function.
//!
//! ## Example
//!
//! Input:
//! ```js
//! async function foo() {
//! await bar();
//! }
//! const foo2 = async () => {
//! await bar();
//! };
//! async () => {
//! await bar();
//! }
//! ```
//!
//! Output:
//! ```js
//! function foo() {
//! return _foo.apply(this, arguments);
//! }
//! function _foo() {
//! _foo = babelHelpers.asyncToGenerator(function* () {
//! yield bar();
//! });
//! return _foo.apply(this, arguments);
//! }
//! const foo2 = function() {
//! var _ref = babelHelpers.asyncToGenerator(function* () {
//! yield bar();
//! });
//! return function foo2() {
//! return _ref.apply(this, arguments);
//! };
//! }();
//! babelHelpers.asyncToGenerator(function* () {
//! yield bar();
//! });
//! ```
//!
//! ## Implementation
//!
//! Implementation based on [@babel/plugin-transform-async-to-generator](https://babel.dev/docs/babel-plugin-transform-async-to-generator).
//!
//! Reference:
//! * Babel docs: <https://babeljs.io/docs/en/babel-plugin-transform-async-to-generator>
//! * Babel implementation: <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-async-to-generator>
//! * Async / Await TC39 proposal: <https://github.com/tc39/proposal-async-await>
use std::mem;
use oxc_allocator::{Box, GetAddress};
use oxc_ast::ast::*;
use oxc_ast::Visit;
use oxc_ast::NONE;
use oxc_semantic::{ReferenceFlags, ScopeFlags, ScopeId, SymbolFlags};
use oxc_span::{Atom, GetSpan, SPAN};
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};
use crate::{common::helper_loader::Helper, TransformCtx};
pub struct AsyncToGenerator<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,
}
impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self {
Self { ctx }
}
}
impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let new_expr = match expr {
Expression::AwaitExpression(await_expr) => {
self.transform_await_expression(await_expr, ctx)
}
Expression::FunctionExpression(func) => self.transform_function_expression(func, ctx),
Expression::ArrowFunctionExpression(arrow) => self.transform_arrow_function(arrow, ctx),
_ => None,
};
if let Some(new_expr) = new_expr {
*expr = new_expr;
}
}
fn exit_statement(&mut self, stmt: &mut Statement<'a>, ctx: &mut TraverseCtx<'a>) {
let new_statement = match stmt {
Statement::FunctionDeclaration(func) => self.transform_function_declaration(func, ctx),
Statement::ExportDefaultDeclaration(decl) => {
if let ExportDefaultDeclarationKind::FunctionDeclaration(func) =
&mut decl.declaration
{
self.transform_function_declaration(func, ctx)
} else {
None
}
}
Statement::ExportNamedDeclaration(decl) => {
if let Some(Declaration::FunctionDeclaration(func)) = &mut decl.declaration {
self.transform_function_declaration(func, ctx)
} else {
None
}
}
_ => None,
};
if let Some(new_statement) = new_statement {
self.ctx.statement_injector.insert_after(stmt.address(), new_statement);
}
}
fn exit_method_definition(
&mut self,
node: &mut MethodDefinition<'a>,
ctx: &mut TraverseCtx<'a>,
) {
self.transform_function_for_method_definition(&mut node.value, ctx);
}
}
impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
/// Transforms `await` expressions to `yield` expressions.
/// Ignores top-level await expressions.
#[allow(clippy::unused_self)]
fn transform_await_expression(
&self,
expr: &mut Box<'a, AwaitExpression<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
// We don't need to handle top-level await.
if ctx.parent().is_program() {
None
} else {
Some(ctx.ast.expression_yield(
SPAN,
false,
Some(ctx.ast.move_expression(&mut expr.argument)),
))
}
}
/// Transforms async method definitions to generator functions wrapped in asyncToGenerator.
///
/// ## Example
///
/// Input:
/// ```js
/// class A { async foo() { await bar(); } }
/// ```
///
/// Output:
/// ```js
/// class A {
/// foo() {
/// return babelHelpers.asyncToGenerator(function* () {
/// yield bar();
/// })();
/// }
/// ```
fn transform_function_for_method_definition(
&self,
func: &mut Box<'a, Function<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
if !func.r#async {
return;
}
let Some(body) = func.body.take() else {
return;
};
let (generator_scope_id, wrapper_scope_id) = {
let new_scope_id = ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
let scope_id = func.scope_id.replace(Some(new_scope_id)).unwrap();
// We need to change the parent id to new scope id because we need to this function's body inside the wrapper function,
// and then the new scope id will be wrapper function's scope id.
ctx.scopes_mut().change_parent_id(scope_id, Some(new_scope_id));
// We need to transform formal parameters change back to the original scope,
// because we only move out the function body.
BindingMover::new(new_scope_id, ctx).visit_formal_parameters(&func.params);
(scope_id, new_scope_id)
};
let params = Self::create_empty_params(ctx);
let expression = self.create_async_to_generator_call(params, body, generator_scope_id, ctx);
// Construct the IIFE
let expression = ctx.ast.expression_call(SPAN, expression, NONE, ctx.ast.vec(), false);
let statement = ctx.ast.statement_return(SPAN, Some(expression));
// Modify the wrapper function
func.r#async = false;
func.body = Some(ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), ctx.ast.vec1(statement)));
func.scope_id.set(Some(wrapper_scope_id));
}
/// Transforms [`Function`] whose type is [`FunctionType::FunctionExpression`] to a generator function
/// and wraps it in asyncToGenerator helper function.
fn transform_function_expression(
&self,
wrapper_function: &mut Box<'a, Function<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
if !wrapper_function.r#async
|| wrapper_function.generator
|| wrapper_function.is_typescript_syntax()
{
return None;
}
let body = wrapper_function.body.take().unwrap();
let params = ctx.alloc(ctx.ast.move_formal_parameters(&mut wrapper_function.params));
let id = wrapper_function.id.take();
let has_function_id = id.is_some();
if !has_function_id && !Self::is_function_length_affected(¶ms) {
return Some(self.create_async_to_generator_call(
params,
body,
wrapper_function.scope_id.take().unwrap(),
ctx,
));
}
let (generator_scope_id, wrapper_scope_id) = {
let wrapper_scope_id =
ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
let scope_id = wrapper_function.scope_id.replace(Some(wrapper_scope_id)).unwrap();
// Change the parent scope of the function scope with the current scope.
ctx.scopes_mut().change_parent_id(scope_id, Some(wrapper_scope_id));
// If there is an id, then we will use it as the name of caller_function,
// and the caller_function is inside the wrapper function.
// so we need to move the id to the new scope.
if let Some(id) = id.as_ref() {
BindingMover::new(wrapper_scope_id, ctx).visit_binding_identifier(id);
let symbol_id = id.symbol_id.get().unwrap();
*ctx.symbols_mut().get_flags_mut(symbol_id) = SymbolFlags::FunctionScopedVariable;
}
(scope_id, wrapper_scope_id)
};
let bound_ident = Self::create_bound_identifier(id.as_ref(), wrapper_scope_id, ctx);
let caller_function = {
let scope_id = ctx.create_child_scope(wrapper_scope_id, ScopeFlags::Function);
let params = Self::create_placeholder_params(¶ms, scope_id, ctx);
let statements = ctx.ast.vec1(Self::create_apply_call_statement(&bound_ident, ctx));
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
let id = id.or_else(|| {
Self::infer_function_id_from_variable_declarator(wrapper_scope_id, ctx)
});
Self::create_function(id, params, body, scope_id, ctx)
};
{
// Modify the wrapper function to add new body, params, and scope_id.
let mut statements = ctx.ast.vec_with_capacity(3);
let statement = self.create_async_to_generator_declaration(
&bound_ident,
params,
body,
generator_scope_id,
ctx,
);
statements.push(statement);
if has_function_id {
let id = caller_function.id.as_ref().unwrap();
// If the function has an id, then we need to return the id.
// `function foo() { ... }` -> `function foo() {} return foo;`
let reference = ctx.create_bound_reference_id(
SPAN,
id.name.clone(),
id.symbol_id.get().unwrap(),
ReferenceFlags::Read,
);
let statement = Statement::from(ctx.ast.declaration_from_function(caller_function));
statements.push(statement);
let argument = Some(ctx.ast.expression_from_identifier_reference(reference));
statements.push(ctx.ast.statement_return(SPAN, argument));
} else {
// If the function doesn't have an id, then we need to return the function itself.
// `function() { ... }` -> `return function() { ... };`
let statement_return = ctx.ast.statement_return(
SPAN,
Some(ctx.ast.expression_from_function(caller_function)),
);
statements.push(statement_return);
}
debug_assert!(wrapper_function.body.is_none());
wrapper_function.r#async = false;
wrapper_function.body.replace(ctx.ast.alloc_function_body(
SPAN,
ctx.ast.vec(),
statements,
));
}
// Construct the IIFE
let callee = ctx.ast.expression_from_function(ctx.ast.move_function(wrapper_function));
Some(ctx.ast.expression_call(SPAN, callee, NONE, ctx.ast.vec(), false))
}
/// Transforms async function declarations into generator functions wrapped in the asyncToGenerator helper.
fn transform_function_declaration(
&self,
wrapper_function: &mut Box<'a, Function<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Statement<'a>> {
if !wrapper_function.r#async
|| wrapper_function.generator
|| wrapper_function.is_typescript_syntax()
{
return None;
}
let (generator_scope_id, wrapper_scope_id) = {
let wrapper_scope_id =
ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
let scope_id = wrapper_function.scope_id.replace(Some(wrapper_scope_id)).unwrap();
// Change the parent scope of the function scope with the current scope.
ctx.scopes_mut().change_parent_id(scope_id, Some(wrapper_scope_id));
(scope_id, wrapper_scope_id)
};
let body = wrapper_function.body.take().unwrap();
let params =
Self::create_placeholder_params(&wrapper_function.params, wrapper_scope_id, ctx);
let params = mem::replace(&mut wrapper_function.params, params);
let bound_ident = Self::create_bound_identifier(
wrapper_function.id.as_ref(),
ctx.current_scope_id(),
ctx,
);
// Modify the wrapper function
{
wrapper_function.r#async = false;
let statements = ctx.ast.vec1(Self::create_apply_call_statement(&bound_ident, ctx));
debug_assert!(wrapper_function.body.is_none());
wrapper_function.body.replace(ctx.ast.alloc_function_body(
SPAN,
ctx.ast.vec(),
statements,
));
}
// function _name() { _ref.apply(this, arguments); }
{
let mut statements = ctx.ast.vec_with_capacity(2);
statements.push(self.create_async_to_generator_assignment(
&bound_ident,
params,
body,
generator_scope_id,
ctx,
));
statements.push(Self::create_apply_call_statement(&bound_ident, ctx));
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
let scope_id = ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
// The generator function will move to this function, so we need
// to change the parent scope of the generator function to the scope of this function.
ctx.scopes_mut().change_parent_id(generator_scope_id, Some(scope_id));
let params = Self::create_empty_params(ctx);
let id = Some(bound_ident.create_binding_identifier(ctx));
let caller_function = Self::create_function(id, params, body, scope_id, ctx);
Some(Statement::from(ctx.ast.declaration_from_function(caller_function)))
}
}
/// Transforms async arrow functions into generator functions wrapped in the asyncToGenerator helper.
fn transform_arrow_function(
&self,
arrow: &mut ArrowFunctionExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
if !arrow.r#async {
return None;
}
let mut body = ctx.ast.move_function_body(&mut arrow.body);
// If the arrow's expression is true, we need to wrap the only one expression with return statement.
if arrow.expression {
let statement = body.statements.first_mut().unwrap();
let expression = match statement {
Statement::ExpressionStatement(es) => ctx.ast.move_expression(&mut es.expression),
_ => unreachable!(),
};
*statement = ctx.ast.statement_return(expression.span(), Some(expression));
}
let params = ctx.alloc(ctx.ast.move_formal_parameters(&mut arrow.params));
let generator_function_id = arrow.scope_id.get().unwrap();
ctx.scopes_mut().get_flags_mut(generator_function_id).remove(ScopeFlags::Arrow);
if !Self::is_function_length_affected(¶ms) {
return Some(self.create_async_to_generator_call(
params,
ctx.ast.alloc(body),
generator_function_id,
ctx,
));
}
let wrapper_scope_id = ctx.create_child_scope(ctx.current_scope_id(), ScopeFlags::Function);
// The generator function will move to inside wrapper, so we need
// to change the parent scope of the generator function to the wrapper function.
ctx.scopes_mut().change_parent_id(generator_function_id, Some(wrapper_scope_id));
let bound_ident = Self::create_bound_identifier(None, wrapper_scope_id, ctx);
let caller_function = {
let scope_id = ctx.create_child_scope(wrapper_scope_id, ScopeFlags::Function);
let params = Self::create_placeholder_params(¶ms, scope_id, ctx);
let statements = ctx.ast.vec1(Self::create_apply_call_statement(&bound_ident, ctx));
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
let id = Self::infer_function_id_from_variable_declarator(wrapper_scope_id, ctx);
let function = Self::create_function(id, params, body, scope_id, ctx);
let argument = Some(ctx.ast.expression_from_function(function));
ctx.ast.statement_return(SPAN, argument)
};
// Wrapper function
{
let statement = self.create_async_to_generator_declaration(
&bound_ident,
params,
ctx.ast.alloc(body),
generator_function_id,
ctx,
);
let mut statements = ctx.ast.vec_with_capacity(2);
statements.push(statement);
statements.push(caller_function);
let body = ctx.ast.alloc_function_body(SPAN, ctx.ast.vec(), statements);
let params = Self::create_empty_params(ctx);
let wrapper_function = Self::create_function(None, params, body, wrapper_scope_id, ctx);
// Construct the IIFE
let callee = ctx.ast.expression_from_function(wrapper_function);
Some(ctx.ast.expression_call(SPAN, callee, NONE, ctx.ast.vec(), false))
}
}
/// Infers the function id from [`Ancestor::VariableDeclaratorInit`].
#[inline]
fn infer_function_id_from_variable_declarator(
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Option<BindingIdentifier<'a>> {
let Ancestor::VariableDeclaratorInit(declarator) = ctx.parent() else {
return None;
};
let Some(id) = declarator.id().get_binding_identifier() else { unreachable!() };
Some(
ctx.generate_binding(id.name.clone(), scope_id, SymbolFlags::FunctionScopedVariable)
.create_binding_identifier(ctx),
)
}
/// Creates a [`Function`] with the specified params, body and scope_id.
#[inline]
fn create_function(
id: Option<BindingIdentifier<'a>>,
params: Box<'a, FormalParameters<'a>>,
body: Box<'a, FunctionBody<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Function<'a> {
let r#type = if id.is_some() {
FunctionType::FunctionDeclaration
} else {
FunctionType::FunctionExpression
};
ctx.ast.function_with_scope_id(
r#type,
SPAN,
id,
false,
false,
false,
NONE,
NONE,
params,
NONE,
Some(body),
scope_id,
)
}
/// Creates a [`Statement`] that calls the `apply` method on the bound identifier.
///
/// The generated code structure is:
/// ```js
/// bound_ident.apply(this, arguments);
/// ```
#[inline]
fn create_apply_call_statement(
bound_ident: &BoundIdentifier<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "arguments");
let arguments_ident =
ctx.create_reference_id(SPAN, Atom::from("arguments"), symbol_id, ReferenceFlags::Read);
let arguments_ident = ctx.ast.expression_from_identifier_reference(arguments_ident);
// (this, arguments)
let mut arguments = ctx.ast.vec_with_capacity(2);
arguments.push(ctx.ast.argument_expression(ctx.ast.expression_this(SPAN)));
arguments.push(ctx.ast.argument_expression(arguments_ident));
// _ref.apply
let callee = ctx.ast.expression_member(ctx.ast.member_expression_static(
SPAN,
bound_ident.create_read_expression(ctx),
ctx.ast.identifier_name(SPAN, "apply"),
false,
));
let argument = ctx.ast.expression_call(SPAN, callee, NONE, arguments, false);
ctx.ast.statement_return(SPAN, Some(argument))
}
/// Creates an [`Expression`] that calls the [`Helper::AsyncToGenerator`] helper function.
///
/// This function constructs the helper call with arguments derived from the provided
/// parameters, body, and scope_id.
///
/// The generated code structure is:
/// ```js
/// asyncToGenerator(function* (PARAMS) {
/// BODY
/// });
/// ```
#[inline]
fn create_async_to_generator_call(
&self,
params: Box<'a, FormalParameters<'a>>,
body: Box<'a, FunctionBody<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let mut function = Self::create_function(None, params, body, scope_id, ctx);
function.generator = true;
let function_expression = ctx.ast.expression_from_function(function);
let argument = ctx.ast.argument_expression(function_expression);
let arguments = ctx.ast.vec1(argument);
self.ctx.helper_call_expr(Helper::AsyncToGenerator, arguments, ctx)
}
/// Creates a helper declaration statement for async-to-generator transformation.
///
/// This function generates code that looks like:
/// ```js
/// var _ref = asyncToGenerator(function* (PARAMS) {
/// BODY
/// });
/// ```
#[inline]
fn create_async_to_generator_declaration(
&self,
bound_ident: &BoundIdentifier<'a>,
params: Box<'a, FormalParameters<'a>>,
body: Box<'a, FunctionBody<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let init = self.create_async_to_generator_call(params, body, scope_id, ctx);
let declarations = ctx.ast.vec1(ctx.ast.variable_declarator(
SPAN,
VariableDeclarationKind::Var,
bound_ident.create_binding_pattern(ctx),
Some(init),
false,
));
ctx.ast.statement_declaration(ctx.ast.declaration_variable(
SPAN,
VariableDeclarationKind::Var,
declarations,
false,
))
}
/// Creates a helper assignment statement for async-to-generator transformation.
///
/// This function generates code that looks like:
/// ```js
/// _ref = asyncToGenerator(function* (PARAMS) {
/// BODY
/// });
/// ```
#[inline]
fn create_async_to_generator_assignment(
&self,
bound: &BoundIdentifier<'a>,
params: Box<'a, FormalParameters<'a>>,
body: Box<'a, FunctionBody<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Statement<'a> {
let right = self.create_async_to_generator_call(params, body, scope_id, ctx);
let expression = ctx.ast.expression_assignment(
SPAN,
AssignmentOperator::Assign,
bound.create_write_target(ctx),
right,
);
ctx.ast.statement_expression(SPAN, expression)
}
/// Creates placeholder [`FormalParameters`] which named `_x` based on the passed-in parameters.
/// `function p(x, y, z, d = 0, ...rest) {}` -> `function* (_x, _x1, _x2) {}`
#[inline]
fn create_placeholder_params(
params: &FormalParameters<'a>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> Box<'a, FormalParameters<'a>> {
let mut parameters = ctx.ast.vec_with_capacity(params.items.len());
for param in ¶ms.items {
if param.pattern.kind.is_assignment_pattern() {
break;
}
let binding = ctx.generate_uid("x", scope_id, SymbolFlags::FunctionScopedVariable);
parameters.push(
ctx.ast.plain_formal_parameter(param.span(), binding.create_binding_pattern(ctx)),
);
}
let parameters = ctx.ast.alloc_formal_parameters(
SPAN,
FormalParameterKind::FormalParameter,
parameters,
NONE,
);
parameters
}
/// Creates an empty [FormalParameters] with [FormalParameterKind::FormalParameter].
#[inline]
fn create_empty_params(ctx: &mut TraverseCtx<'a>) -> Box<'a, FormalParameters<'a>> {
ctx.ast.alloc_formal_parameters(
SPAN,
FormalParameterKind::FormalParameter,
ctx.ast.vec(),
NONE,
)
}
/// Creates a [`BoundIdentifier`] for the id of the function.
#[inline]
fn create_bound_identifier(
id: Option<&BindingIdentifier<'a>>,
scope_id: ScopeId,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
ctx.generate_uid(
id.as_ref().map_or_else(|| "ref", |id| id.name.as_str()),
scope_id,
SymbolFlags::FunctionScopedVariable,
)
}
/// Checks if the function length is affected by the parameters.
///
/// TODO: Needs to handle `ignoreFunctionLength` assumption.
// <https://github.com/babel/babel/blob/3bcfee232506a4cebe410f02042fb0f0adeeb0b1/packages/babel-helper-wrap-function/src/index.ts#L164>
#[inline]
fn is_function_length_affected(params: &Box<'_, FormalParameters<'_>>) -> bool {
params.items.first().is_some_and(|param| !param.pattern.kind.is_assignment_pattern())
}
}
/// Moves the bindings from original scope to target scope.
struct BindingMover<'a, 'ctx> {
ctx: &'ctx mut TraverseCtx<'a>,
target_scope_id: ScopeId,
}
impl<'a, 'ctx> BindingMover<'a, 'ctx> {
fn new(target_scope_id: ScopeId, ctx: &'ctx mut TraverseCtx<'a>) -> Self {
Self { ctx, target_scope_id }
}
}
impl<'a, 'ctx> Visit<'a> for BindingMover<'a, 'ctx> {
/// Visits a binding identifier and moves it to the target scope.
fn visit_binding_identifier(&mut self, ident: &BindingIdentifier<'a>) {
let symbols = self.ctx.symbols();
let symbol_id = ident.symbol_id.get().unwrap();
let current_scope_id = symbols.get_scope_id(symbol_id);
let scopes = self.ctx.scopes_mut();
scopes.move_binding(current_scope_id, self.target_scope_id, ident.name.as_str());
let symbols = self.ctx.symbols_mut();
symbols.set_scope_id(symbol_id, self.target_scope_id);
}
}