|
| 1 | +import { createRule } from '../utils/index.js'; |
| 2 | +import { type TSTools, getTypeScriptTools } from 'src/utils/ts-utils/index.js'; |
| 3 | +import { |
| 4 | + type MethodSignature, |
| 5 | + type Symbol, |
| 6 | + SymbolFlags, |
| 7 | + SyntaxKind, |
| 8 | + type Type, |
| 9 | + type TypeReferenceNode |
| 10 | +} from 'typescript'; |
| 11 | +import type { CallExpression } from 'estree'; |
| 12 | + |
| 13 | +export default createRule('require-event-prefix', { |
| 14 | + meta: { |
| 15 | + docs: { |
| 16 | + description: 'require component event names to start with "on"', |
| 17 | + category: 'Stylistic Issues', |
| 18 | + conflictWithPrettier: false, |
| 19 | + recommended: false |
| 20 | + }, |
| 21 | + schema: [ |
| 22 | + { |
| 23 | + type: 'object', |
| 24 | + properties: { |
| 25 | + checkAsyncFunctions: { |
| 26 | + type: 'boolean' |
| 27 | + } |
| 28 | + }, |
| 29 | + additionalProperties: false |
| 30 | + } |
| 31 | + ], |
| 32 | + messages: { |
| 33 | + nonPrefixedFunction: 'Component event name must start with "on".' |
| 34 | + }, |
| 35 | + type: 'suggestion', |
| 36 | + conditions: [ |
| 37 | + { |
| 38 | + svelteVersions: ['5'], |
| 39 | + svelteFileTypes: ['.svelte'] |
| 40 | + } |
| 41 | + ] |
| 42 | + }, |
| 43 | + create(context) { |
| 44 | + const tsTools = getTypeScriptTools(context); |
| 45 | + if (!tsTools) { |
| 46 | + return {}; |
| 47 | + } |
| 48 | + |
| 49 | + const checkAsyncFunctions = context.options[0]?.checkAsyncFunctions ?? false; |
| 50 | + |
| 51 | + return { |
| 52 | + CallExpression(node) { |
| 53 | + const propsType = getPropsType(node, tsTools); |
| 54 | + if (propsType === undefined) { |
| 55 | + return; |
| 56 | + } |
| 57 | + for (const property of propsType.getProperties()) { |
| 58 | + if (property.getFlags() & SymbolFlags.Method && !property.getName().startsWith('on')) { |
| 59 | + if (!checkAsyncFunctions && isFunctionAsync(property)) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + context.report({ |
| 63 | + node, |
| 64 | + messageId: 'nonPrefixedFunction' |
| 65 | + }); |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + }; |
| 70 | + } |
| 71 | +}); |
| 72 | + |
| 73 | +function getPropsType(node: CallExpression, tsTools: TSTools): Type | undefined { |
| 74 | + if ( |
| 75 | + node.callee.type !== 'Identifier' || |
| 76 | + node.callee.name !== '$props' || |
| 77 | + node.parent.type !== 'VariableDeclarator' |
| 78 | + ) { |
| 79 | + return undefined; |
| 80 | + } |
| 81 | + |
| 82 | + const tsNode = tsTools.service.esTreeNodeToTSNodeMap.get(node.parent.id); |
| 83 | + if (tsNode === undefined) { |
| 84 | + return undefined; |
| 85 | + } |
| 86 | + |
| 87 | + const checker = tsTools.service.program.getTypeChecker(); |
| 88 | + return checker.getTypeAtLocation(tsNode); |
| 89 | +} |
| 90 | + |
| 91 | +function isFunctionAsync(functionSymbol: Symbol): boolean { |
| 92 | + return ( |
| 93 | + functionSymbol.getDeclarations()?.some((declaration) => { |
| 94 | + if (declaration.kind !== SyntaxKind.MethodSignature) { |
| 95 | + return false; |
| 96 | + } |
| 97 | + const declarationType = (declaration as MethodSignature).type; |
| 98 | + if (declarationType?.kind !== SyntaxKind.TypeReference) { |
| 99 | + return false; |
| 100 | + } |
| 101 | + const declarationTypeName = (declarationType as TypeReferenceNode).typeName; |
| 102 | + return ( |
| 103 | + declarationTypeName.kind === SyntaxKind.Identifier && |
| 104 | + declarationTypeName.escapedText === 'Promise' |
| 105 | + ); |
| 106 | + }) ?? false |
| 107 | + ); |
| 108 | +} |
0 commit comments