import { createRule } from '../utils/index.js'; import { getSvelteContext, getSvelteVersion } from '../utils/svelte-context.js'; const PAGE_PROP_NAMES = ['data', 'form', 'params', 'snapshot']; const LEGACY_PAGE_PROP_NAMES = [...PAGE_PROP_NAMES, 'errors']; const LAYOUT_PROP_NAMES = [...PAGE_PROP_NAMES, 'children']; const ERROR_PROP_NAMES = ['error']; function checkProp(node, context, expectedPropNames) { if (node.id.type !== 'ObjectPattern') return; for (const p of node.id.properties) { if (p.type === 'Property' && p.key.type === 'Identifier' && !expectedPropNames.includes(p.key.name)) { context.report({ node: p.key, loc: p.key.loc, messageId: 'unexpected' }); } } } function isModuleScript(node) { // 'Program > SvelteScriptElement:exit': () => { isScript = false; }, // Svelte3,4 'ExportNamedDeclaration > VariableDeclaration > VariableDeclarator': (node) => { if (!isScript) return; // export let foo if (node.id.type === 'Identifier') { if (!LEGACY_PAGE_PROP_NAMES.includes(node.id.name)) { context.report({ node, loc: node.loc, messageId: 'unexpected' }); } return; } // export let { xxx, yyy } = zzz checkProp(node, context, LEGACY_PAGE_PROP_NAMES); }, // Svelte5 // let { foo, bar } = $props(); 'VariableDeclaration > VariableDeclarator': (node) => { if (!isScript) return; if (node.init?.type !== 'CallExpression' || node.init.callee?.type !== 'Identifier' || node.init.callee?.name !== '$props') { return; } checkProp(node, context, expectedPropNames); } }; } });