diff --git a/src/ast/KnownGlobal.zig b/src/ast/KnownGlobal.zig index 35fb4c8190..08bd1ca950 100644 --- a/src/ast/KnownGlobal.zig +++ b/src/ast/KnownGlobal.zig @@ -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 diff --git a/test/regression/issue/minify-new-array-with-if.test.ts b/test/regression/issue/minify-new-array-with-if.test.ts new file mode 100644 index 0000000000..4d55201f1a --- /dev/null +++ b/test/regression/issue/minify-new-array-with-if.test.ts @@ -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)); + " + `); +});