Skip to content

Fix IIFE optimization removing const/let declarations causing undefined variable errors#10855

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/fix-10849
Closed

Fix IIFE optimization removing const/let declarations causing undefined variable errors#10855
Copilot wants to merge 3 commits intomainfrom
copilot/fix-10849

Conversation

Copy link
Contributor

Copilot AI commented Jul 15, 2025

The IIFE (Immediately Invoked Function Expression) optimization was incorrectly handling const and let declarations, causing undefined variable errors in the minified output.

Issue

When minifying code with IIFE containing const or let declarations, the optimizer would:

  1. Extract initializers from variable declarations and convert them to assignments
  2. Add the now-empty declarations to prepend_stmts
  3. This created invalid declarations like const obj; (not allowed in JavaScript)

Example input:

(function () {
    const obj = { value: 42 };
    console.log(obj === null || obj === void 0 ? void 0 : obj.value)
})()

Before fix (invalid output):

console.log(null==obj?void 0:42);
// ReferenceError: obj is not defined

Fix

Modified the inline_fn_like function in crates/swc_ecma_minifier/src/compress/optimize/iife.rs to only add var declarations to prepend_stmts, since:

  • var declarations can exist without initializers: var obj;
  • const and let declarations cannot: const obj;

After fix (valid output):

!function(){const obj={value:42};console.log(null==obj?void 0:obj.value)}();
// Works correctly, outputs: 42

Testing

Added comprehensive test case in tests/fixture/iife_const_bug/ that verifies:

  • The minified output doesn't create undefined variable references
  • The generated code executes without errors
  • The optimization still provides benefits while maintaining correctness

Fixes #10849.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@changeset-bot
Copy link

changeset-bot bot commented Jul 15, 2025

⚠️ No Changeset found

Latest commit: 2bd1ceb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

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

Co-authored-by: kdy1 <29931815+kdy1@users.noreply.github.com>
@codspeed-hq
Copy link

codspeed-hq bot commented Jul 15, 2025

CodSpeed Performance Report

Merging #10855 will not alter performance

Comparing copilot/fix-10849 (2bd1ceb) with main (93b318f)

Summary

✅ 140 untouched benchmarks

Co-authored-by: kdy1 <29931815+kdy1@users.noreply.github.com>
Copilot AI changed the title [WIP] Minifier bug with iife and undefined variable Fix IIFE optimization removing const/let declarations causing undefined variable errors Jul 15, 2025
Copilot AI requested a review from kdy1 July 15, 2025 03:44
Copilot finished work on behalf of kdy1 July 15, 2025 03:44
@kdy1
Copy link
Member

kdy1 commented Jul 15, 2025

@copilot Fix CI

@kdy1
Copy link
Member

kdy1 commented Jul 15, 2025

Copilot does not work well

@kdy1 kdy1 closed this Jul 15, 2025
@kdy1 kdy1 deleted the copilot/fix-10849 branch July 15, 2025 04:00
@kdy1 kdy1 added this to the Unknown milestone Jul 21, 2025
@swc-project swc-project locked as resolved and limited conversation to collaborators Aug 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Minifier bug with iife and undefined variable

3 participants

Comments