Skip to content

Commit 6625c1e

Browse files
authored
fix: warn when $props rune not called (#10655)
It's a warning because even when typing it out and knowing what to do you'll always be in a state where the validation kicks in, and it would be too distracting to always see a compiler error during that short time frame. closes #10374
1 parent 77b1f2f commit 6625c1e

File tree

6 files changed

+35
-2
lines changed

6 files changed

+35
-2
lines changed

.changeset/slow-kids-sparkle.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: warn when `$props` rune not called

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,12 @@ export const validation_runes = merge(validation, a11y_validators, {
10021002
const init = node.init;
10031003
const rune = get_rune(init, state.scope);
10041004

1005-
if (rune === null) return;
1005+
if (rune === null) {
1006+
if (init?.type === 'Identifier' && init.name === '$props' && !state.scope.get('props')) {
1007+
warn(state.analysis.warnings, node, path, 'invalid-props-declaration');
1008+
}
1009+
return;
1010+
}
10061011

10071012
const args = /** @type {import('estree').CallExpression} */ (init).arguments;
10081013

packages/svelte/src/compiler/warnings.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ const runes = {
2727
/** @param {string} name */
2828
'non-state-reference': (name) =>
2929
`${name} is updated, but is not declared with $state(...). Changing its value will not correctly trigger updates.`,
30-
'derived-iife': () => `Use \`$derived.by(() => {...})\` instead of \`$derived((() => {...})());\``
30+
'derived-iife': () =>
31+
`Use \`$derived.by(() => {...})\` instead of \`$derived((() => {...})());\``,
32+
'invalid-props-declaration': () =>
33+
`Component properties are declared using $props() in runes mode. Did you forget to call the function?`
3134
};
3235

3336
/** @satisfies {Warnings} */
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { test } from '../../test';
2+
3+
export default test({});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<script>
2+
let { a } = $props;
3+
</script>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[
2+
{
3+
"code": "invalid-props-declaration",
4+
"message": "Component properties are declared using $props() in runes mode. Did you forget to call the function?",
5+
"start": {
6+
"column": 5,
7+
"line": 2
8+
},
9+
"end": {
10+
"column": 19,
11+
"line": 2
12+
}
13+
}
14+
]

0 commit comments

Comments
 (0)