Skip to content

Commit 2c551b2

Browse files
authored
feat: support Svelte5 of valid-prop-names-in-kit-pages rule (#963)
1 parent 4a20331 commit 2c551b2

File tree

11 files changed

+113
-13
lines changed

11 files changed

+113
-13
lines changed

.changeset/sixty-news-look.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'eslint-plugin-svelte': minor
3+
---
4+
5+
feat: support Svelte5 of `valid-prop-names-in-kit-pages` rule

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

+34-13
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,27 @@ import type { AST } from 'svelte-eslint-parser';
22
import type { TSESTree } from '@typescript-eslint/types';
33
import { createRule } from '../utils/index.js';
44
import { isKitPageComponent } from '../utils/svelte-kit.js';
5+
import type { RuleContext } from '../types.js';
56

67
const EXPECTED_PROP_NAMES = ['data', 'errors', 'form', 'snapshot'];
78

9+
function checkProp(node: TSESTree.VariableDeclarator, context: RuleContext) {
10+
if (node.id.type !== 'ObjectPattern') return;
11+
for (const p of node.id.properties) {
12+
if (
13+
p.type === 'Property' &&
14+
p.value.type === 'Identifier' &&
15+
!EXPECTED_PROP_NAMES.includes(p.value.name)
16+
) {
17+
context.report({
18+
node: p.value,
19+
loc: p.value.loc,
20+
messageId: 'unexpected'
21+
});
22+
}
23+
}
24+
}
25+
826
export default createRule('valid-prop-names-in-kit-pages', {
927
meta: {
1028
docs: {
@@ -39,6 +57,7 @@ export default createRule('valid-prop-names-in-kit-pages', {
3957
isScript = false;
4058
},
4159

60+
// Svelte3,4
4261
'ExportNamedDeclaration > VariableDeclaration > VariableDeclarator': (
4362
node: TSESTree.VariableDeclarator
4463
) => {
@@ -57,20 +76,22 @@ export default createRule('valid-prop-names-in-kit-pages', {
5776
}
5877

5978
// export let { xxx, yyy } = zzz
60-
if (node.id.type !== 'ObjectPattern') return;
61-
for (const p of node.id.properties) {
62-
if (
63-
p.type === 'Property' &&
64-
p.value.type === 'Identifier' &&
65-
!EXPECTED_PROP_NAMES.includes(p.value.name)
66-
) {
67-
context.report({
68-
node: p.value,
69-
loc: p.value.loc,
70-
messageId: 'unexpected'
71-
});
72-
}
79+
checkProp(node, context);
80+
},
81+
82+
// Svelte5
83+
// let { foo, bar } = $props();
84+
'VariableDeclaration > VariableDeclarator': (node: TSESTree.VariableDeclarator) => {
85+
if (!isScript) return;
86+
if (
87+
node.init?.type !== 'CallExpression' ||
88+
node.init.callee?.type !== 'Identifier' ||
89+
node.init.callee?.name !== '$props'
90+
) {
91+
return;
7392
}
93+
94+
checkProp(node, context);
7495
}
7596
};
7697
}
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)