Skip to content

Commit aedca99

Browse files
committed
chore: extracted node type checking functions to ts-utils
1 parent 96ad1db commit aedca99

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, isMethodSymbol } 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+
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';
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 =
@@ -96,25 +98,25 @@ function getPropsType(node: CallExpression, tsTools: TSTools): Type | undefined
9698
function isFunctionLike(functionSymbol: Symbol, tsTools: TSTools): boolean {
9799
return (
98100
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))
101105
);
102106
}
103107

104-
function isFunctionAsync(functionSymbol: Symbol): boolean {
108+
function isFunctionAsync(functionSymbol: Symbol, tsTools: TSTools): boolean {
105109
return (
106110
functionSymbol.getDeclarations()?.some((declaration) => {
107-
if (declaration.kind !== SyntaxKind.MethodSignature) {
111+
if (!isMethodSignatureKind(declaration, tsTools.ts)) {
108112
return false;
109113
}
110-
const declarationType = (declaration as MethodSignature).type;
111-
if (declarationType?.kind !== SyntaxKind.TypeReference) {
114+
if (declaration.type === undefined || !isTypeReferenceKind(declaration.type, tsTools.ts)) {
112115
return false;
113116
}
114-
const declarationTypeName = (declarationType as TypeReferenceNode).typeName;
115117
return (
116-
declarationTypeName.kind === SyntaxKind.Identifier &&
117-
declarationTypeName.escapedText === 'Promise'
118+
isIdentifierKind(declaration.type.typeName, tsTools.ts) &&
119+
declaration.type.typeName.escapedText === 'Promise'
118120
);
119121
}) ?? false
120122
);

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

+38
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,41 @@ export function getTypeOfPropertyOfType(
314314
export function isMethodSymbol(type: TS.Symbol, ts: TypeScript): boolean {
315315
return (type.getFlags() & ts.SymbolFlags.Method) !== 0;
316316
}
317+
318+
/**
319+
* Check whether the given node is a property signature kind or not.
320+
*/
321+
export function isPropertySignatureKind(
322+
node: TS.Node,
323+
ts: TypeScript
324+
): node is TS.PropertySignature {
325+
return node.kind === ts.SyntaxKind.PropertySignature;
326+
}
327+
328+
/**
329+
* Check whether the given node is a function type kind or not.
330+
*/
331+
export function isFunctionTypeKind(node: TS.Node, ts: TypeScript): node is TS.FunctionTypeNode {
332+
return node.kind === ts.SyntaxKind.FunctionType;
333+
}
334+
335+
/**
336+
* Check whether the given node is a method signature kind or not.
337+
*/
338+
export function isMethodSignatureKind(node: TS.Node, ts: TypeScript): node is TS.MethodSignature {
339+
return node.kind === ts.SyntaxKind.MethodSignature;
340+
}
341+
342+
/**
343+
* Check whether the given node is a type reference kind or not.
344+
*/
345+
export function isTypeReferenceKind(node: TS.Node, ts: TypeScript): node is TS.TypeReferenceNode {
346+
return node.kind === ts.SyntaxKind.TypeReference;
347+
}
348+
349+
/**
350+
* Check whether the given node is an identifier kind or not.
351+
*/
352+
export function isIdentifierKind(node: TS.Node, ts: TypeScript): node is TS.Identifier {
353+
return node.kind === ts.SyntaxKind.Identifier;
354+
}

0 commit comments

Comments
 (0)