Repository navigation
fix(ssr): rewrite computed keys in defaulted destructuring params - #23304
Closed
yigiterturk-dev wants to merge 1 commit into
Closed
yigiterturk-dev wants to merge 1 commit into
yigiterturk-dev wants to merge 1 commit into
Conversation
Author
|
The one red job here is unrelated to this change — flagging it so it doesn't cost a reviewer time.
preceded by The same job and step is currently failing on The unit tests that cover this change — |
A parameter that is an AssignmentPattern is walked generically instead of
going through handlePattern, and the only property-key guard there is
isStaticPropertyKey, which matches non-computed keys only. A computed key
therefore fell through and was registered as a locally scoped binding, so
the later pass skipped the import rewrite and left a bare identifier:
import { KEY } from './data.js'
function f({ [KEY]: v = null } = {}) {} // ReferenceError: KEY is not defined
Guard on the key/value relationship instead, mirroring handlePattern.
Shorthand bindings keep their scoping because key === value there.
Fixes #23232
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Smwb1MRyiGRRMEEDVioV88
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #23232.
Problem
In
ssrTransform's scope walker, function parameters take two paths. A parameter that is directly anObjectPattern/ArrayPatterngoes throughhandlePattern, which recurses into each property's value and so never scopes a key. A parameter that is anAssignmentPattern(has a default) is instead walked generically, where the only property-key guard isisStaticPropertyKey— and that matches non-computed keys only.A computed key therefore falls through to
setScope(...)and is registered as a locally scoped binding. The rewrite pass then treats it as a local and skips the import rewrite, leaving a bare identifier:The trigger is the default on the parameter, not on the property —
f({ [KEY]: v = null })is fine,f({ [KEY]: v } = {})is not. Beyond the plain object case it also affects arrow functions, class methods, nested patterns and array patterns. Non-parameter positions (object literals, in-body destructuring) were never affected.Fix
Guard on the key/value relationship instead of on staticness, mirroring what
handlePatternalready does:Computed keys stop being scoped and fall through to the normal rewrite. Shorthand bindings (
{ x } = {}) keep their scoping, sincekey === valuethere and theparent.value !== childclause lets them through tosetScope.Alternatives considered
Extending
isStaticPropertyKeyitself would have been smaller, but that helper is also used byisRefIdentifier, where "static" genuinely is the property being tested — widening it there would change unrelated behaviour. Keeping the change local to the parameter walker avoids that.Tests
Three added to
ssrTransform.spec.ts:The first two fail on
mainand pass with this change. The third passes either way and is there to pin the shadowing behaviour, which is the thing most at risk from this guard.Full unit suite is green — 936 passed, 3 skipped across 68 files — plus
eslintandoxfmtclean.Attention for reviewers
The shadowing side is where I would look hardest. Besides the test above I checked, against a module exporting
KEY, that a local of the same name still wins in:f({ KEY } = {}),f({ KEY }),f({ a: KEY } = {}),f(KEY),f({ KEY = 'D' } = {})andf([KEY] = []), while a plain reference to the import still resolves to the import.🤖 Generated with Claude Code
https://claude.ai/code/session_01Smwb1MRyiGRRMEEDVioV88