|
1 | 1 | import { createRule } from '../utils/index.js';
|
2 |
| -import { type TSTools, getTypeScriptTools, isMethodType } 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 | + isMethodType, |
| 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 =
|
@@ -99,25 +101,25 @@ function isFunctionLike(functionSymbol: Symbol, tsTools: TSTools): boolean {
|
99 | 101 | tsTools.service.program.getTypeChecker().getTypeOfSymbol(functionSymbol),
|
100 | 102 | tsTools.ts
|
101 | 103 | ) ||
|
102 |
| - (functionSymbol.valueDeclaration?.kind === SyntaxKind.PropertySignature && |
103 |
| - (functionSymbol.valueDeclaration as PropertySignature).type?.kind === SyntaxKind.FunctionType) |
| 104 | + (functionSymbol.valueDeclaration !== undefined && |
| 105 | + isPropertySignatureKind(functionSymbol.valueDeclaration, tsTools.ts) && |
| 106 | + functionSymbol.valueDeclaration.type !== undefined && |
| 107 | + isFunctionTypeKind(functionSymbol.valueDeclaration.type, tsTools.ts)) |
104 | 108 | );
|
105 | 109 | }
|
106 | 110 |
|
107 |
| -function isFunctionAsync(functionSymbol: Symbol): boolean { |
| 111 | +function isFunctionAsync(functionSymbol: Symbol, tsTools: TSTools): boolean { |
108 | 112 | return (
|
109 | 113 | functionSymbol.getDeclarations()?.some((declaration) => {
|
110 |
| - if (declaration.kind !== SyntaxKind.MethodSignature) { |
| 114 | + if (!isMethodSignatureKind(declaration, tsTools.ts)) { |
111 | 115 | return false;
|
112 | 116 | }
|
113 |
| - const declarationType = (declaration as MethodSignature).type; |
114 |
| - if (declarationType?.kind !== SyntaxKind.TypeReference) { |
| 117 | + if (declaration.type === undefined || !isTypeReferenceKind(declaration.type, tsTools.ts)) { |
115 | 118 | return false;
|
116 | 119 | }
|
117 |
| - const declarationTypeName = (declarationType as TypeReferenceNode).typeName; |
118 | 120 | return (
|
119 |
| - declarationTypeName.kind === SyntaxKind.Identifier && |
120 |
| - declarationTypeName.escapedText === 'Promise' |
| 121 | + isIdentifierKind(declaration.type.typeName, tsTools.ts) && |
| 122 | + declaration.type.typeName.escapedText === 'Promise' |
121 | 123 | );
|
122 | 124 | }) ?? false
|
123 | 125 | );
|
|
0 commit comments