Skip to content

fix(es/minifier): Keep function and class declarations whose self-reference is dropped - #12401

Merged
Donny/강동윤 (kdy1) merged 1 commit into
swc-project:mainfrom
baltasarblanco:fix/12400-dce-self-ref-usage
Sep 23, 2026
Merged

Donny/강동윤 (kdy1) merged 1 commit into
swc-project:mainfrom
baltasarblanco:fix/12400-dce-self-ref-usage

Conversation

@baltasarblanco

Copy link
Copy Markdown
Contributor

Description:

The tree shaker's Analyzer deliberately does not count a function's or class's references to itself (VarInfo docs: "This does not include self-references in a function"). The Dropper, which subtracts usages when a node is removed, had no such exclusion. So when an unused local initialized with the enclosing function's name was dropped, the function lost a usage it never gained, hit zero, and was dropped on the next pass while still referenced from outside:

(function () {
  function handler() { var self = handler; }
  setTimeout(handler, 0);
})();
// ReferenceError: handler is not defined

This makes the Dropper track the innermost enclosing named function/class the same way the Analyzer does, and skip self-references when subtracting. The same mechanism applied to class declarations, so both are covered.

Tests:

Two exec tests in tests/exec.rs (function declaration and class declaration variants). Both fail on main with ReferenceError and pass with this change. ./scripts/exec.sh and the full swc_ecma_transforms_optimization and swc_ecma_minifier suites pass with no fixture output changes.

Related issue (if exists):

Closes #12400

@changeset-bot

changeset-bot Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 3644472

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@codspeed

codspeed Bot commented Sep 23, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 200 untouched benchmarks
⏩ 67 skipped benchmarks1


Comparing baltasarblanco:fix/12400-dce-self-ref-usage (3644472) with main (42e4f7e)

Open in CodSpeed

Footnotes

  1. 67 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@kdy1 Donny/강동윤 (kdy1) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@kdy1
Donny/강동윤 (kdy1) merged commit cd25b14 into swc-project:main Sep 23, 2026
73 checks passed
@github-actions github-actions Bot modified the milestones: Planned, v1.16.12 Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

es/minifier: function declaration removed while still referenced when its body assigns it to an unused local

2 participants