Skip to content

Commit dfa0a83

Browse files
committed
chore: extracted node type checking functions to ts-utils
1 parent 1a5b2df commit dfa0a83

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed

Diff for: packages/eslint-plugin-svelte/src/rules/require-event-prefix.ts

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { createRule } from '../utils/index.js';
2-
import { type TSTools, getTypeScriptTools, isMethodType } from '../utils/ts-utils/index.js';
32
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';
1113
import type { CallExpression } from 'estree';
1214

1315
export default createRule('require-event-prefix', {
@@ -58,7 +60,7 @@ export default createRule('require-event-prefix', {
5860
if (
5961
isFunctionLike(property, tsTools) &&
6062
!property.getName().startsWith('on') &&
61-
(checkAsyncFunctions || !isFunctionAsync(property))
63+
(checkAsyncFunctions || !isFunctionAsync(property, tsTools))
6264
) {
6365
const declarationTsNode = property.getDeclarations()?.[0];
6466
const declarationEstreeNode =
@@ -99,25 +101,25 @@ function isFunctionLike(functionSymbol: Symbol, tsTools: TSTools): boolean {
99101
tsTools.service.program.getTypeChecker().getTypeOfSymbol(functionSymbol),
100102
tsTools.ts
101103
) ||
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))
104108
);
105109
}
106110

107-
function isFunctionAsync(functionSymbol: Symbol): boolean {
111+
function isFunctionAsync(functionSymbol: Symbol, tsTools: TSTools): boolean {
108112
return (
109113
functionSymbol.getDeclarations()?.some((declaration) => {
110-
if (declaration.kind !== SyntaxKind.MethodSignature) {
114+
if (!isMethodSignatureKind(declaration, tsTools.ts)) {
111115
return false;
112116
}
113-
const declarationType = (declaration as MethodSignature).type;
114-
if (declarationType?.kind !== SyntaxKind.TypeReference) {
117+
if (declaration.type === undefined || !isTypeReferenceKind(declaration.type, tsTools.ts)) {
115118
return false;
116119
}
117-
const declarationTypeName = (declarationType as TypeReferenceNode).typeName;
118120
return (
119-
declarationTypeName.kind === SyntaxKind.Identifier &&
120-
declarationTypeName.escapedText === 'Promise'
121+
isIdentifierKind(declaration.type.typeName, tsTools.ts) &&
122+
declaration.type.typeName.escapedText === 'Promise'
121123
);
122124
}) ?? false
123125
);

Diff for: packages/eslint-plugin-svelte/src/utils/ts-utils/index.ts

+38
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,41 @@ export function getTypeOfPropertyOfType(
313313
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- getTypeOfPropertyOfType is an internal API of TS.
314314
return (checker as any).getTypeOfPropertyOfType(type, name);
315315
}
316+
317+
/**
318+
* Check whether the given node is a property signature kind or not.
319+
*/
320+
export function isPropertySignatureKind(
321+
node: TS.Node,
322+
ts: TypeScript
323+
): node is TS.PropertySignature {
324+
return node.kind === ts.SyntaxKind.PropertySignature;
325+
}
326+
327+
/**
328+
* Check whether the given node is a function type kind or not.
329+
*/
330+
export function isFunctionTypeKind(node: TS.Node, ts: TypeScript): node is TS.FunctionTypeNode {
331+
return node.kind === ts.SyntaxKind.FunctionType;
332+
}
333+
334+
/**
335+
* Check whether the given node is a method signature kind or not.
336+
*/
337+
export function isMethodSignatureKind(node: TS.Node, ts: TypeScript): node is TS.MethodSignature {
338+
return node.kind === ts.SyntaxKind.MethodSignature;
339+
}
340+
341+
/**
342+
* Check whether the given node is a type reference kind or not.
343+
*/
344+
export function isTypeReferenceKind(node: TS.Node, ts: TypeScript): node is TS.TypeReferenceNode {
345+
return node.kind === ts.SyntaxKind.TypeReference;
346+
}
347+
348+
/**
349+
* Check whether the given node is an identifier kind or not.
350+
*/
351+
export function isIdentifierKind(node: TS.Node, ts: TypeScript): node is TS.Identifier {
352+
return node.kind === ts.SyntaxKind.Identifier;
353+
}

0 commit comments

Comments
 (0)