-
-
Notifications
You must be signed in to change notification settings - Fork 466
/
exponentiation_operator.rs
590 lines (548 loc) · 22.4 KB
/
exponentiation_operator.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
//! ES2016: Exponentiation Operator
//!
//! This plugin transforms the exponentiation operator (`**`) to `Math.pow`.
//!
//! > This plugin is included in `preset-env`, in ES2016
//!
//! ## Example
//!
//! Input:
//! ```js
//! let x = 10 ** 2;
//! x **= 3;
//! obj.prop **= 4;
//! ```
//!
//! Output:
//! ```js
//! let x = Math.pow(10, 2);
//! x = Math.pow(x, 3);
//! obj["prop"] = Math.pow(obj["prop"], 4);
//! ```
//!
//! ## Implementation
//!
//! Implementation based on [@babel/plugin-transform-exponentiation-operator](https://babel.dev/docs/babel-plugin-transform-exponentiation-operator).
//!
//! ## References:
//!
//! * Babel plugin implementation:
//! <https://github.com/babel/babel/blob/main/packages/babel-plugin-transform-exponentiation-operator>
//! <https://github.com/babel/babel/tree/main/packages/babel-helper-builder-binary-assignment-operator-visitor>
//! * Exponentiation operator TC39 proposal: <https://github.com/tc39/proposal-exponentiation-operator>
//! * Exponentiation operator specification: <https://tc39.es/ecma262/#sec-exp-operator>
use oxc_allocator::{CloneIn, Vec};
use oxc_ast::{ast::*, NONE};
use oxc_semantic::{ReferenceFlags, SymbolFlags};
use oxc_span::SPAN;
use oxc_syntax::operator::{AssignmentOperator, BinaryOperator};
use oxc_traverse::{Ancestor, BoundIdentifier, Traverse, TraverseCtx};
use crate::TransformCtx;
pub struct ExponentiationOperator<'a, 'ctx> {
ctx: &'ctx TransformCtx<'a>,
}
impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
pub fn new(ctx: &'ctx TransformCtx<'a>) -> Self {
Self { ctx }
}
}
impl<'a, 'ctx> Traverse<'a> for ExponentiationOperator<'a, 'ctx> {
// Note: Do not transform to `Math.pow` with BigInt arguments - that's a runtime error
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
match expr {
// `left ** right`
Expression::BinaryExpression(binary_expr) => {
if binary_expr.operator != BinaryOperator::Exponential
|| binary_expr.left.is_big_int_literal()
|| binary_expr.right.is_big_int_literal()
{
return;
}
Self::convert_binary_expression(expr, ctx);
}
// `left **= right`
Expression::AssignmentExpression(assign_expr) => {
if assign_expr.operator != AssignmentOperator::Exponential
|| assign_expr.right.is_big_int_literal()
{
return;
}
match &assign_expr.left {
AssignmentTarget::AssignmentTargetIdentifier(_) => {
self.convert_identifier_assignment(expr, ctx);
}
AssignmentTarget::StaticMemberExpression(_) => {
self.convert_static_member_expression_assignment(expr, ctx);
}
AssignmentTarget::ComputedMemberExpression(_) => {
self.convert_computed_member_expression_assignment(expr, ctx);
}
// Babel refuses to transform this: "We can't generate property ref for private name,
// please install `@babel/plugin-transform-class-properties`".
// But there's no reason not to.
AssignmentTarget::PrivateFieldExpression(_) => {
self.convert_private_field_assignment(expr, ctx);
}
_ => {}
}
}
_ => {}
}
}
}
impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
/// Convert `BinaryExpression`.
///
/// `left ** right` -> `Math.pow(left, right)`
//
// `#[inline]` so compiler knows `expr` is a `BinaryExpression`
#[inline]
fn convert_binary_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let binary_expr = match ctx.ast.move_expression(expr) {
Expression::BinaryExpression(binary_expr) => binary_expr.unbox(),
_ => unreachable!(),
};
*expr = Self::math_pow(binary_expr.left, binary_expr.right, ctx);
}
/// Convert `AssignmentExpression` where assignee is an identifier.
///
/// `left **= right` transformed to:
/// * If `left` is a bound symbol:
/// -> `left = Math.pow(left, right)`
/// * If `left` is unbound:
/// -> `var _left; _left = left, left = Math.pow(_left, right)`
///
/// Temporary variable `_left` is to avoid side-effects of getting `left` from running twice.
//
// `#[inline]` so compiler knows `expr` is an `AssignmentExpression` with `IdentifierReference` on left
#[inline]
fn convert_identifier_assignment(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::AssignmentExpression(assign_expr) = expr else { unreachable!() };
let AssignmentTarget::AssignmentTargetIdentifier(ident) = &mut assign_expr.left else {
unreachable!()
};
let (pow_left, temp_var_inits) = self.get_pow_left_identifier(ident, ctx);
Self::convert_assignment(assign_expr, pow_left, ctx);
Self::revise_expression(expr, temp_var_inits, ctx);
}
/// Get left side of `Math.pow(pow_left, ...)` for identifier
fn get_pow_left_identifier(
&mut self,
ident: &mut IdentifierReference<'a>,
ctx: &mut TraverseCtx<'a>,
) -> (
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
) {
let mut temp_var_inits = ctx.ast.vec();
// Make sure side-effects of evaluating `left` only happen once
let reference = ctx.scoping.symbols_mut().get_reference_mut(ident.reference_id().unwrap());
let pow_left = if let Some(symbol_id) = reference.symbol_id() {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
// `left **= right` is being transformed to `left = Math.pow(left, right)`,
// so if `left` is no longer being read from, update its `ReferenceFlags`.
if matches!(ctx.ancestry.parent(), Ancestor::ExpressionStatementExpression(_)) {
*reference.flags_mut() = ReferenceFlags::Write;
}
ctx.ast.expression_from_identifier_reference(ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
))
} else {
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
let reference = ctx.ast.expression_from_identifier_reference(
ctx.create_unbound_reference_id(SPAN, ident.name.clone(), ReferenceFlags::Read),
);
let binding = self.create_temp_var(reference, &mut temp_var_inits, ctx);
binding.create_read_expression(ctx)
};
(pow_left, temp_var_inits)
}
/// Convert `AssignmentExpression` where assignee is a static member expression.
///
/// `obj.prop **= right` transformed to:
/// * If `obj` is a bound symbol:
/// -> `obj["prop"] = Math.pow(obj["prop"], right)`
/// * If `obj` is unbound:
/// -> `var _obj; _obj = obj, _obj["prop"] = Math.pow(_obj["prop"], right)`
///
/// `obj.foo.bar.qux **= right` transformed to:
/// ```js
/// var _obj$foo$bar;
/// _obj$foo$bar = obj.foo.bar, _obj$foo$bar["qux"] = Math.pow(_obj$foo$bar["qux"], right)
/// ```
///
/// Temporary variables are to avoid side-effects of getting `obj` / `obj.foo.bar` being run twice.
///
/// TODO(improve-on-babel): `obj.prop` does not need to be transformed to `obj["prop"]`.
//
// `#[inline]` so compiler knows `expr` is an `AssignmentExpression` with `StaticMemberExpression` on left
#[inline]
fn convert_static_member_expression_assignment(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::AssignmentExpression(assign_expr) = expr else { unreachable!() };
let AssignmentTarget::StaticMemberExpression(member_expr) = &mut assign_expr.left else {
unreachable!()
};
let (replacement_left, pow_left, temp_var_inits) =
self.get_pow_left_static_member(member_expr, ctx);
assign_expr.left = replacement_left;
Self::convert_assignment(assign_expr, pow_left, ctx);
Self::revise_expression(expr, temp_var_inits, ctx);
}
/// Get left side of `Math.pow(pow_left, ...)` for static member expression
/// and replacement for left side of assignment.
fn get_pow_left_static_member(
&mut self,
member_expr: &mut StaticMemberExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> (
// Replacement left of assignment
AssignmentTarget<'a>,
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
) {
// Object part of 2nd member expression
// ```
// obj["prop"] = Math.pow(obj["prop"], right)
// ^^^
// ```
let mut temp_var_inits = ctx.ast.vec();
let obj = self.get_second_member_expression_object(
&mut member_expr.object,
&mut temp_var_inits,
ctx,
);
// Property part of 2nd member expression
// ```
// obj["prop"] = Math.pow(obj["prop"], right)
// ^^^^^^
// ```
let prop_span = member_expr.property.span;
let prop_name = member_expr.property.name.clone();
let prop = ctx.ast.expression_string_literal(prop_span, prop_name.clone());
// Complete 2nd member expression
// ```
// obj["prop"] = Math.pow(obj["prop"], right)
// ^^^^^^^^^^^
// ```
let pow_left = Expression::from(ctx.ast.member_expression_computed(SPAN, obj, prop, false));
// Replacement for original member expression
// ```
// obj["prop"] = Math.pow(obj["prop"], right)
// ^^^^^^^^^^^
// ```
let replacement_left =
AssignmentTarget::ComputedMemberExpression(ctx.ast.alloc_computed_member_expression(
member_expr.span,
ctx.ast.move_expression(&mut member_expr.object),
ctx.ast.expression_string_literal(prop_span, prop_name),
false,
));
(replacement_left, pow_left, temp_var_inits)
}
/// Convert `AssignmentExpression` where assignee is a computed member expression.
///
/// `obj[prop] **= right` transformed to:
/// * If `obj` is a bound symbol:
/// -> `var _prop; _prop = prop, obj[_prop] = Math.pow(obj[_prop], 2)`
/// * If `obj` is unbound:
/// -> `var _obj, _prop; _obj = obj, _prop = prop, _obj[_prop] = Math.pow(_obj[_prop], 2)`
///
/// `obj.foo.bar[qux] **= right` transformed to:
/// ```js
/// var _obj$foo$bar, _qux;
/// _obj$foo$bar = obj.foo.bar, _qux = qux, _obj$foo$bar[_qux] = Math.pow(_obj$foo$bar[_qux], right)
/// ```
///
/// Temporary variables are to avoid side-effects of getting `obj` / `obj.foo.bar` or `prop` being run twice.
///
/// TODO(improve-on-babel):
/// 1. If `prop` is bound, it doesn't need a temp variable `_prop`.
/// 2. Temp var initializations could be inlined:
/// * Current: `(_obj = obj, _prop = prop, _obj[_prop] = Math.pow(_obj[_prop], 2))`
/// * Could be: `(_obj = obj)[_prop = prop] = Math.pow(_obj[_prop], 2)`
//
// `#[inline]` so compiler knows `expr` is an `AssignmentExpression` with `ComputedMemberExpression` on left
#[inline]
fn convert_computed_member_expression_assignment(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::AssignmentExpression(assign_expr) = expr else { unreachable!() };
let AssignmentTarget::ComputedMemberExpression(member_expr) = &mut assign_expr.left else {
unreachable!()
};
let (pow_left, temp_var_inits) = self.get_pow_left_computed_member(member_expr, ctx);
Self::convert_assignment(assign_expr, pow_left, ctx);
Self::revise_expression(expr, temp_var_inits, ctx);
}
/// Get left side of `Math.pow(pow_left, ...)` for computed member expression
fn get_pow_left_computed_member(
&mut self,
member_expr: &mut ComputedMemberExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> (
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
) {
// Object part of 2nd member expression
// ```
// obj[_prop] = Math.pow(obj[_prop], right)
// ^^^
// ```
let mut temp_var_inits = ctx.ast.vec();
let obj = self.get_second_member_expression_object(
&mut member_expr.object,
&mut temp_var_inits,
ctx,
);
// Property part of 2nd member expression
// ```
// obj[_prop] = Math.pow(obj[_prop], right)
// ^^^^^ replaced ^^^^^ prop
// ```
let prop = &mut member_expr.expression;
let prop = if prop.is_literal() {
prop.clone_in(ctx.ast.allocator)
} else {
let owned_prop = ctx.ast.move_expression(prop);
let binding = self.create_temp_var(owned_prop, &mut temp_var_inits, ctx);
*prop = binding.create_read_expression(ctx);
binding.create_read_expression(ctx)
};
// Complete 2nd member expression
// ```
// obj[_prop] = Math.pow(obj[_prop], right)
// ^^^^^^^^^^
// ```
let pow_left = Expression::from(ctx.ast.member_expression_computed(SPAN, obj, prop, false));
(pow_left, temp_var_inits)
}
/// Convert `AssignmentExpression` where assignee is a private field member expression.
///
/// `obj.#prop **= right` transformed to:
/// * If `obj` is a bound symbol:
/// -> `obj.#prop = Math.pow(obj.#prop, right)`
/// * If `obj` is unbound:
/// -> `var _obj; _obj = obj, _obj.#prop = Math.pow(_obj.#prop, right)`
///
/// `obj.foo.bar.#qux **= right` transformed to:
/// ```js
/// var _obj$foo$bar;
/// _obj$foo$bar = obj.foo.bar, _obj$foo$bar.#qux = Math.pow(_obj$foo$bar.#qux, right)
/// ```
///
/// Temporary variable is to avoid side-effects of getting `obj` / `obj.foo.bar` being run twice.
//
// `#[inline]` so compiler knows `expr` is an `AssignmentExpression` with `PrivateFieldExpression` on left
#[inline]
fn convert_private_field_assignment(
&mut self,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let Expression::AssignmentExpression(assign_expr) = expr else { unreachable!() };
let AssignmentTarget::PrivateFieldExpression(member_expr) = &mut assign_expr.left else {
unreachable!()
};
let (pow_left, temp_var_inits) = self.get_pow_left_private_field(member_expr, ctx);
Self::convert_assignment(assign_expr, pow_left, ctx);
Self::revise_expression(expr, temp_var_inits, ctx);
}
/// Get left side of `Math.pow(pow_left, ...)` for static member expression
/// and replacement for left side of assignment.
fn get_pow_left_private_field(
&mut self,
field_expr: &mut PrivateFieldExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> (
// Left side of `Math.pow(pow_left, ...)`
Expression<'a>,
// Temporary var initializations
Vec<'a, Expression<'a>>,
) {
// Object part of 2nd member expression
// ```
// obj.#prop = Math.pow(obj.#prop, right)
// ^^^
// ```
let mut temp_var_inits = ctx.ast.vec();
let obj = self.get_second_member_expression_object(
&mut field_expr.object,
&mut temp_var_inits,
ctx,
);
// Property part of 2nd member expression
// ```
// obj.#prop = Math.pow(obj.#prop, right)
// ^^^^^
// ```
let field = field_expr.field.clone_in(ctx.ast.allocator);
// Complete 2nd member expression
// ```
// obj.#prop = Math.pow(obj.#prop, right)
// ^^^^^^^^^
// ```
let pow_left = Expression::from(
ctx.ast.member_expression_private_field_expression(SPAN, obj, field, false),
);
(pow_left, temp_var_inits)
}
/// Get object part of 2nd member expression to be used as `left` in `Math.pow(left, right)`.
///
/// Also update the original `obj` passed in to function, and add a temp var initializer, if necessary.
///
/// Original:
/// ```js
/// obj.prop **= 2`
/// ^^^ original `obj` passed in to this function
/// ```
///
/// is transformed to:
///
/// If `obj` is a bound symbol:
/// ```js
/// obj["prop"] = Math.pow(obj["prop"], 2)
/// ^^^ not updated ^^^ returned
/// ```
///
/// If `obj` is unbound:
/// ```js
/// var _obj;
/// _obj = obj, _obj["prop"] = Math.pow(_obj["prop"], 2)
/// ^^^^ updated ^^^^ returned
/// ^^^^^^^^^^ added to `temp_var_inits`
/// ```
///
/// Original:
/// ```js
/// obj.foo.bar.qux **= 2
/// ^^^^^^^^^^^ original `obj` passed in to this function
/// ```
/// is transformed to:
/// ```js
/// var _obj$foo$bar;
/// _obj$foo$bar = obj.foo.bar, _obj$foo$bar["qux"] = Math.pow(_obj$foo$bar["qux"], 2)
/// ^^^^^^^^^^^^ updated ^^^^^^^^^^^^ returned
/// ^^^^^^^^^^^^^^^^^^^^^^^^^^ added to `temp_var_inits`
/// ```
fn get_second_member_expression_object(
&mut self,
obj: &mut Expression<'a>,
temp_var_inits: &mut Vec<'a, Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
// If the object reference that we need to save is locally declared, evaluating it multiple times
// will not trigger getters or setters. `super` cannot be directly assigned, so use it directly too.
// TODO(improve-on-babel): We could also skip creating a temp var for `this.x **= 2`.
match obj {
Expression::Super(super_) => return ctx.ast.expression_super(super_.span),
Expression::Identifier(ident) => {
let symbol_id =
ctx.symbols().get_reference(ident.reference_id().unwrap()).symbol_id();
if let Some(symbol_id) = symbol_id {
// This variable is declared in scope so evaluating it multiple times can't trigger a getter.
// No need for a temp var.
return ctx.ast.expression_from_identifier_reference(
ctx.create_bound_reference_id(
SPAN,
ident.name.clone(),
symbol_id,
ReferenceFlags::Read,
),
);
}
// Unbound reference. Could possibly trigger a getter so we need to only evaluate it once.
// Assign to a temp var.
}
_ => {
// Other expression. Assign to a temp var.
}
}
let binding = self.create_temp_var(ctx.ast.move_expression(obj), temp_var_inits, ctx);
*obj = binding.create_read_expression(ctx);
binding.create_read_expression(ctx)
}
/// `x **= right` -> `x = Math.pow(pow_left, right)` (with provided `pow_left`)
fn convert_assignment(
assign_expr: &mut AssignmentExpression<'a>,
pow_left: Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) {
let pow_right = ctx.ast.move_expression(&mut assign_expr.right);
assign_expr.right = Self::math_pow(pow_left, pow_right, ctx);
assign_expr.operator = AssignmentOperator::Assign;
}
/// If needs temp var initializers, replace expression `expr` with `(temp1, temp2, expr)`.
fn revise_expression(
expr: &mut Expression<'a>,
mut temp_var_inits: Vec<'a, Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) {
if !temp_var_inits.is_empty() {
temp_var_inits.reserve_exact(1);
temp_var_inits.push(ctx.ast.move_expression(expr));
*expr = ctx.ast.expression_sequence(SPAN, temp_var_inits);
}
}
/// `Math.pow(left, right)`
fn math_pow(
left: Expression<'a>,
right: Expression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
let math_symbol_id = ctx.scopes().find_binding(ctx.current_scope_id(), "Math");
let ident_math =
ctx.create_reference_id(SPAN, Atom::from("Math"), math_symbol_id, ReferenceFlags::Read);
let object = ctx.ast.expression_from_identifier_reference(ident_math);
let property = ctx.ast.identifier_name(SPAN, "pow");
let callee =
Expression::from(ctx.ast.member_expression_static(SPAN, object, property, false));
let arguments = ctx.ast.vec_from_iter([Argument::from(left), Argument::from(right)]);
ctx.ast.expression_call(SPAN, callee, NONE, arguments, false)
}
/// Create a temporary variable.
/// Add a `var _name;` statement to enclosing scope.
/// Add initialization expression `_name = expr` to `temp_var_inits`.
/// Return `BoundIdentifier` for the temp var.
fn create_temp_var(
&mut self,
expr: Expression<'a>,
temp_var_inits: &mut Vec<'a, Expression<'a>>,
ctx: &mut TraverseCtx<'a>,
) -> BoundIdentifier<'a> {
let binding = ctx.generate_uid_in_current_scope_based_on_node(
&expr,
SymbolFlags::FunctionScopedVariable,
);
// var _name;
self.ctx.var_declarations.insert(&binding, None, ctx);
// Add new reference `_name = name` to `temp_var_inits`
temp_var_inits.push(ctx.ast.expression_assignment(
SPAN,
AssignmentOperator::Assign,
binding.create_read_write_target(ctx),
expr,
));
binding
}
}