Skip to content

Commit 1510c13

Browse files
authored
feat: add warning when using $bindable runes without calling it (#11181)
* feat: add warning when using `$bindable` rune without calling it * --amend
1 parent 777527b commit 1510c13

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

.changeset/shiny-rats-heal.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+
feat: add warning when using `$bindable` rune without calling it

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,15 @@ export const validation_runes = merge(validation, a11y_validators, {
11741174
}
11751175
}
11761176
},
1177+
AssignmentPattern(node, { state, path }) {
1178+
if (
1179+
node.right.type === 'Identifier' &&
1180+
node.right.name === '$bindable' &&
1181+
!state.scope.get('bindable')
1182+
) {
1183+
warn(state.analysis.warnings, node, path, 'invalid-bindable-declaration');
1184+
}
1185+
},
11771186
// TODO this is a code smell. need to refactor this stuff
11781187
ClassBody: validation_runes_js.ClassBody,
11791188
ClassDeclaration: validation_runes_js.ClassDeclaration,

packages/svelte/src/compiler/warnings.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ const runes = {
3939
'derived-iife': () =>
4040
`Use \`$derived.by(() => {...})\` instead of \`$derived((() => {...})());\``,
4141
'invalid-props-declaration': () =>
42-
`Component properties are declared using $props() in runes mode. Did you forget to call the function?`
42+
`Component properties are declared using $props() in runes mode. Did you forget to call the function?`,
43+
'invalid-bindable-declaration': () =>
44+
`Bindable component properties are declared using $bindable() in runes mode. Did you forget to call the function?`
4345
};
4446

4547
/** @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 = $bindable } = $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-bindable-declaration",
4+
"message": "Bindable component properties are declared using $bindable() in runes mode. Did you forget to call the function?",
5+
"start": {
6+
"column": 7,
7+
"line": 2
8+
},
9+
"end": {
10+
"column": 20,
11+
"line": 2
12+
}
13+
}
14+
]

0 commit comments

Comments
 (0)