Skip to content

Commit

Permalink
Fix: Static props name and length require defineProperty
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Nov 20, 2024
1 parent 54419a7 commit 658ee38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
17 changes: 14 additions & 3 deletions crates/oxc_transformer/src/es2022/class_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
} else {
// Convert to assignment or `_defineProperty` call, depending on `loose` option
let this = ctx.ast.expression_this(SPAN);
self.create_init_assignment(prop, value, this, ctx)
self.create_init_assignment(prop, value, this, false, ctx)
};
instance_inits.push(init_expr);
}
Expand All @@ -675,7 +675,7 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
unreachable!();
};
let assignee = class_name_binding.create_read_expression(ctx);
let init_expr = self.create_init_assignment(prop, value, assignee, ctx);
let init_expr = self.create_init_assignment(prop, value, assignee, true, ctx);
self.insert_expr_after_class(init_expr, ctx);
}
}
Expand Down Expand Up @@ -705,11 +705,12 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
prop: &mut PropertyDefinition<'a>,
value: Expression<'a>,
assignee: Expression<'a>,
is_static: bool,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
if self.set_public_class_fields {
// `assignee.foo = value`
self.create_init_assignment_loose(prop, value, assignee, ctx)
self.create_init_assignment_loose(prop, value, assignee, is_static, ctx)
} else {
// `_defineProperty(assignee, "foo", value)`
self.create_init_assignment_not_loose(prop, value, assignee, ctx)
Expand All @@ -722,12 +723,22 @@ impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
prop: &mut PropertyDefinition<'a>,
value: Expression<'a>,
assignee: Expression<'a>,
is_static: bool,
ctx: &mut TraverseCtx<'a>,
) -> Expression<'a> {
// In-built static props `name` and `length` need to be set with `_defineProperty`
let needs_define = |name| is_static && (name == "name" || name == "length");

let left = match &mut prop.key {
PropertyKey::StaticIdentifier(ident) => {
if needs_define(&ident.name) {
return self.create_init_assignment_not_loose(prop, value, assignee, ctx);
}
ctx.ast.member_expression_static(SPAN, assignee, ident.as_ref().clone(), false)
}
PropertyKey::StringLiteral(str_lit) if needs_define(&str_lit.value) => {
return self.create_init_assignment_not_loose(prop, value, assignee, ctx);
}
key @ match_expression!(PropertyKey) => {
// TODO: This can also be a numeric key (non-computed). Maybe other key types?
let key = self.create_computed_key_temp_var(key.to_expression_mut(), ctx);
Expand Down
7 changes: 2 additions & 5 deletions tasks/transform_conformance/snapshots/babel.snap.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
commit: d20b314c

Passed: 400/822
Passed: 401/822

# All Passed:
* babel-plugin-transform-class-static-block
Expand Down Expand Up @@ -273,7 +273,7 @@ x Output mismatch
x Output mismatch


# babel-plugin-transform-class-properties (73/248)
# babel-plugin-transform-class-properties (74/248)
* assumption-constantSuper/complex-super-class/input.js
x Output mismatch

Expand All @@ -298,9 +298,6 @@ x Output mismatch
* assumption-setPublicClassFields/foobar/input.js
x Output mismatch

* assumption-setPublicClassFields/length-name-use-define/input.js
x Output mismatch

* assumption-setPublicClassFields/regression-T7364/input.mjs
Scope children mismatch:
after transform: ScopeId(1): [ScopeId(2), ScopeId(7)]
Expand Down

0 comments on commit 658ee38

Please sign in to comment.