-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix(es/minifier): Prevent array destructuring optimization in assignment contexts #11221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…ent contexts This fixes issue #11084 where the minifier incorrectly optimized array expressions used as assignment targets in destructuring assignments, causing them to be replaced with literal values. Example of the bug: ```javascript const bin = { hasMore: false, hasDisorder: false }; [bin.hasMore, bin.hasDisorder] = [true, true]; // Was incorrectly minified to: [!1, !1] = [!0, !0] // Which produces: SyntaxError: Invalid destructuring assignment target ``` The fix adds a context check to `eval_array_spread()` to skip optimization when the array is used: - As the left-hand side of an assignment (IS_LHS_OF_ASSIGN) - In a delete operation (IN_DELETE) - As an argument to update operators like ++ or -- (IS_UPDATE_ARG) This follows the same pattern used by other evaluation functions in the codebase, such as `eval_array_method_call()`. Closes #11084 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
|
|
|
|
🦋 Changeset detectedLatest commit: 2ccefa2 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 |
|
🤖 This pull request has been linked to DevBird Task #1193 View the task details and manage the automated development workflow in DevBird. Learn more about DevBird here or the announcement blog post here. |
📋 DevBird Task PromptObjectiveFix the bug in SWC minifier where array destructuring assignments are incorrectly optimized, causing the minifier to replace valid assignment targets with literal values, resulting in a SyntaxError. Issue DetailsGitHub Issue: #11084 Problem: When minifying code with array destructuring assignments like: const bin = { hasMore: false, hasDisorder: false };
[bin.hasMore, bin.hasDisorder] = [true, true];The minifier incorrectly outputs: [!1, !1] = [!0, !0];This produces a "SyntaxError: Invalid destructuring assignment target" because the left side becomes literal boolean values instead of valid assignment targets. Root Cause AnalysisThe bug is in Similar functions that correctly check context:
Technical RequirementsDocumentation & Resources
Code Location
Implementation Steps
Success Criteria
Project-Specific Requirements (from CLAUDE.md)
Context References
This comment was automatically added by DevBird. You can disable this feature in DevBird Settings. |
|
Claude finished @DeLino[bot]'s task —— View job Code Review for Array Destructuring Minifier FixReviewing the pull request for issue #11084... Todo List:
✅ Overall AssessmentThis is a well-implemented fix that correctly addresses the array destructuring optimization bug. The solution follows established patterns in the codebase and includes comprehensive test coverage. 🔍 Code Quality Analysis✅ Core Fix (
|
Binary Sizes
Commit: 719deea |
CodSpeed Performance ReportMerging #11221 will not alter performanceComparing Summary
Footnotes |
Update the expected test output to match the actual formatting produced by the minifier. The fix is working correctly - it preserves assignment targets on the left-hand side of destructuring assignments and doesn't optimize them away. The only difference was in the code formatting (multi-line vs single-line array literals). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a minification bug where array literals used as left-hand side (LHS) of destructuring assignments were incorrectly being optimized. The fix prevents the minifier from transforming boolean literals within arrays when those arrays are assignment targets.
- Added guard to prevent optimization of arrays used as assignment targets in destructuring patterns
- Added test case covering various destructuring assignment scenarios
- Ensures expressions like
[obj.prop] = [true]are not incorrectly transformed to[!0] = [!0]
Reviewed Changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| crates/swc_ecma_minifier/src/compress/pure/evaluate.rs | Added context check to prevent array optimization when used as LHS of assignments |
| crates/swc_ecma_minifier/tests/fixture/issues/11084/input.js | Input test file with destructuring assignment patterns |
| crates/swc_ecma_minifier/tests/fixture/issues/11084/output.js | Expected output demonstrating correct handling of destructuring assignments |
| crates/swc_ecma_minifier/tests/fixture/issues/11084/config.json | Test configuration enabling booleans, properties, and evaluate optimizations |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if self.ctx.intersects( | ||
| Ctx::IN_DELETE | ||
| .union(Ctx::IS_UPDATE_ARG) | ||
| .union(Ctx::IS_LHS_OF_ASSIGN), | ||
| ) { | ||
| return; | ||
| } |
Copilot
AI
Nov 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment on line 76-79 states this prevents transformations like '[obj.prop] = [true] => [!0] = [!0]', but this example is misleading. The actual problematic transformation would be '[obj.prop] = [true] => [obj.prop] = [!0]' where the right-hand side array's boolean literal gets optimized. The left-hand side array should never be transformed to '[!0]' as it's an assignment target, not a literal.
Summary
This PR fixes issue #11084 where the SWC minifier incorrectly optimized array destructuring assignments, causing the minifier to replace valid assignment targets with literal values, resulting in a
SyntaxError.Problem
When minifying code with array destructuring assignments like:
The minifier incorrectly outputs:
This produces a "SyntaxError: Invalid destructuring assignment target" because the left side becomes literal boolean values instead of valid assignment targets.
Root Cause
The bug is in
eval_array_spread()function (crates/swc_ecma_minifier/src/compress/pure/evaluate.rs:71-110). This function performs array optimization but did not check the context flags to determine if the array is being used in an assignment context where optimization should be skipped.Solution
Added a context check at the beginning of
eval_array_spread()to prevent optimization when the array is:IS_LHS_OF_ASSIGN)IN_DELETE)IS_UPDATE_ARG)This follows the same pattern used by other evaluation functions in the codebase, such as
eval_array_method_call()at line 188-199.Changes
eval_array_spread()incrates/swc_ecma_minifier/src/compress/pure/evaluate.rscrates/swc_ecma_minifier/tests/fixture/issues/11084/:Test Plan
cargo fmt --allRelated Issues
Closes #11084
🤖 Generated with Claude Code