Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/renamer.zig
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,14 @@ pub const MinifyRenamer = struct {
var slots = this.slots.getPtr(ns);
sorted.clearRetainingCapacity();
try sorted.ensureUnusedCapacity(slots.items.len);
sorted.items.len = slots.items.len;

for (sorted.items, slots.items, 0..) |*elem, slot, i| {
elem.* = SlotAndCount{
for (slots.items, 0..) |slot, i| {
// Skip symbols with zero use count - they're never used and don't need a minified name
if (slot.count == 0) continue;
sorted.appendAssumeCapacity(SlotAndCount{
.slot = @as(u32, @intCast(i)),
.count = slot.count,
};
});
}
std.sort.pdq(SlotAndCount, sorted.items, {}, SlotAndCount.lessThan);

Expand Down
32 changes: 32 additions & 0 deletions test/bundler/bundler_minify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,4 +1135,36 @@ describe("bundler", () => {
);
},
});
// Regression test for: https://github.com/oxc-project/oxc/pull/18183
// When symbols have zero use count, we skip them during renaming.
// This tests that when nested scopes are tree-shaken, their pre-allocated
// symbol slots (which have count=0) are skipped during name assignment.
itBundled("minify/SkipUnusedSymbolSlots", {
files: {
"/entry.js": /* js */ `
// Create nested scopes with many symbols that will be tree-shaken
function unused() {
var a = 1, b = 2, c = 3;
function inner() {
var x = 1, y = 2, z = 3, w = 4, v = 5;
return x + y + z + w + v;
}
return a + b + c + inner();
}
// This function is used and should get short minified names
function used() { return 42; }
console.log(used());
`,
},
minifyIdentifiers: true,
minifyWhitespace: true,
minifySyntax: true,
onAfterBundle(api) {
const code = api.readFile("/out.js").trim();
// The unused function and its nested scopes are tree-shaken.
// The optimization ensures we don't waste time generating names for
// the pre-allocated slots that were never used.
expect(code).toMatchInlineSnapshot(`"function j(){return 42}console.log(j());"`);
},
});
});