fix(ssr): rewrite imported binding used as computed key in defaulted destructuring param - #23233
Open
webbertakken wants to merge 2 commits into
Open
fix(ssr): rewrite imported binding used as computed key in defaulted destructuring param#23233webbertakken wants to merge 2 commits into
webbertakken wants to merge 2 commits into
Conversation
…destructuring param
A destructured parameter with a default value (`{ [KEY]: v } = {}`) is an AssignmentPattern, so it is walked by the generic param walker instead of handlePattern. That walker only skipped non-computed property keys (isStaticPropertyKey), so a computed key referencing an imported binding was registered as a local scope variable, causing the SSR import-rewrite to be skipped and throwing `ReferenceError` at runtime.
Skip any property key that is not the bound value (static or computed), mirroring handlePattern, so computed keys stay rewritable while shorthand bindings remain scoped.
Fixes vitejs#23232
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.
Description
Under SSR, an imported binding used as a computed key in a destructured parameter that has a default value (
function f({ [KEY]: v } = {}) {}) was not rewritten to the import namespace, throwingReferenceError: KEY is not definedat runtime (viassrLoadModule, vite-node, and Vitest).Fixes #23232.
Root cause
A destructured parameter with a default is an
AssignmentPattern, so it is walked by the generic param walker inssrTransformrather than byhandlePattern. That walker only skipped non-computed property keys (isStaticPropertyKey), so a computed key referencing an imported binding was registered as a local scope variable viasetScope. The later rewrite pass then treated it as a local and skipped the import rewrite, leaving a bare identifier.handlePattern(used for non-defaultedObjectPattern/ArrayPatternparams) already ignores computed keys, which is why the same code without a default value worked.Fix
Skip any property key that is not the bound value (both static and computed), mirroring
handlePattern:Shorthand bindings (
{ x } = {}, wherekey === value) remain scoped; computed keys stay rewritable.Tests
Added
packages/vite/src/node/ssr/__tests__/ssrTransform.spec.tscase verifying the computed key is rewritten in both the parameter and the function body. The fullssrTransformspec passes (76 tests). Also verified end-to-end with a standalonecreateServer().ssrLoadModule(...)repro: throws before this change, returns the expected object after.Note: this contribution was prepared with AI assistance; I have verified the reasoning, the fix, and the tests.