aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Jarred Sumner <jarred@jarredsumner.com> 2023-05-18 13:27:05 -0700
committerGravatar GitHub <noreply@github.com> 2023-05-18 13:27:05 -0700
commitf3a1a3bb2bee56940426a6541c46775a2f8ab28a (patch)
tree2e7afa31e566b16feed02d4d3bcef666377768fd
parent755c0d62c47b0e8301a3d975697d476fe5c7436f (diff)
downloadbun-f3a1a3bb2bee56940426a6541c46775a2f8ab28a.tar.gz
bun-f3a1a3bb2bee56940426a6541c46775a2f8ab28a.tar.zst
bun-f3a1a3bb2bee56940426a6541c46775a2f8ab28a.zip
Fixes #2946 (#2949)
* Fixes #2946 * Update string_mutable.zig * Fixes #2948 --------- Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
-rw-r--r--src/js_parser.zig5
-rw-r--r--src/string_mutable.zig11
-rw-r--r--test/bundler/bundler_regressions.test.ts43
3 files changed, 57 insertions, 2 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig
index 90d7a5d24..ee6a5e6b8 100644
--- a/src/js_parser.zig
+++ b/src/js_parser.zig
@@ -20389,7 +20389,10 @@ fn NewParser_(
var decl = &prev_local.decls.slice()[0];
if (decl.binding.data == .b_identifier and
decl.binding.data.b_identifier.ref.eql(bin_assign.left.data.e_identifier.ref) and
- (decl.value == null or decl.value.?.isPrimitiveLiteral()))
+ // If the value was assigned, we shouldn't merge it incase it was used in the current statement
+ // https://github.com/oven-sh/bun/issues/2948
+ // We don't have a more granular way to check symbol usage so this is the best we can do
+ decl.value == null)
{
decl.value = bin_assign.right;
p.ignoreUsage(bin_assign.left.data.e_identifier.ref);
diff --git a/src/string_mutable.zig b/src/string_mutable.zig
index 2b8d80429..e6738ae7f 100644
--- a/src/string_mutable.zig
+++ b/src/string_mutable.zig
@@ -109,7 +109,12 @@ pub const MutableString = struct {
}
if (needs_gap) {
- var mutable = try MutableString.initCopy(allocator, str[0..start_i]);
+ var mutable = try MutableString.initCopy(allocator, if (start_i == 0)
+ // the first letter can be a non-identifier start
+ // https://github.com/oven-sh/bun/issues/2946
+ "_"
+ else
+ str[0..start_i]);
needs_gap = false;
var slice = str[start_i..];
@@ -137,6 +142,10 @@ pub const MutableString = struct {
has_needed_gap = true;
}
+ if (comptime bun.Environment.allow_assert) {
+ std.debug.assert(js_lexer.isIdentifier(mutable.list.items));
+ }
+
return try mutable.list.toOwnedSlice(allocator);
}
diff --git a/test/bundler/bundler_regressions.test.ts b/test/bundler/bundler_regressions.test.ts
index 057c33af6..35cf2f8b2 100644
--- a/test/bundler/bundler_regressions.test.ts
+++ b/test/bundler/bundler_regressions.test.ts
@@ -4,6 +4,19 @@ import { itBundled, testForFile } from "./expectBundled";
var { describe, test, expect } = testForFile(import.meta.path);
describe("bundler", () => {
+ // https://github.com/oven-sh/bun/issues/2946
+ itBundled("regression/InvalidIdentifierInFileName#2946", {
+ files: {
+ "/entry.js": "import foo from './1.png';\nconsole.log(foo);",
+ "/1.png": "abcdefgh",
+ },
+ entryPoints: ["/entry.js"],
+ outdir: "/out",
+ run: {
+ file: "/out/entry.js",
+ },
+ });
+
itBundled("regression/MergeAdjacentDecl#2942", {
files: {
"/shared.js": `
@@ -137,4 +150,34 @@ describe("bundler", () => {
"/b_13.js",
],
});
+
+ // https://github.com/oven-sh/bun/issues/2948
+ itBundled("regression/ReassignLocal#2948", {
+ files: {
+ "/entry.js": `
+ import { Buffer } from 'node:buffer';
+
+ export function schemaEncode(data) {
+ const filename_len = Buffer.byteLength(data.filename);
+ const buf = Buffer.allocUnsafe(29 + filename_len);
+ buf.writeUInt8(1);
+ for (let i=0; i<3; i++) buf.writeUInt32LE(data.to[i], 1 + i * 4);
+ buf.writeDoubleLE(data.random, 13);
+ buf.writeUInt16LE(data.page, 21);
+ let offset = 23;
+ offset = buf.writeUInt16LE(filename_len, offset);
+ offset += buf.write(data.filename, offset);
+ buf.writeUInt32LE(data.from, offset);
+ return buf;
+ }
+
+ schemaEncode({filename: "heyyyy", to: [1,2,3], page: 123, random: Math.random(), from: 158})
+ `,
+ },
+ minifySyntax: true,
+ target: "bun",
+ run: {
+ file: "/entry.js",
+ },
+ });
});