-
-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into fix-define-span
- Loading branch information
Showing
6 changed files
with
110 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
crates/oxc_transformer/src/es2022/class_properties/supers.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
//! ES2022: Class Properties | ||
//! Transform of `super` expressions. | ||
use oxc_ast::ast::*; | ||
use oxc_traverse::TraverseCtx; | ||
|
||
use crate::Helper; | ||
|
||
use super::ClassProperties; | ||
|
||
impl<'a, 'ctx> ClassProperties<'a, 'ctx> { | ||
/// Transform static member expression where object is `super`. | ||
/// | ||
/// `super.prop` -> `_superPropGet(_Class, "prop", _Class)` | ||
// | ||
// `#[inline]` so that compiler sees that `expr` is an `Expression::StaticMemberExpression`. | ||
#[inline] | ||
pub(super) fn transform_static_member_expression( | ||
&mut self, | ||
expr: &mut Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
let Expression::StaticMemberExpression(member) = expr else { unreachable!() }; | ||
if matches!(member.object, Expression::Super(_)) { | ||
*expr = self.transform_static_member_expression_impl(member, ctx); | ||
} | ||
} | ||
|
||
fn transform_static_member_expression_impl( | ||
&mut self, | ||
member: &mut StaticMemberExpression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) -> Expression<'a> { | ||
let property = &member.property; | ||
let property = | ||
ctx.ast.expression_string_literal(property.span, property.name.clone(), None); | ||
self.create_super_prop_get(member.span, property, ctx) | ||
} | ||
|
||
/// Transform computed member expression where object is `super`. | ||
/// | ||
/// `super[prop]` -> `_superPropGet(_Class, prop, _Class)` | ||
// | ||
// `#[inline]` so that compiler sees that `expr` is an `Expression::ComputedMemberExpression`. | ||
#[inline] | ||
pub(super) fn transform_computed_member_expression( | ||
&mut self, | ||
expr: &mut Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
let Expression::ComputedMemberExpression(member) = expr else { unreachable!() }; | ||
if matches!(member.object, Expression::Super(_)) { | ||
*expr = self.transform_computed_member_expression_impl(member, ctx); | ||
} | ||
} | ||
|
||
fn transform_computed_member_expression_impl( | ||
&mut self, | ||
member: &mut ComputedMemberExpression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) -> Expression<'a> { | ||
let property = ctx.ast.move_expression(&mut member.expression); | ||
self.create_super_prop_get(member.span, property, ctx) | ||
} | ||
|
||
/// `_superPropGet(_Class, prop, _Class)` | ||
fn create_super_prop_get( | ||
&mut self, | ||
span: Span, | ||
property: Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) -> Expression<'a> { | ||
let class_binding = self.get_temp_binding(ctx); | ||
// `(_Class, prop, _Class)` | ||
let arguments = ctx.ast.vec_from_array([ | ||
Argument::from(class_binding.create_read_expression(ctx)), | ||
Argument::from(property), | ||
Argument::from(class_binding.create_read_expression(ctx)), | ||
]); | ||
// `_superPropGet(_Class, prop, _Class)` | ||
self.ctx.helper_call_expr(Helper::SuperPropGet, span, arguments, ctx) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters