Skip to content

Commit 803050e

Browse files
committed
fix(minifier): handle __proto__ when inlining single-use variables
1 parent 165a4af commit 803050e

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

crates/oxc_minifier/src/peephole/minimize_statements.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,17 @@ impl<'a> PeepholeOptimizations {
16051605
ctx,
16061606
)
16071607
{
1608+
if prop.shorthand && prop.key.is_specific_id("__proto__") {
1609+
// { __proto__ } -> { ['__proto__']: value }
1610+
prop.computed = true;
1611+
prop.key =
1612+
PropertyKey::from(ctx.ast.expression_string_literal(
1613+
prop.key.span(),
1614+
"__proto__",
1615+
None,
1616+
));
1617+
}
1618+
prop.shorthand = false;
16081619
return Some(changed);
16091620
}
16101621
}

crates/oxc_minifier/tests/peephole/inline_single_use_variable.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,15 @@ fn test_inline_single_use_variable() {
212212
"function wrapper() { var x = a; for (let a of x) console.log(a) }",
213213
"function wrapper() { var x = a; for (let a of x) console.log(a) }",
214214
);
215+
216+
test(
217+
"function wrapper() { var __proto__ = []; return { __proto__: __proto__ } }",
218+
"function wrapper() { return { __proto__: [] } }",
219+
);
220+
test(
221+
"function wrapper() { var __proto__ = []; return { __proto__ } }",
222+
"function wrapper() { return { ['__proto__']: [] } }",
223+
);
215224
}
216225

217226
#[test]

tasks/coverage/snapshots/minifier_node_compat.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@ commit: ed0d6ba5
22

33
minifier_node_compat Summary:
44
AST Parsed : 938/938 (100.00%)
5-
Positive Passed: 928/938 (98.93%)
5+
Positive Passed: 929/938 (99.04%)
66
execution_result: tasks/coverage/ES2015/annex b__proto__ in object literalsbasic support
77

8-
execution_result: tasks/coverage/ES2015/annex b__proto__ in object literalsnot a shorthand property
9-
108
execution_result: tasks/coverage/ES2015/built-insProxy"getOwnPropertyDescriptor" handler invariants
119

1210
execution_result: tasks/coverage/ES2015/miscProxy, internal 'get' callsHasBinding

0 commit comments

Comments
 (0)