diff options
author | 2023-06-16 21:11:57 -0700 | |
---|---|---|
committer | 2023-06-16 21:11:57 -0700 | |
commit | 9b8054ae11cdcbfb94974c55b492b553b0e8bc1d (patch) | |
tree | 5fe47be3f64318db18487ec6e665b42fbf816cd7 | |
parent | 0591c6b4bbb672bdb1472a5d488867af22e92d58 (diff) | |
download | bun-9b8054ae11cdcbfb94974c55b492b553b0e8bc1d.tar.gz bun-9b8054ae11cdcbfb94974c55b492b553b0e8bc1d.tar.zst bun-9b8054ae11cdcbfb94974c55b492b553b0e8bc1d.zip |
don't remove const if referenced before declaration (#3337)
-rw-r--r-- | src/js_parser.zig | 6 | ||||
-rw-r--r-- | test/bundler/esbuild/default.test.ts | 42 |
2 files changed, 45 insertions, 3 deletions
diff --git a/src/js_parser.zig b/src/js_parser.zig index a64e32114..5a9bca91a 100644 --- a/src/js_parser.zig +++ b/src/js_parser.zig @@ -5798,8 +5798,7 @@ fn NewParser_( if (p.options.features.inlining) { if (p.const_values.get(ref)) |replacement| { - // TODO: - // p.ignoreUsage(ref); + p.ignoreUsage(ref); return replacement; } } @@ -20427,7 +20426,8 @@ fn NewParser_( var end: usize = 0; for (decls) |decl| { if (decl.binding.data == .b_identifier) { - if (p.const_values.contains(decl.binding.data.b_identifier.ref)) { + const symbol = p.symbols.items[decl.binding.data.b_identifier.ref.innerIndex()]; + if (p.const_values.contains(decl.binding.data.b_identifier.ref) and symbol.use_count_estimate == 0) { continue; } } diff --git a/test/bundler/esbuild/default.test.ts b/test/bundler/esbuild/default.test.ts index 215276139..0d1775606 100644 --- a/test/bundler/esbuild/default.test.ts +++ b/test/bundler/esbuild/default.test.ts @@ -6520,4 +6520,46 @@ describe("bundler", () => { `, }, }); + itBundled("default/ConstDeclNotRemovedIfReferencedBeforeDecl", { + files: { + "/entry.js": ` + { + const foo = () => { + return data; + } + const data = 123; + + console.log(foo()); + } + `, + }, + minifySyntax: true, + run: { + stdout: "123", + }, + onAfterBundle(api) { + api.expectFile("/out.js").toContain("data = 123"); + }, + }); + itBundled("default/ConstDeclRemovedIfReferencedBeforeAllUses", { + files: { + "/entry.js": ` + { + const data = 123; + const foo = () => { + return data; + } + + console.log(foo()); + } + `, + }, + minifySyntax: true, + run: { + stdout: "123", + }, + onAfterBundle(api) { + api.expectFile("/out.js").not.toContain("data = 123"); + }, + }); }); |