Skip to content

Commit c42bb04

Browse files
authored
fix: disallow accessing internal Svelte props (#12207)
closes #12184
1 parent 33e44ea commit c42bb04

File tree

8 files changed

+55
-0
lines changed

8 files changed

+55
-0
lines changed

.changeset/olive-cobras-wonder.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': patch
3+
---
4+
5+
fix: disallow accessing internal Svelte props

packages/svelte/messages/compile-errors/script.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@
7878

7979
> Cannot use `$props()` more than once
8080
81+
## props_illegal_name
82+
83+
> Declaring or accessing a prop starting with `$$` is illegal (they are reserved for Svelte internals)
84+
8185
## props_invalid_identifier
8286

8387
> `$props()` can only be used with an object destructuring pattern

packages/svelte/src/compiler/errors.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,15 @@ export function props_duplicate(node) {
276276
e(node, "props_duplicate", "Cannot use `$props()` more than once");
277277
}
278278

279+
/**
280+
* Declaring or accessing a prop starting with `$$` is illegal (they are reserved for Svelte internals)
281+
* @param {null | number | NodeLike} node
282+
* @returns {never}
283+
*/
284+
export function props_illegal_name(node) {
285+
e(node, "props_illegal_name", "Declaring or accessing a prop starting with `$$` is illegal (they are reserved for Svelte internals)");
286+
}
287+
279288
/**
280289
* `$props()` can only be used with an object destructuring pattern
281290
* @param {null | number | NodeLike} node

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,14 @@ function validate_block_not_empty(node, context) {
341341
* @type {import('zimmerframe').Visitors<import('#compiler').SvelteNode, import('./types.js').AnalysisState>}
342342
*/
343343
const validation = {
344+
MemberExpression(node, context) {
345+
if (node.object.type === 'Identifier' && node.property.type === 'Identifier') {
346+
const binding = context.state.scope.get(node.object.name);
347+
if (binding?.kind === 'rest_prop' && node.property.name.startsWith('$$')) {
348+
e.props_illegal_name(node.property);
349+
}
350+
}
351+
},
344352
AssignmentExpression(node, context) {
345353
validate_assignment(node, node.left, context.state);
346354
},
@@ -1255,6 +1263,10 @@ export const validation_runes = merge(validation, a11y_validators, {
12551263
e.props_invalid_pattern(property);
12561264
}
12571265

1266+
if (property.key.type === 'Identifier' && property.key.name.startsWith('$$')) {
1267+
e.props_illegal_name(property);
1268+
}
1269+
12581270
const value =
12591271
property.value.type === 'AssignmentPattern' ? property.value.left : property.value;
12601272

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
error: {
5+
code: 'props_illegal_name',
6+
message:
7+
'Declaring or accessing a prop starting with `$$` is illegal (they are reserved for Svelte internals)'
8+
}
9+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
let { $$slots: a } = $props();
3+
</script>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test } from '../../test';
2+
3+
export default test({
4+
error: {
5+
code: 'props_illegal_name',
6+
message:
7+
'Declaring or accessing a prop starting with `$$` is illegal (they are reserved for Svelte internals)'
8+
}
9+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script>
2+
let props = $props();
3+
props.$$slots;
4+
</script>

0 commit comments

Comments
 (0)