Skip to content

Commit

Permalink
fix(minifier): computed property key prototype cannot be changed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 9, 2025
1 parent 82ee77e commit cb098c7
Showing 1 changed file with 8 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1026,18 +1026,19 @@ impl<'a, 'b> PeepholeSubstituteAlternateSyntax {
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;
}
if *computed {
*computed = false;
}
if is_identifier_name(&s.value) {
if is_identifier_name(value) {
self.changed = true;
*key = PropertyKey::StaticIdentifier(
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;
*key = PropertyKey::NumericLiteral(ctx.ast.alloc_numeric_literal(
s.span,
Expand Down Expand Up @@ -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' = _ }"
);

test_same("class C { ['prototype']() {} }");
test_same("class C { ['__proto__']() {} }");
test_same("class C { ['constructor']() {} }");
}

#[test]
Expand Down

0 comments on commit cb098c7

Please sign in to comment.