Skip to content

Commit b00c09e

Browse files
committed
feat: support Svelte5 of valid-prop-names-in-kit-pages
1 parent ad4e538 commit b00c09e

File tree

10 files changed

+103
-0
lines changed

10 files changed

+103
-0
lines changed

packages/eslint-plugin-svelte/src/rules/valid-prop-names-in-kit-pages.ts

+29
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export default createRule('valid-prop-names-in-kit-pages', {
3939
isScript = false;
4040
},
4141

42+
// Svelte3,4
4243
'ExportNamedDeclaration > VariableDeclaration > VariableDeclarator': (
4344
node: TSESTree.VariableDeclarator
4445
) => {
@@ -57,6 +58,34 @@ export default createRule('valid-prop-names-in-kit-pages', {
5758
}
5859

5960
// export let { xxx, yyy } = zzz
61+
if (node.id.type !== 'ObjectPattern') return;
62+
for (const p of node.id.properties) {
63+
if (
64+
p.type === 'Property' &&
65+
p.value.type === 'Identifier' &&
66+
!EXPECTED_PROP_NAMES.includes(p.value.name)
67+
) {
68+
context.report({
69+
node: p.value,
70+
loc: p.value.loc,
71+
messageId: 'unexpected'
72+
});
73+
}
74+
}
75+
},
76+
77+
// Svelte5
78+
// let { foo, bar } = $props();
79+
'VariableDeclaration > VariableDeclarator': (node: TSESTree.VariableDeclarator) => {
80+
if (!isScript) return;
81+
if (
82+
node.init?.type !== 'CallExpression' ||
83+
node.init.callee?.type !== 'Identifier' ||
84+
node.init.callee?.name !== '$props'
85+
) {
86+
return;
87+
}
88+
6089
if (node.id.type !== 'ObjectPattern') return;
6190
for (const p of node.id.properties) {
6291
if (
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
- message: disallow props other than data or errors in SvelteKit page components.
2+
line: 2
3+
column: 8
4+
suggestions: null
5+
- message: disallow props other than data or errors in SvelteKit page components.
6+
line: 2
7+
column: 13
8+
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let { foo, bar } = $props();
3+
</script>
4+
5+
{foo}, {bar}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.0.0-0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"languageOptions": {
3+
"parserOptions": {
4+
"svelteConfig": {
5+
"kit": {
6+
"files": {
7+
"routes": "tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5"
8+
}
9+
}
10+
}
11+
}
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<script>
2+
let { data, errors, foo, bar } = $props();
3+
</script>
4+
5+
{data}, {errors}, {foo}, {bar}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.0.0-0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<script>
2+
let { data, errors, form } = $props();
3+
4+
let comment = '';
5+
6+
export const snapshot = {
7+
capture: () => comment,
8+
restore: (value) => (comment = value)
9+
};
10+
</script>
11+
12+
{data}, {errors}
13+
14+
{#if form?.success}
15+
<p>Successfully logged in! Welcome back, {data.user.name}</p>
16+
{/if}
17+
18+
<form method="POST">
19+
<textarea bind:value={comment} />
20+
<button>Post comment</button>
21+
</form>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"svelte": ">=5.0.0-0"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"languageOptions": {
3+
"parserOptions": {
4+
"svelteConfig": {
5+
"kit": {
6+
"files": {
7+
"routes": "tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5"
8+
}
9+
}
10+
}
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)