Skip to content

feat: support Svelte5 of valid-prop-names-in-kit-pages rule #963

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/sixty-news-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

feat: support Svelte5 of `valid-prop-names-in-kit-pages` rule
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,27 @@ import type { AST } from 'svelte-eslint-parser';
import type { TSESTree } from '@typescript-eslint/types';
import { createRule } from '../utils/index.js';
import { isKitPageComponent } from '../utils/svelte-kit.js';
import type { RuleContext } from '../types.js';

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

function checkProp(node: TSESTree.VariableDeclarator, context: RuleContext) {
if (node.id.type !== 'ObjectPattern') return;
for (const p of node.id.properties) {
if (
p.type === 'Property' &&
p.value.type === 'Identifier' &&
!EXPECTED_PROP_NAMES.includes(p.value.name)
) {
context.report({
node: p.value,
loc: p.value.loc,
messageId: 'unexpected'
});
}
}
}

export default createRule('valid-prop-names-in-kit-pages', {
meta: {
docs: {
Expand Down Expand Up @@ -39,6 +57,7 @@ export default createRule('valid-prop-names-in-kit-pages', {
isScript = false;
},

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

// export let { xxx, yyy } = zzz
if (node.id.type !== 'ObjectPattern') return;
for (const p of node.id.properties) {
if (
p.type === 'Property' &&
p.value.type === 'Identifier' &&
!EXPECTED_PROP_NAMES.includes(p.value.name)
) {
context.report({
node: p.value,
loc: p.value.loc,
messageId: 'unexpected'
});
}
checkProp(node, context);
},

// Svelte5
// let { foo, bar } = $props();
'VariableDeclaration > VariableDeclarator': (node: TSESTree.VariableDeclarator) => {
if (!isScript) return;
if (
node.init?.type !== 'CallExpression' ||
node.init.callee?.type !== 'Identifier' ||
node.init.callee?.name !== '$props'
) {
return;
}

checkProp(node, context);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
- message: disallow props other than data or errors in SvelteKit page components.
line: 2
column: 8
suggestions: null
- message: disallow props other than data or errors in SvelteKit page components.
line: 2
column: 13
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { foo, bar } = $props();
</script>

{foo}, {bar}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.0.0-0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"languageOptions": {
"parserOptions": {
"svelteConfig": {
"kit": {
"files": {
"routes": "tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5"
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<script>
let { data, errors, foo, bar } = $props();
</script>

{data}, {errors}, {foo}, {bar}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.0.0-0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
let { data, errors, form } = $props();

let comment = '';

export const snapshot = {
capture: () => comment,
restore: (value) => (comment = value)
};
</script>

{data}, {errors}

{#if form?.success}
<p>Successfully logged in! Welcome back, {data.user.name}</p>
{/if}

<form method="POST">
<textarea bind:value={comment} />
<button>Post comment</button>
</form>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"svelte": ">=5.0.0-0"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"languageOptions": {
"parserOptions": {
"svelteConfig": {
"kit": {
"files": {
"routes": "tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5"
}
}
}
}
}
}
Loading