fix: bindable props on class instances (spread + mount)#18143
Open
ashishkr96 wants to merge 1 commit intosveltejs:mainfrom
Open
fix: bindable props on class instances (spread + mount)#18143ashishkr96 wants to merge 1 commit intosveltejs:mainfrom
ashishkr96 wants to merge 1 commit intosveltejs:mainfrom
Conversation
`Object.getOwnPropertyDescriptor` skips inherited accessors, so spread/`mount` props that come from class instances (including class fields with `$state`) silently lost their setter and bindings became no-ops. Walks the prototype chain to find accessor descriptors, routes spread-proxy writes through `props[key] = v` so `this` resolves correctly, and applies the same fix to SSR `spread_props` / `bind_props`. Closes sveltejs#18140 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 2d2bae5 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 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.
Summary
Closes #18140.
Object.getOwnPropertyDescriptoronly inspects own properties, so when a class instance was passed as props (<Comp {...instance} />ormount(Comp, instance)), Svelte couldn't find the inherited accessor and silently dropped the setter. Reads worked, writes didn't — bindings became no-ops, which made it awkward to use classes in component tests wheremount/renderis the natural entry point.This walks the prototype chain (stopping at
Object.prototype) when resolving property descriptors in:spread_props_handler.setandgetOwnPropertyDescriptor(client) — so<Comp {...instance} />finds the prototype accessor.prop()(client, themountpath) — same, plus the setter is now invoked viaprops[key] = vsothisresolves to the class instance and re-enters the spread proxy'ssettrap when relevant.spread_propsandbind_props(server) — SSR previously relied onObject.keys/getOwnPropertyDescriptor, which also missed inherited accessors. Inherited accessors are now copied withget/setbound to the source object sothissurvives.A shared
get_descriptor_in_chainhelper lives ininternal/shared/utils.js.Test plan
bind-and-spread-class-instance—<Comp {...accessor} />and<Comp {...field} />(class fields with$state) round-trip writes through to the parent's class instance, on dom/hydrate/ssr/async-ssr.mount-class-instance-props— same coverage viamount(Component, classInstance).runtime-runes,runtime-legacy,server-side-rendering,hydrationsuites still green.pnpm checkandpnpm lintpass.