|
| 1 | +import { createRule } from '../utils/index.js'; |
| 2 | +import { getTypeScriptTools } from '../utils/ts-utils/index.js'; |
| 3 | +import type { TSESTree } from '@typescript-eslint/types'; |
| 4 | +import type ts from 'typescript'; |
| 5 | +import { findVariable } from '../utils/ast-utils.js'; |
| 6 | + |
| 7 | +const unknown = Symbol('unknown'); |
| 8 | + |
| 9 | +export default createRule('no-unused-props', { |
| 10 | + meta: { |
| 11 | + docs: { |
| 12 | + description: 'Warns about defined Props properties that are unused', |
| 13 | + category: 'Best Practices', |
| 14 | + recommended: true |
| 15 | + }, |
| 16 | + schema: [], |
| 17 | + messages: { |
| 18 | + unusedProp: "'{{name}}' is an unused Props property." |
| 19 | + }, |
| 20 | + type: 'suggestion', |
| 21 | + conditions: [ |
| 22 | + { |
| 23 | + svelteVersions: ['5'], |
| 24 | + runes: [true, 'undetermined'] |
| 25 | + } |
| 26 | + ] |
| 27 | + }, |
| 28 | + create(context) { |
| 29 | + const tools = getTypeScriptTools(context); |
| 30 | + if (!tools) { |
| 31 | + return {}; |
| 32 | + } |
| 33 | + |
| 34 | + const typeChecker = tools.service.program.getTypeChecker(); |
| 35 | + if (!typeChecker) { |
| 36 | + return {}; |
| 37 | + } |
| 38 | + |
| 39 | + function getUsedPropertyNames(node: TSESTree.Identifier): (string | typeof unknown)[] { |
| 40 | + const variable = findVariable(context, node); |
| 41 | + if (!variable) { |
| 42 | + return [unknown]; |
| 43 | + } |
| 44 | + |
| 45 | + const usedProps = new Set<string | typeof unknown>(); |
| 46 | + |
| 47 | + for (const reference of variable.references) { |
| 48 | + const parent = reference.identifier.parent; |
| 49 | + if (!parent) continue; |
| 50 | + |
| 51 | + if (parent.type === 'MemberExpression' && parent.object === reference.identifier) { |
| 52 | + if (parent.property.type === 'Identifier' && !parent.computed) { |
| 53 | + usedProps.add(parent.property.name); |
| 54 | + } else { |
| 55 | + usedProps.add(unknown); |
| 56 | + } |
| 57 | + } else if (parent.type === 'CallExpression' || parent.type === 'NewExpression') { |
| 58 | + if ( |
| 59 | + 'arguments' in parent && |
| 60 | + Array.isArray(parent.arguments) && |
| 61 | + parent.arguments.some((arg): arg is TSESTree.Identifier => arg === reference.identifier) |
| 62 | + ) { |
| 63 | + usedProps.add(unknown); |
| 64 | + } |
| 65 | + } else if (parent.type === 'AssignmentExpression' || parent.type === 'AssignmentPattern') { |
| 66 | + usedProps.add(unknown); |
| 67 | + } else if (parent.type === 'SpreadElement') { |
| 68 | + usedProps.add(unknown); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return Array.from(usedProps); |
| 73 | + } |
| 74 | + |
| 75 | + return { |
| 76 | + 'VariableDeclaration > VariableDeclarator': (node: TSESTree.VariableDeclarator) => { |
| 77 | + if ( |
| 78 | + node.init?.type !== 'CallExpression' || |
| 79 | + node.init.callee.type !== 'Identifier' || |
| 80 | + node.init.callee.name !== '$props' |
| 81 | + ) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const tsNode = tools.service.esTreeNodeToTSNodeMap.get(node) as ts.VariableDeclaration; |
| 86 | + if (!tsNode || !tsNode.type) return; |
| 87 | + const checker = tools.service.program.getTypeChecker(); |
| 88 | + const propType = checker.getTypeFromTypeNode(tsNode.type); |
| 89 | + const properties = checker.getPropertiesOfType(propType); |
| 90 | + const propNames = properties.map((p) => p.getName()); |
| 91 | + |
| 92 | + const usedNames: (string | typeof unknown)[] = []; |
| 93 | + if (node.id.type === 'ObjectPattern') { |
| 94 | + for (const prop of node.id.properties) { |
| 95 | + if (prop.type === 'Property' && prop.key.type === 'Identifier') { |
| 96 | + usedNames.push(prop.key.name); |
| 97 | + } else if (prop.type === 'RestElement' && prop.argument.type === 'Identifier') { |
| 98 | + usedNames.push(...getUsedPropertyNames(prop.argument)); |
| 99 | + } |
| 100 | + } |
| 101 | + } else if (node.id.type === 'Identifier' && node.id.typeAnnotation) { |
| 102 | + usedNames.push(...getUsedPropertyNames(node.id)); |
| 103 | + } |
| 104 | + |
| 105 | + if (usedNames.includes(unknown)) { |
| 106 | + return; |
| 107 | + } |
| 108 | + for (const propName of propNames) { |
| 109 | + if (!usedNames.includes(propName)) { |
| 110 | + context.report({ |
| 111 | + node: node.id, |
| 112 | + messageId: 'unusedProp', |
| 113 | + data: { |
| 114 | + name: propName |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
| 121 | + } |
| 122 | +}); |
0 commit comments