Skip to content

Commit 14ded24

Browse files
fix(signals): treat a plain merge() result as an ordinary source (#3384) (#3401)
merge()'s plain-object form stamped its flattened sources on the result under the $SOURCES symbol, and a later merge() flattened through them instead of reading the object. The result is a real object callers copy (Reflect.ownKeys descriptor copies, {...props}) or mutate afterwards (@solidjs/html assigns props and a children getter after spreading), so the re-merge resurrected removed keys and dropped added or overwritten ones. Nothing short of re-walking the object can validate the sources, so the plain form now records none and is read like any other source; only merge proxies, whose writes are no-ops, are still flattened. Side effect: merge(defaults, props) with props a plain merge result that covers every default now returns props instead of always allocating — flattening used to defeat the covered-source shortcut. Interleaved A/B (old vs new dist, same process): plain construct -3%, nested-not-covered construct +5–10%, nested-covered construct -48%, reads and proxy paths within noise. Co-authored-by: Cursor <[email protected]>
1 parent 042b540 commit 14ded24

3 files changed

Lines changed: 94 additions & 14 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
`merge()` no longer flattens through a plain-object merge result. That form is a real object callers may copy (`Reflect.ownKeys` descriptor copies, `{...props}`) or mutate afterwards (`@solidjs/html` assigns props and a `children` getter after spreading), and a later `merge` tunnelled back to the original sources through the `$SOURCES` symbol — resurrecting removed keys and dropping added or overwritten ones (#3384). The plain result now records no sources and is read like any other object; only merge *proxies*, whose writes are no-ops, are still flattened. A side effect: `merge(defaults, props)` where `props` is itself a plain merge result that covers every default now returns `props` directly instead of always allocating.

‎packages/signals/src/store/utils.ts‎

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,12 @@ function resolveSource(s: any) {
8181

8282
const $SOURCES = Symbol(__DEV__ ? "MERGE_SOURCE" : 0);
8383
/** @internal The flattened sources behind a `merge()` PROXY, or undefined.
84-
* Only the proxy form: its writes are no-ops, so the sources are the whole
85-
* truth. merge()'s plain-object form also records `$SOURCES` (so nested
86-
* merges flatten), but it is a real object callers may mutate afterwards
87-
* (html's tagged templates assign props after spreading) — those own writes
88-
* live on the object, not in the sources, so it must be read directly. */
84+
* Only the proxy form carries sources: its writes are no-ops, so they are
85+
* the whole truth. The plain-object form is a real object callers may copy
86+
* (descriptor copies, `{...props}`) or mutate afterwards (html's tagged
87+
* templates assign props and a children getter after spreading) — what is
88+
* on the object is the truth there, so it records nothing and every consumer,
89+
* a nested merge included, reads it directly (#3384). */
8990
export function mergeSources(o: any): any[] | undefined {
9091
return o != null && o[$PROXY] === o ? o[$SOURCES] : undefined;
9192
}
@@ -115,14 +116,25 @@ export function merge<T extends unknown[]>(...sources: T): Merge<T> {
115116
const flattened: T[] = [];
116117
for (let i = 0; i < sources.length; i++) {
117118
const s = sources[i];
118-
proxy = proxy || (!!s && $PROXY in (s as object));
119-
const childSources = !!s && (s as object)[$SOURCES];
120-
if (childSources) {
121-
for (let i = 0; i < childSources.length; i++) flattened.push(childSources[i]);
122-
} else
123-
flattened.push(
124-
typeof s === "function" ? ((proxy = true), createMemo(s as () => any)) : (s as any)
125-
);
119+
if (typeof s === "function") {
120+
proxy = true;
121+
flattened.push(createMemo(s as () => any) as any);
122+
continue;
123+
}
124+
if (s && $PROXY in (s as object)) {
125+
proxy = true;
126+
// Only a merge() PROXY is flattened through: its writes are no-ops, so
127+
// its sources are exactly what it reads. A plain-object merge result
128+
// is an ordinary source — it may have been copied or mutated since,
129+
// and what is on the object is the truth (#3384). omit() proxies
130+
// answer $SOURCES with undefined on purpose (#3014).
131+
const childSources = (s as object)[$SOURCES];
132+
if (childSources) {
133+
for (let j = 0; j < childSources.length; j++) flattened.push(childSources[j]);
134+
continue;
135+
}
136+
}
137+
flattened.push(s as any);
126138
}
127139
if (SUPPORTS_PROXY && proxy) {
128140
return new Proxy(
@@ -188,7 +200,6 @@ export function merge<T extends unknown[]>(...sources: T): Merge<T> {
188200
if (desc.get) Object.defineProperty(target, key, desc);
189201
else target[key] = desc.value;
190202
}
191-
(target as any)[$SOURCES] = flattened;
192203
return target as any;
193204
}
194205

‎packages/signals/tests/store/utilities.test.ts‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
flush,
88
getOwner,
99
merge,
10+
mergeSources,
1011
omit,
1112
reconcile,
1213
snapshot,
@@ -177,6 +178,69 @@ describe("merge", () => {
177178
b.value = 2;
178179
expect(props.b.value).toBe(2);
179180
});
181+
// #3384: the plain-object result is a real object callers may copy or
182+
// mutate. A later merge must read what is on the object, not tunnel back
183+
// to the sources it was built from.
184+
it("re-merging a descriptor copy of a merged object reads the copy (#3384)", () => {
185+
const props = merge({ href: "/x", title: "t" }, { $active: true, children: "hi" });
186+
const out: Record<PropertyKey, unknown> = {};
187+
for (const key of Reflect.ownKeys(props)) {
188+
if (key === "$active") continue;
189+
Object.defineProperty(out, key, Reflect.getOwnPropertyDescriptor(props, key)!);
190+
}
191+
Object.defineProperty(out, "class", {
192+
get: () => "yak-abc",
193+
enumerable: true,
194+
configurable: true
195+
});
196+
const final = merge(out, { rel: "noopener" }) as Record<string, unknown>;
197+
expect(final.class).toBe("yak-abc");
198+
expect(final.$active).toBeUndefined();
199+
expect(final.href).toBe("/x");
200+
expect(final.children).toBe("hi");
201+
expect(Object.keys(final).sort()).toEqual(["children", "class", "href", "rel", "title"]);
202+
});
203+
it("re-merging a spread copy of a merged object reads the copy (#3384)", () => {
204+
const props = merge({ a: 1, b: 2 }, { c: 3 });
205+
const copy = { ...props, b: 20, d: 4 } as Record<string, unknown>;
206+
delete copy.a;
207+
const final = merge(copy, { e: 5 }) as Record<string, unknown>;
208+
expect(final.a).toBeUndefined();
209+
expect(final.b).toBe(20);
210+
expect(final.d).toBe(4);
211+
expect(Object.getOwnPropertySymbols(final)).toEqual([]);
212+
});
213+
it("re-merging a merged object mutated afterwards reads the mutations (#3384)", () => {
214+
// @solidjs/html assigns props and a children getter onto merge()'s result
215+
// after spreading; a component's own merge(defaults, props) must see them.
216+
const props = merge({ type: "button", label: "a" }, { disabled: false }) as Record<
217+
string,
218+
unknown
219+
>;
220+
props.label = "b";
221+
props.extra = 1;
222+
Object.defineProperty(props, "children", { get: () => "kids", configurable: true });
223+
const final = merge({ type: "submit", size: "m" }, props) as Record<string, unknown>;
224+
expect(final.type).toBe("button");
225+
expect(final.label).toBe("b");
226+
expect(final.extra).toBe(1);
227+
expect(final.children).toBe("kids");
228+
expect(final.size).toBe("m");
229+
});
230+
it("still flattens merge proxies (writes are no-ops, so the sources are the truth)", () => {
231+
const [store] = createStore({ a: 1 });
232+
const b = { b: 2 };
233+
const c = { c: 3 };
234+
const inner = merge(b, store);
235+
const outer = merge(inner, c);
236+
expect(mergeSources(outer)).toEqual([b, store, c]);
237+
expect(outer.a).toBe(1);
238+
expect(outer.b).toBe(2);
239+
expect(outer.c).toBe(3);
240+
expect(Object.keys(outer).sort()).toEqual(["a", "b", "c"]);
241+
// and a plain-object result is not a source list
242+
expect(mergeSources(merge(b, c))).toBeUndefined();
243+
});
180244
it("handles undefined values", () => {
181245
const props = merge({ a: 1 }, { a: undefined });
182246
expect(props.a).toBe(undefined);

0 commit comments

Comments
 (0)