Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/dry-queens-clean.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_ecma_minifier: patch
swc_core: patch
---

fix(es/minifier): Preserve `__proto__` shorthand property behavior
33 changes: 24 additions & 9 deletions crates/swc_ecma_minifier/src/compress/optimize/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,8 +469,9 @@ impl VisitMut for Finalizer<'_> {

if let Prop::Shorthand(i) = n {
if let Some(expr) = self.lits.get(&i.to_id()) {
let key = prop_name_from_ident(i.take());
*n = Prop::KeyValue(KeyValueProp {
key: i.take().into(),
key,
value: expr.clone(),
});
self.changed = true;
Expand Down Expand Up @@ -566,10 +567,8 @@ impl VisitMut for NormalMultiReplacer<'_> {
debug!("multi-replacer: Replaced `{}` as shorthand", i);
self.changed = true;

*p = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(IdentName::new(i.sym.clone(), i.span)),
value,
});
let key = prop_name_from_ident(i.take());
*p = Prop::KeyValue(KeyValueProp { key, value });
}
}
}
Expand Down Expand Up @@ -642,10 +641,8 @@ impl VisitMut for ExprReplacer {
} else {
unreachable!("`{}` is already taken", i)
};
*p = Prop::KeyValue(KeyValueProp {
key: PropName::Ident(i.clone().into()),
value,
});
let key = prop_name_from_ident(i.take());
*p = Prop::KeyValue(KeyValueProp { key, value });
}
}
}
Expand Down Expand Up @@ -820,3 +817,21 @@ pub fn get_ids_of_pat(pat: &Pat) -> Vec<Id> {
append(pat, &mut idents);
idents
}

/// Creates a PropName for a shorthand property, handling the special case of
/// `__proto__`. When the property name is `__proto__`, it must be converted to
/// a computed property to preserve JavaScript semantics.
fn prop_name_from_ident(ident: Ident) -> PropName {
if ident.sym == "__proto__" {
PropName::Computed(ComputedPropName {
span: ident.span,
expr: Box::new(Expr::Lit(Lit::Str(Str {
span: ident.span,
value: ident.sym.clone(),
raw: None,
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider using Some(format!("\"{}\"", ident.sym)) instead of None for the raw field to maintain consistent string literal formatting in the generated code.

Suggested change
raw: None,
raw: Some(format!("\"{}\"", ident.sym)),

Copilot uses AI. Check for mistakes.
}))),
})
} else {
ident.into()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var __proto__ = []; console.log({ __proto__ } instanceof Array) // false
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
console.log(({
["__proto__"]: []
}) instanceof Array); // false
Loading