Skip to content

fix(ssr): rewrite computed keys in defaulted destructuring params - #23304

Closed
yigiterturk-dev wants to merge 1 commit into
vitejs:mainfrom
yigiterturk-dev:fix/ssr-computed-key-in-defaulted-param
Closed

yigiterturk-dev wants to merge 1 commit into
vitejs:mainfrom
yigiterturk-dev:fix/ssr-computed-key-in-defaulted-param

Conversation

@yigiterturk-dev

Copy link
Copy Markdown

Fixes #23232.

Problem

In ssrTransform's scope walker, function parameters take two paths. A parameter that is directly an ObjectPattern/ArrayPattern goes through handlePattern, which recurses into each property's value and so never scopes a key. A parameter that is an AssignmentPattern (has a default) is instead walked generically, where the only property-key guard is isStaticPropertyKey — 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:

import { KEY } from './data.js'
export function f({ [KEY]: v = null } = {}) { return v }
// emitted:  function f({ [KEY]: v = null } = {}) { ... }
// runtime:  ReferenceError: KEY is not defined

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 handlePattern already does:

if (parent?.type === 'Property' && parent.key === child && parent.value !== child) {
  return
}

Computed keys stop being scoped and fall through to the normal rewrite. Shorthand bindings ({ x } = {}) keep their scoping, since key === value there and the parent.value !== child clause lets them through to setScope.

Alternatives considered

Extending isStaticPropertyKey itself would have been smaller, but that helper is also used by isRefIdentifier, 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:

  • computed key in a defaulted destructuring param is rewritten
  • same for nested and array patterns
  • regression guard: a shorthand binding of the same name is not rewritten

The first two fail on main and 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 eslint and oxfmt clean.

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' } = {}) and f([KEY] = []), while a plain reference to the import still resolves to the import.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Smwb1MRyiGRRMEEDVioV88

@yigiterturk-dev

Copy link
Copy Markdown
Author

The one red job here is unrelated to this change — flagging it so it doesn't cost a reviewer time.

Build&Test: node-24, macos-latest failed on Test serve (bundled dev):

playground/json/__tests__/csr/json-csr.spec.ts > should full reload
AssertionError: expected '{"hmr":"this is hmr json"}' to match '"this is hmr update json"'

preceded by Error: transport was disconnected, cannot call "fetchModule". It's a JSON HMR timing assertion — nothing this PR touches. 689 passed, 1 failed, and the same suite is green on ubuntu (node 20/24/26) and windows.

The same job and step is currently failing on main too (run 32220262703), along with Test serve on windows in 32232101060 and 32220785973, so it looks like an existing flake rather than something introduced here.

The unit tests that cover this change — packages/vite/src/node/ssr/__tests__/ssrTransform.spec.ts — pass on every platform in this run.

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSR transform: imported binding used as a computed key in a defaulted destructured parameter is not rewritten (ReferenceError)

1 participant