Skip to content

Commit d3d68f4

Browse files
fix(bundler): minify Array constructor with ternary regression (#22803)
### What does this PR do? Fixes accessing the wrong union field. Resolves BUN-WQF ### How did you verify your code works? Added a regression test --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent b2b1bc9 commit d3d68f4

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/ast/KnownGlobal.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ pub const KnownGlobal = enum {
109109
return js_ast.Expr.init(E.Array, .{ .items = e.args }, loc);
110110
},
111111
.number => {
112+
if (arg.data != .e_number) {
113+
return callFromNew(e, loc);
114+
}
112115
const val = arg.data.e_number.value;
113116
if (
114117
// only want this with whitespace minification
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { build, file } from "bun";
2+
import { expect, test } from "bun:test";
3+
import { tempDir } from "harness";
4+
import { join } from "path";
5+
6+
test("minifying new Array(if (0) 1 else 2) works", async () => {
7+
using testDir = tempDir("minify-new-array-with-if", {
8+
"entry.js": "console.log(new Array(Math.random() > -1 ? 1 : 2));",
9+
});
10+
11+
await build({
12+
entrypoints: [join(testDir, "entry.js")],
13+
minify: true,
14+
outdir: join(testDir, "outdir"),
15+
});
16+
17+
expect(await file(join(testDir, "outdir/entry.js")).text()).toMatchInlineSnapshot(`
18+
"console.log(Array(Math.random()>-1?1:2));
19+
"
20+
`);
21+
});

0 commit comments

Comments
 (0)