-
-
Notifications
You must be signed in to change notification settings - Fork 484
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(transformer/class-properties): transform super assignment expressions that are inside static prop initializer #7959
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,9 +34,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { | |
is_callee: bool, | ||
ctx: &mut TraverseCtx<'a>, | ||
) -> Expression<'a> { | ||
let property = &member.property; | ||
let property = | ||
ctx.ast.expression_string_literal(property.span, property.name.clone(), None); | ||
let property = Expression::StringLiteral(ctx.ast.alloc(member.property.clone().into())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is shorter, but I'm not really a fan of Reason is: You're dealing with an owned type on the stack. Usually, it's better to allocate nodes into the arena as quickly as possible, and then only hold smaller types like:
Generally, if you find yourself having to use In the original version, you only had to clone the In this case, it's not a big deal, because If you wanted to, you could implement this conversion instead as a method on |
||
self.create_super_prop_get(member.span, property, is_callee, ctx) | ||
} | ||
|
||
|
@@ -125,6 +123,71 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { | |
arguments.push(Argument::from(array)); | ||
} | ||
|
||
/// Transform assignment expression where left-hand side contains `super`. | ||
/// | ||
/// * `object.#prop = value` | ||
/// -> `_superPropSet(_Class, prop, value, _Class)` | ||
/// * `object.#prop += value` | ||
/// -> `_superPropSet(_Class, prop, _superPropGet(_Class, prop, _Class) + value, _Class)` | ||
/// * `object.#prop &&= value` | ||
/// -> `_superPropGet(_Class, prop, _Class) && _superPropSet(_Class, prop, value, _Class)` | ||
// | ||
// `#[inline]` so that compiler sees that `expr` is an `Expression::AssignmentExpression` | ||
pub(super) fn transform_super_assignment_expression( | ||
&mut self, | ||
expr: &mut Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
let Expression::AssignmentExpression(assign_expr) = expr else { unreachable!() }; | ||
match &assign_expr.left { | ||
AssignmentTarget::StaticMemberExpression(member) if member.object.is_super() => { | ||
self.transform_assignment_expression_for_super_static_member_expr(expr, ctx); | ||
} | ||
AssignmentTarget::ComputedMemberExpression(member) if member.object.is_super() => { | ||
self.transform_assignment_expression_for_super_computed_member_expr(expr, ctx); | ||
} | ||
_ => {} | ||
}; | ||
} | ||
|
||
fn transform_assignment_expression_for_super_static_member_expr( | ||
&mut self, | ||
expr: &mut Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
let getter = | ||
|s: &mut Self, object: Expression<'a>, span: Span, ctx: &mut TraverseCtx<'a>| { | ||
s.create_super_prop_get(span, object, false, ctx) | ||
}; | ||
let setter = |s: &mut Self, | ||
object: Expression<'a>, | ||
value: Expression<'a>, | ||
span: Span, | ||
ctx: &mut TraverseCtx<'a>| { | ||
s.create_super_prop_set(span, object, value, ctx) | ||
}; | ||
self.transform_instance_assignment_expression(expr, getter, setter, ctx); | ||
} | ||
|
||
fn transform_assignment_expression_for_super_computed_member_expr( | ||
&mut self, | ||
expr: &mut Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) { | ||
let getter = | ||
|s: &mut Self, object: Expression<'a>, span: Span, ctx: &mut TraverseCtx<'a>| { | ||
s.create_super_prop_get(span, object, false, ctx) | ||
}; | ||
let setter = |s: &mut Self, | ||
object: Expression<'a>, | ||
value: Expression<'a>, | ||
span: Span, | ||
ctx: &mut TraverseCtx<'a>| { | ||
s.create_super_prop_set(span, object, value, ctx) | ||
}; | ||
self.transform_instance_assignment_expression(expr, getter, setter, ctx); | ||
} | ||
|
||
/// Member: | ||
/// `_superPropGet(_Class, prop, _Class)` | ||
/// | ||
|
@@ -155,4 +218,28 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> { | |
// `_superPropGet(_Class, prop, _Class)` or `_superPropGet(_Class, prop, _Class, 2)` | ||
self.ctx.helper_call_expr(Helper::SuperPropGet, span, arguments, ctx) | ||
} | ||
|
||
/// `_superPropSet(_Class, prop, value, _Class)` | ||
fn create_super_prop_set( | ||
&mut self, | ||
span: Span, | ||
property: Expression<'a>, | ||
value: Expression<'a>, | ||
ctx: &mut TraverseCtx<'a>, | ||
) -> Expression<'a> { | ||
let temp_binding = self.current_class_mut().bindings.get_or_init_temp_binding(ctx); | ||
let arguments = ctx.ast.vec_from_array([ | ||
Argument::from(temp_binding.create_read_expression(ctx)), | ||
Argument::from(property), | ||
Argument::from(value), | ||
Argument::from(temp_binding.create_read_expression(ctx)), | ||
Argument::from(ctx.ast.expression_numeric_literal( | ||
SPAN, | ||
1.0, | ||
None, | ||
NumberBase::Decimal, | ||
)), | ||
]); | ||
self.ctx.helper_call_expr(Helper::SuperPropSet, span, arguments, ctx) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const ident = "A"; | ||
class Outer { | ||
static A = 0; | ||
static B = () => { | ||
super.A += 1; | ||
super.A -= 1; | ||
super.A &&= 1; | ||
super.A ||= 1; | ||
super.A = 1; | ||
|
||
super[ident] += 1; | ||
super[ident] -= 1; | ||
super[ident] &&= 1; | ||
super[ident] ||= 1; | ||
super[ident] = 1; | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var _Outer; | ||
const ident = "A"; | ||
class Outer {} | ||
_Outer = Outer; | ||
babelHelpers.defineProperty(Outer, "A", 0); | ||
babelHelpers.defineProperty(Outer, "B", () => { | ||
babelHelpers.superPropSet(_Outer, "A", babelHelpers.superPropGet(_Outer, "A", _Outer) + 1, _Outer, 1); | ||
babelHelpers.superPropSet(_Outer, "A", babelHelpers.superPropGet(_Outer, "A", _Outer) - 1, _Outer, 1); | ||
babelHelpers.superPropGet(_Outer, "A", _Outer) && | ||
babelHelpers.superPropSet(_Outer, "A", 1, _Outer, 1); | ||
babelHelpers.superPropGet(_Outer, "A", _Outer) || | ||
babelHelpers.superPropSet(_Outer, "A", 1, _Outer, 1); | ||
babelHelpers.superPropSet(_Outer, "A", 1, _Outer, 1); | ||
babelHelpers.superPropSet( | ||
_Outer, | ||
ident, | ||
babelHelpers.superPropGet(_Outer, ident, _Outer) + 1, | ||
_Outer, | ||
1, | ||
); | ||
babelHelpers.superPropSet( | ||
_Outer, | ||
ident, | ||
babelHelpers.superPropGet(_Outer, ident, _Outer) - 1, | ||
_Outer, | ||
1, | ||
); | ||
babelHelpers.superPropGet(_Outer, ident, _Outer) && | ||
babelHelpers.superPropSet(_Outer, ident, 1, _Outer, 1); | ||
babelHelpers.superPropGet(_Outer, ident, _Outer) || | ||
babelHelpers.superPropSet(_Outer, ident, 1, _Outer, 1); | ||
babelHelpers.superPropSet(_Outer, ident, 1, _Outer, 1); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is the cause of the panic in conformance. Problem is that
transform_assignment_expression
sometimes mutatesexpr
so it's not anAssignmentExpression
any more. So thentransform_super_assignment_expression
entersunreachable!()
.So we need to check that it's still an
AssignmentExpression
:Having to do this check again is inefficient, but don't worry - this visitor will be removed anyway in my forthcoming refactoring, so we can change it then.