diff options
author | 2023-04-09 03:43:04 -0700 | |
---|---|---|
committer | 2023-04-10 11:58:19 -0700 | |
commit | e5c431f52ea2559b9c8cf6ac2496c1a62b8b1cf8 (patch) | |
tree | f231ef950b82f668e99a6fd5a17732117fad48fb | |
parent | 57d0f2da7e532dd29c60575ef0739558a5b4aa93 (diff) | |
download | bun-e5c431f52ea2559b9c8cf6ac2496c1a62b8b1cf8.tar.gz bun-e5c431f52ea2559b9c8cf6ac2496c1a62b8b1cf8.tar.zst bun-e5c431f52ea2559b9c8cf6ac2496c1a62b8b1cf8.zip |
Fixes #2594 (#2600)
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r-- | src/js_ast.zig | 10 | ||||
-rw-r--r-- | src/js_parser.zig | 3 | ||||
-rw-r--r-- | test/bundler/transpiler.test.js | 16 |
3 files changed, 28 insertions, 1 deletions
diff --git a/src/js_ast.zig b/src/js_ast.zig index f8adaa953..d3f908fd0 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -2526,6 +2526,16 @@ pub const Expr = struct { }, this.loc); } + pub fn canBeInlinedFromPropertyAccess(this: Expr) bool { + return switch (this.data) { + // if the array has a spread we must keep it + // https://github.com/oven-sh/bun/issues/2594 + .e_spread => false, + + .e_missing => false, + else => true, + }; + } pub fn canBeConstValue(this: Expr) bool { return this.data.canBeConstValue(); } diff --git a/src/js_parser.zig b/src/js_parser.zig index 2df6240ea..219aee8ad 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -15318,7 +15318,8 @@ fn NewParser_( target.data.e_array.items.len == 1 and e_.index.data == .e_number and e_.index.data.e_number.value == 0.0 and - e_.optional_chain == null) + e_.optional_chain == null and + target.data.e_array.items.ptr[0].canBeInlinedFromPropertyAccess()) { return target.data.e_array.items.ptr[0]; } diff --git a/test/bundler/transpiler.test.js b/test/bundler/transpiler.test.js index 85c7affdb..7d05c2e65 100644 --- a/test/bundler/transpiler.test.js +++ b/test/bundler/transpiler.test.js @@ -73,6 +73,22 @@ describe("Bun.Transpiler", () => { ts.expectPrinted_("console.log(`\r\n\r\n\r\n`)", "console.log(`\n\n\n`);\n"); }); + describe("property access inlining", () => { + it("bails out with spread", () => { + ts.expectPrinted_("const a = [...b][0];", "const a = [...b][0]"); + ts.expectPrinted_("const a = {...b}[0];", "const a = { ...b }[0]"); + }); + it("bails out with multiple items", () => { + ts.expectPrinted_("const a = [b, c][0];", "const a = [b, c][0]"); + }); + it("works", () => { + ts.expectPrinted_('const a = ["hey"][0];', 'const a = "hey"'); + }); + it("works nested", () => { + ts.expectPrinted_('const a = ["hey"][0][0];', 'const a = "h"'); + }); + }); + describe("TypeScript", () => { it("import Foo = Baz.Bar", () => { ts.expectPrinted_("import Foo = Baz.Bar;\nexport default Foo;", "const Foo = Baz.Bar;\nexport default Foo"); |