Skip to content

Commit 92f2c8b

Browse files
committed
code-golf, docs
1 parent 517f7f0 commit 92f2c8b

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

packages/svelte/src/internal/client/render.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
get_descriptor,
5252
get_descriptors,
5353
is_array,
54+
is_function,
5455
object_assign,
5556
object_keys
5657
} from './utils.js';
@@ -2524,6 +2525,8 @@ export function spread_dynamic_element_attributes(node, prev, attrs, css_hash) {
25242525
}
25252526

25262527
/**
2528+
* The proxy handler for rest props (i.e. `const { x, ...rest } = $props()`).
2529+
* Is passed the full `$$props` object and excludes the named props.
25272530
* @type {ProxyHandler<{ props: Record<string | symbol, unknown>, exclude: Array<string | symbol> }>}}
25282531
*/
25292532
const rest_props_handler = {
@@ -2567,6 +2570,9 @@ export function rest_props(props, rest) {
25672570
}
25682571

25692572
/**
2573+
* The proxy handler for spread props. Handles the incoming array of props
2574+
* that looks like `() => { dynamic: props }, { static: prop }, ..` and wraps
2575+
* them so that the whole thing is passed to the component as the `$$props` argument.
25702576
* @template {Record<string | symbol, unknown>} T
25712577
* @type {ProxyHandler<{ props: Array<T | (() => T)> }>}}
25722578
*/
@@ -2575,7 +2581,7 @@ const spread_props_handler = {
25752581
let i = target.props.length;
25762582
while (i--) {
25772583
let p = target.props[i];
2578-
if (typeof p === 'function') p = p();
2584+
if (is_function(p)) p = p();
25792585
if (typeof p === 'object' && p !== null && key in p) return p[key];
25802586
}
25812587
},
@@ -2584,7 +2590,7 @@ const spread_props_handler = {
25842590
},
25852591
has(target, key) {
25862592
for (let p of target.props) {
2587-
if (typeof p === 'function') p = p();
2593+
if (is_function(p)) p = p();
25882594
if (key in p) return true;
25892595
}
25902596

@@ -2595,7 +2601,7 @@ const spread_props_handler = {
25952601
const keys = [];
25962602

25972603
for (let p of target.props) {
2598-
if (typeof p === 'function') p = p();
2604+
if (is_function(p)) p = p();
25992605
for (const key in p) {
26002606
if (!keys.includes(key)) keys.push(key);
26012607
}

packages/svelte/src/internal/client/utils.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ export var object_assign = Object.assign;
88
export var define_property = Object.defineProperty;
99
export var get_descriptor = Object.getOwnPropertyDescriptor;
1010
export var get_descriptors = Object.getOwnPropertyDescriptors;
11+
12+
/**
13+
* @param {any} thing
14+
* @returns {thing is Function}
15+
*/
16+
export function is_function(thing) {
17+
return typeof thing === 'function';
18+
}

0 commit comments

Comments
 (0)