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>
This commit is contained in:
Dylan Conway
2025-09-19 17:32:12 -07:00
committed by GitHub
parent b2b1bc9ba8
commit d3d68f45fd
2 changed files with 24 additions and 0 deletions

View File

@@ -109,6 +109,9 @@ pub const KnownGlobal = enum {
return js_ast.Expr.init(E.Array, .{ .items = e.args }, loc);
},
.number => {
if (arg.data != .e_number) {
return callFromNew(e, loc);
}
const val = arg.data.e_number.value;
if (
// only want this with whitespace minification

View File

@@ -0,0 +1,21 @@
import { build, file } from "bun";
import { expect, test } from "bun:test";
import { tempDir } from "harness";
import { join } from "path";
test("minifying new Array(if (0) 1 else 2) works", async () => {
using testDir = tempDir("minify-new-array-with-if", {
"entry.js": "console.log(new Array(Math.random() > -1 ? 1 : 2));",
});
await build({
entrypoints: [join(testDir, "entry.js")],
minify: true,
outdir: join(testDir, "outdir"),
});
expect(await file(join(testDir, "outdir/entry.js")).text()).toMatchInlineSnapshot(`
"console.log(Array(Math.random()>-1?1:2));
"
`);
});