From 9b8054ae11cdcbfb94974c55b492b553b0e8bc1d Mon Sep 17 00:00:00 2001 From: Dylan Conway <35280289+dylan-conway@users.noreply.github.com> Date: Fri, 16 Jun 2023 21:11:57 -0700 Subject: don't remove const if referenced before declaration (#3337) --- src/js_parser.zig | 6 +++--- 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"); + }, + }); }); -- cgit v1.2.3