Skip to content

Commit 4afc575

Browse files
committed
only allow $props at the top level of .svelte files
1 parent 87b33a5 commit 4afc575

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

sites/svelte-5-preview/src/lib/autocomplete.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { snippetCompletion } from '@codemirror/autocomplete';
22
import { syntaxTree } from '@codemirror/language';
33

4-
/** @typedef {(node: import('@lezer/common').SyntaxNode, context: import('@codemirror/autocomplete').CompletionContext) => boolean} Test */
4+
/** @typedef {(node: import('@lezer/common').SyntaxNode, context: import('@codemirror/autocomplete').CompletionContext, selected: import('./types').File) => boolean} Test */
55

66
/**
77
* Returns `true` if `$bindable()` is valid
@@ -39,8 +39,14 @@ function is_bindable(node, context) {
3939
* TODO only allow in `.svelte` files, and only at the top level
4040
* @type {Test}
4141
*/
42-
function is_props(node) {
43-
return node.name === 'VariableName' && node.parent?.name === 'VariableDeclaration';
42+
function is_props(node, _, selected) {
43+
if (selected.type !== 'svelte') return false;
44+
45+
return (
46+
node.name === 'VariableName' &&
47+
node.parent?.name === 'VariableDeclaration' &&
48+
node.parent.parent?.name === 'Script'
49+
);
4450
}
4551

4652
/**
@@ -188,7 +194,7 @@ export function autocomplete(context, selected, files) {
188194
return {
189195
from: open.from,
190196
options: options
191-
.filter((option) => (option.test ? option.test(node, context) : true))
197+
.filter((option) => (option.test ? option.test(node, context, selected) : true))
192198
.map((option) => option.option)
193199
};
194200
}

0 commit comments

Comments
 (0)