diff --git a/.changeset/sixty-news-look.md b/.changeset/sixty-news-look.md new file mode 100644 index 000000000..e70b9cd96 --- /dev/null +++ b/.changeset/sixty-news-look.md @@ -0,0 +1,5 @@ +--- +'eslint-plugin-svelte': minor +--- + +feat: support Svelte5 of `valid-prop-names-in-kit-pages` rule diff --git a/packages/eslint-plugin-svelte/src/rules/valid-prop-names-in-kit-pages.ts b/packages/eslint-plugin-svelte/src/rules/valid-prop-names-in-kit-pages.ts index 77b3e98da..ff12df72a 100644 --- a/packages/eslint-plugin-svelte/src/rules/valid-prop-names-in-kit-pages.ts +++ b/packages/eslint-plugin-svelte/src/rules/valid-prop-names-in-kit-pages.ts @@ -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: { @@ -39,6 +57,7 @@ export default createRule('valid-prop-names-in-kit-pages', { isScript = false; }, + // Svelte3,4 'ExportNamedDeclaration > VariableDeclaration > VariableDeclarator': ( node: TSESTree.VariableDeclarator ) => { @@ -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); } }; } diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-errors.yaml b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-errors.yaml new file mode 100644 index 000000000..a5dcdf655 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-errors.yaml @@ -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 diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-input.svelte b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-input.svelte new file mode 100644 index 000000000..33f7c90fd --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-input.svelte @@ -0,0 +1,5 @@ + + +{foo}, {bar} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-requirements.json new file mode 100644 index 000000000..0192b1098 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/+svelte5-test001-requirements.json @@ -0,0 +1,3 @@ +{ + "svelte": ">=5.0.0-0" +} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/_config.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/_config.json new file mode 100644 index 000000000..7a71ebe76 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5/_config.json @@ -0,0 +1,13 @@ +{ + "languageOptions": { + "parserOptions": { + "svelteConfig": { + "kit": { + "files": { + "routes": "tests/fixtures/rules/valid-prop-names-in-kit-pages/invalid/svelte5" + } + } + } + } + } +} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/not-page/svelte5/test01-input.svelte b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/not-page/svelte5/test01-input.svelte new file mode 100644 index 000000000..b042de2c4 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/not-page/svelte5/test01-input.svelte @@ -0,0 +1,5 @@ + + +{data}, {errors}, {foo}, {bar} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/not-page/svelte5/test01-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/not-page/svelte5/test01-requirements.json new file mode 100644 index 000000000..0192b1098 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/not-page/svelte5/test01-requirements.json @@ -0,0 +1,3 @@ +{ + "svelte": ">=5.0.0-0" +} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/+test001-input.svelte b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/+test001-input.svelte new file mode 100644 index 000000000..6ed2875b6 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/+test001-input.svelte @@ -0,0 +1,21 @@ + + +{data}, {errors} + +{#if form?.success} +
Successfully logged in! Welcome back, {data.user.name}
+{/if} + + diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/+test001-requirements.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/+test001-requirements.json new file mode 100644 index 000000000..0192b1098 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/+test001-requirements.json @@ -0,0 +1,3 @@ +{ + "svelte": ">=5.0.0-0" +} diff --git a/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/_config.json b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/_config.json new file mode 100644 index 000000000..cd38d8ea8 --- /dev/null +++ b/packages/eslint-plugin-svelte/tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5/_config.json @@ -0,0 +1,13 @@ +{ + "languageOptions": { + "parserOptions": { + "svelteConfig": { + "kit": { + "files": { + "routes": "tests/fixtures/rules/valid-prop-names-in-kit-pages/valid/svelte5" + } + } + } + } + } +}