Skip to content

Commit 1fec35a

Browse files
claude[bot]kdy1
andcommitted
fix(es/minifier): Make array.join optimization more conservative
Only allow optimization for literals (string, number, null) and pure undefined. All other expressions (identifiers, function calls, member expressions, etc.) can potentially be null/undefined and should prevent optimization. Co-authored-by: Donny/강동윤 <[email protected]>
1 parent 6c8ee70 commit 1fec35a

File tree

2 files changed

+14
-5
lines changed
  • crates/swc_ecma_minifier

2 files changed

+14
-5
lines changed

crates/swc_ecma_minifier/src/compress/pure/misc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,10 +563,7 @@ impl Pure<'_> {
563563
.any(|v| match &*v.expr {
564564
e if is_pure_undefined(self.expr_ctx, e) => false,
565565
Expr::Lit(lit) => !matches!(lit, Lit::Str(..) | Lit::Num(..) | Lit::Null(..)),
566-
// This can change behavior if the value is `undefined` or `null`.
567-
Expr::Ident(..) => false,
568-
Expr::OptChain(..) => false,
569-
Expr::Member(..) => false,
566+
// All other expressions can potentially be null/undefined
570567
_ => true,
571568
})
572569
{

crates/swc_ecma_minifier/tests/fixture/issues/10936/input.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,16 @@ const test1 = [1, null, 2].join('');
1717
const test2 = [1, undefined, 2].join('');
1818
const test3 = [1, variable?.notExist, 2].join('');
1919
const test4 = [1, variable.data?.value, 2].join('');
20-
const test5 = [1, obj?.a?.b?.c, 2].join('');
20+
const test5 = [1, obj?.a?.b?.c, 2].join('');
21+
22+
// Function calls can return null/undefined
23+
const test6 = [1, someFunction(), 2].join('');
24+
const test7 = [1, obj.method(), 2].join('');
25+
26+
// Identifiers can be null/undefined
27+
const test8 = [1, unknownVar, 2].join('');
28+
29+
// Other expressions that can be null/undefined
30+
const test9 = [1, condition ? null : 'x', 2].join('');
31+
const test10 = [1, await promise, 2].join('');
32+
const test11 = [1, new Constructor(), 2].join('');

0 commit comments

Comments
 (0)