Skip to content

Commit

Permalink
fix: spreading non-object props
Browse files Browse the repository at this point in the history
- `createComponent` passes an empty object instead of non-object props which are spread
- `mergeProps` treats non-object sources as empty objects
- added tests

fixes solidjs#958
  • Loading branch information
otonashixav committed May 3, 2022
1 parent 01d21ca commit 3af8568
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
7 changes: 4 additions & 3 deletions packages/solid/src/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,18 @@ export type ComponentProps<T extends keyof JSX.IntrinsicElements | Component<any
? JSX.IntrinsicElements[T]
: {};
export function createComponent<T>(Comp: (props: T) => JSX.Element, props: T): JSX.Element {
if (props == null || typeof props !== "object") props = {} as T;
if (hydrationEnabled) {
if (sharedConfig.context) {
const c = sharedConfig.context;
setHydrateContext(nextHydrateContext());
const r = "_SOLID_DEV_" ? devComponent(Comp, props) : untrack(() => Comp(props as T));
const r = "_SOLID_DEV_" ? devComponent(Comp, props) : untrack(() => Comp(props));
setHydrateContext(c);
return r;
}
}
if ("_SOLID_DEV_") return devComponent(Comp, props);
return untrack(() => Comp(props as T));
return untrack(() => Comp(props));
}

function trueFn() {
Expand Down Expand Up @@ -87,7 +88,7 @@ type UnboxIntersection<T> = T extends { 0: infer U } ? U : never;
type MergeProps<T extends any[]> = UnboxIntersection<UnionToIntersection<BoxedTupleTypes<T>>>;

function resolveSource(s: any) {
return typeof s === "function" ? s() : s;
return (s = typeof s === "function" ? s() : s) == null || typeof s !== "object" ? {} : s;
}

export function mergeProps<T extends any[]>(...sources: T): MergeProps<T>;
Expand Down
14 changes: 14 additions & 0 deletions packages/solid/test/component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ describe("CreateComponent", () => {
expect(out).toBe("Hi dynamic");
});
});
test("non-object props are replaced with empty props", () => {
createRoot(() => {
const nonObjects = [null, undefined, 1, "string", () => "function"];
nonObjects.forEach(nonObject => {
const out = createComponent(p => p, nonObject);
expect(out).toEqual({});
});
});
});
});

describe("Set Default Props", () => {
Expand Down Expand Up @@ -117,6 +126,11 @@ describe("Merge Signal", () => {
expect(res[1]).toBe("h");
expect(res.length).toBe(2);
});

test("non-objects are ignored", () => {
const props = mergeProps({ a: 1 }, 1, null, undefined, "string", () => 1);
expect(props).toEqual({ a: 1 });
});
});

describe("SplitProps Props", () => {
Expand Down

0 comments on commit 3af8568

Please sign in to comment.