Skip to content

Commit 8ad1b98

Browse files
feat: include rest props object name in error message (#10868)
* tweak error message for readonly rest props * remove property in prod * Update packages/svelte/src/internal/client/reactivity/props.js --------- Co-authored-by: Simon H <[email protected]>
1 parent adc3f7a commit 8ad1b98

File tree

2 files changed

+19
-18
lines changed

2 files changed

+19
-18
lines changed

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -220,16 +220,17 @@ export const javascript_visitors_runes = {
220220
}
221221
} else {
222222
// RestElement
223-
declarations.push(
224-
b.declarator(
225-
property.argument,
226-
b.call(
227-
'$.rest_props',
228-
b.id('$$props'),
229-
b.array(seen.map((name) => b.literal(name)))
230-
)
231-
)
232-
);
223+
/** @type {import('estree').Expression[]} */
224+
const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
225+
226+
if (state.options.dev) {
227+
// include rest name, so we can provide informative error messages
228+
args.push(
229+
b.literal(/** @type {import('estree').Identifier} */ (property.argument).name)
230+
);
231+
}
232+
233+
declarations.push(b.declarator(property.argument, b.call('$.rest_props', ...args)));
233234
}
234235
}
235236

packages/svelte/src/internal/client/reactivity/props.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,18 @@ export function update_pre_prop(fn, d = 1) {
3636
/**
3737
* The proxy handler for rest props (i.e. `const { x, ...rest } = $props()`).
3838
* Is passed the full `$$props` object and excludes the named props.
39-
* @type {ProxyHandler<{ props: Record<string | symbol, unknown>, exclude: Array<string | symbol> }>}}
39+
* @type {ProxyHandler<{ props: Record<string | symbol, unknown>, exclude: Array<string | symbol>, name?: string }>}}
4040
*/
4141
const rest_props_handler = {
4242
get(target, key) {
4343
if (target.exclude.includes(key)) return;
4444
return target.props[key];
4545
},
46-
set(_, key) {
46+
set(target, key) {
4747
if (DEV) {
48-
throw new Error(
49-
`Cannot write to property '${String(key)}' of rest element of $props(). It is always readonly.`
50-
);
48+
throw new Error(`Rest element properties of $props() such as ${target.name}.${String(key)} are readonly`);
5149
}
50+
5251
return false;
5352
},
5453
getOwnPropertyDescriptor(target, key) {
@@ -72,11 +71,12 @@ const rest_props_handler = {
7271

7372
/**
7473
* @param {Record<string, unknown>} props
75-
* @param {string[]} rest
74+
* @param {string[]} exclude
75+
* @param {string} [name]
7676
* @returns {Record<string, unknown>}
7777
*/
78-
export function rest_props(props, rest) {
79-
return new Proxy({ props, exclude: rest }, rest_props_handler);
78+
export function rest_props(props, exclude, name) {
79+
return new Proxy(DEV ? { props, exclude, name } : { props, exclude }, rest_props_handler);
8080
}
8181

8282
/**

0 commit comments

Comments
 (0)