Skip to content

Commit f7e8941

Browse files
committed
feat: add warning when using $bindable rune without calling it
1 parent ae7d734 commit f7e8941

File tree

6 files changed

+34
-1
lines changed

6 files changed

+34
-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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ export const validation_runes = merge(validation, a11y_validators, {
11131113
if (init?.type === 'Identifier' && init.name === '$props' && !state.scope.get('props')) {
11141114
warn(state.analysis.warnings, node, path, 'invalid-props-declaration');
11151115
}
1116+
// console.log({init, p: !state.scope.get('bindable')});
11161117
return;
11171118
}
11181119

@@ -1168,6 +1169,11 @@ export const validation_runes = merge(validation, a11y_validators, {
11681169
}
11691170
}
11701171
},
1172+
AssignmentPattern(node, { state, path }) {
1173+
if (node.right.type === 'Identifier' && node.right.name === '$bindable' && !state.scope.get('bindable')) {
1174+
warn(state.analysis.warnings, node, path, 'invalid-bindable-declaration');
1175+
}
1176+
},
11711177
// TODO this is a code smell. need to refactor this stuff
11721178
ClassBody: validation_runes_js.ClassBody,
11731179
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)