Skip to content

Commit

Permalink
feat: support Svelte5 of valid-prop-names-in-kit-pages rule (#963)
Browse files Browse the repository at this point in the history
  • Loading branch information
baseballyama authored Dec 29, 2024
1 parent 4a20331 commit 2c551b2
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 13 deletions.
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"
}
}
}
}
}
}

0 comments on commit 2c551b2

Please sign in to comment.