|
1 | 1 | import { createRule } from '../utils/index.js';
|
2 |
| -import { type TSTools, getTypeScriptTools, isMethodSymbol } from '../utils/ts-utils/index.js'; |
3 | 2 | import {
|
4 |
| - type MethodSignature, |
5 |
| - type Symbol, |
6 |
| - SyntaxKind, |
7 |
| - type Type, |
8 |
| - type TypeReferenceNode, |
9 |
| - type PropertySignature |
10 |
| -} from 'typescript'; |
| 3 | + type TSTools, |
| 4 | + getTypeScriptTools, |
| 5 | + isMethodSymbol, |
| 6 | + isPropertySignatureKind, |
| 7 | + isFunctionTypeKind, |
| 8 | + isMethodSignatureKind, |
| 9 | + isTypeReferenceKind, |
| 10 | + isIdentifierKind |
| 11 | +} from '../utils/ts-utils/index.js'; |
| 12 | +import type { Symbol, Type } from 'typescript'; |
11 | 13 | import type { CallExpression } from 'estree';
|
12 | 14 |
|
13 | 15 | export default createRule('require-event-prefix', {
|
@@ -58,7 +60,7 @@ export default createRule('require-event-prefix', {
|
58 | 60 | if (
|
59 | 61 | isFunctionLike(property, tsTools) &&
|
60 | 62 | !property.getName().startsWith('on') &&
|
61 |
| - (checkAsyncFunctions || !isFunctionAsync(property)) |
| 63 | + (checkAsyncFunctions || !isFunctionAsync(property, tsTools)) |
62 | 64 | ) {
|
63 | 65 | const declarationTsNode = property.getDeclarations()?.[0];
|
64 | 66 | const declarationEstreeNode =
|
@@ -96,25 +98,25 @@ function getPropsType(node: CallExpression, tsTools: TSTools): Type | undefined
|
96 | 98 | function isFunctionLike(functionSymbol: Symbol, tsTools: TSTools): boolean {
|
97 | 99 | return (
|
98 | 100 | isMethodSymbol(functionSymbol, tsTools.ts) ||
|
99 |
| - (functionSymbol.valueDeclaration?.kind === SyntaxKind.PropertySignature && |
100 |
| - (functionSymbol.valueDeclaration as PropertySignature).type?.kind === SyntaxKind.FunctionType) |
| 101 | + (functionSymbol.valueDeclaration !== undefined && |
| 102 | + isPropertySignatureKind(functionSymbol.valueDeclaration, tsTools.ts) && |
| 103 | + functionSymbol.valueDeclaration.type !== undefined && |
| 104 | + isFunctionTypeKind(functionSymbol.valueDeclaration.type, tsTools.ts)) |
101 | 105 | );
|
102 | 106 | }
|
103 | 107 |
|
104 |
| -function isFunctionAsync(functionSymbol: Symbol): boolean { |
| 108 | +function isFunctionAsync(functionSymbol: Symbol, tsTools: TSTools): boolean { |
105 | 109 | return (
|
106 | 110 | functionSymbol.getDeclarations()?.some((declaration) => {
|
107 |
| - if (declaration.kind !== SyntaxKind.MethodSignature) { |
| 111 | + if (!isMethodSignatureKind(declaration, tsTools.ts)) { |
108 | 112 | return false;
|
109 | 113 | }
|
110 |
| - const declarationType = (declaration as MethodSignature).type; |
111 |
| - if (declarationType?.kind !== SyntaxKind.TypeReference) { |
| 114 | + if (declaration.type === undefined || !isTypeReferenceKind(declaration.type, tsTools.ts)) { |
112 | 115 | return false;
|
113 | 116 | }
|
114 |
| - const declarationTypeName = (declarationType as TypeReferenceNode).typeName; |
115 | 117 | return (
|
116 |
| - declarationTypeName.kind === SyntaxKind.Identifier && |
117 |
| - declarationTypeName.escapedText === 'Promise' |
| 118 | + isIdentifierKind(declaration.type.typeName, tsTools.ts) && |
| 119 | + declaration.type.typeName.escapedText === 'Promise' |
118 | 120 | );
|
119 | 121 | }) ?? false
|
120 | 122 | );
|
|
0 commit comments