Skip to content

Commit

Permalink
fix(minifier): class C { ['-1']() {} } cannot be minifized (#8516)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 15, 2025
1 parent 7e61b23 commit 209e313
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 30 deletions.
35 changes: 17 additions & 18 deletions crates/oxc_minifier/src/ast_passes/convert_to_dotted_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,23 @@ impl<'a> ConvertToDottedProperties {
expr: &mut MemberExpression<'a>,
ctx: Ctx<'a, '_>,
) {
if let MemberExpression::ComputedMemberExpression(e) = expr {
let Expression::StringLiteral(s) = &e.expression else { return };
if is_identifier_name(&s.value) {
let property = ctx.ast.identifier_name(s.span, s.value.clone());
let object = ctx.ast.move_expression(&mut e.object);
*expr = MemberExpression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(e.span, object, property, e.optional),
);
self.changed = true;
return;
}
let v = s.value.as_str();
if !e.optional {
if let Some(n) = Ctx::string_to_equivalent_number_value(v) {
e.expression =
ctx.ast.expression_numeric_literal(s.span, n, None, NumberBase::Decimal);
}
}
let MemberExpression::ComputedMemberExpression(e) = expr else { return };
let Expression::StringLiteral(s) = &e.expression else { return };
if is_identifier_name(&s.value) {
let property = ctx.ast.identifier_name(s.span, s.value.clone());
let object = ctx.ast.move_expression(&mut e.object);
*expr = MemberExpression::StaticMemberExpression(
ctx.ast.alloc_static_member_expression(e.span, object, property, e.optional),
);
self.changed = true;
return;
}
let v = s.value.as_str();
if e.optional {
return;
}
if let Some(n) = Ctx::string_to_equivalent_number_value(v) {
e.expression = ctx.ast.expression_numeric_literal(s.span, n, None, NumberBase::Decimal);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1131,7 +1131,7 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
t.to_js_string().map(|val| ctx.ast.expression_string_literal(t.span(), val, None))
}

// https://github.com/swc-project/swc/blob/4e2dae558f60a9f5c6d2eac860743e6c0b2ec562/crates/swc_ecma_minifier/src/compress/pure/properties.rs
// <https://github.com/swc-project/swc/blob/4e2dae558f60a9f5c6d2eac860743e6c0b2ec562/crates/swc_ecma_minifier/src/compress/pure/properties.rs>
#[allow(clippy::cast_lossless)]
fn try_compress_property_key(
&mut self,
Expand All @@ -1152,22 +1152,29 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
if matches!(value, "__proto__" | "prototype" | "constructor" | "#constructor") {
return;
}
if *computed {
*computed = false;
}
if is_identifier_name(value) {
self.changed = true;
*computed = false;
*key = PropertyKey::StaticIdentifier(
ctx.ast.alloc_identifier_name(s.span, s.value.clone()),
);
} else if let Some(value) = Ctx::string_to_equivalent_number_value(value) {
self.changed = true;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span,
value,
None,
NumberBase::Decimal,
));
return;
}
if let Some(value) = Ctx::string_to_equivalent_number_value(value) {
if value >= 0.0 {
*computed = false;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span,
value,
None,
NumberBase::Decimal,
));
self.changed = true;
}
return;
}
if *computed {
*computed = false;
}
}

Expand Down Expand Up @@ -1933,6 +1940,7 @@ mod test {
"class F { accessor 0 = _; accessor a = _; accessor 1 = _;accessor 1 = _; accessor b = _; accessor 'c.c' = _; accessor '1.1' = _; accessor '😊' = _; accessor 'd.d' = _ }"
);

test_same("class C { ['-1']() {} }");
test_same("class C { ['prototype']() {} }");
test_same("class C { ['__proto__']() {} }");
test_same("class C { ['constructor']() {} }");
Expand Down

0 comments on commit 209e313

Please sign in to comment.