fix(minifier): computed property key prototype cannot be changed (#8373)

This commit is contained in:
Boshen 2025-01-09 04:24:03 +00:00
parent 82ee77ef19
commit cb098c7d3c

View file

@ -1026,18 +1026,19 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
return; return;
}; };
let PropertyKey::StringLiteral(s) = key else { return }; let PropertyKey::StringLiteral(s) = key else { return };
if s.value == "__proto__" || s.value == "constructor" { let value = s.value.as_str();
if matches!(value, "__proto__" | "constructor" | "prototype") {
return; return;
} }
if *computed { if *computed {
*computed = false; *computed = false;
} }
if is_identifier_name(&s.value) { if is_identifier_name(value) {
self.changed = true; self.changed = true;
*key = PropertyKey::StaticIdentifier( *key = PropertyKey::StaticIdentifier(
ctx.ast.alloc_identifier_name(s.span, s.value.clone()), ctx.ast.alloc_identifier_name(s.span, s.value.clone()),
); );
} else if let Some(value) = Ctx::string_to_equivalent_number_value(s.value.as_str()) { } else if let Some(value) = Ctx::string_to_equivalent_number_value(value) {
self.changed = true; self.changed = true;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal( *key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span, s.span,
@ -1775,6 +1776,10 @@ mod test {
"class F { accessor '0' = _; accessor 'a' = _; accessor [1] = _; accessor ['1'] = _; accessor ['b'] = _; accessor ['c.c'] = _; accessor '1.1' = _; accessor '😊' = _; accessor 'd.d' = _ }", "class F { accessor '0' = _; accessor 'a' = _; accessor [1] = _; accessor ['1'] = _; accessor ['b'] = _; accessor ['c.c'] = _; accessor '1.1' = _; accessor '😊' = _; accessor 'd.d' = _ }",
"class F { accessor 0 = _; accessor a = _; accessor 1 = _;accessor 1 = _; accessor b = _; accessor 'c.c' = _; accessor '1.1' = _; accessor '😊' = _; accessor 'd.d' = _ }" "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 { ['prototype']() {} }");
test_same("class C { ['__proto__']() {} }");
test_same("class C { ['constructor']() {} }");
} }
#[test] #[test]