Skip to content

Commit 445adc8

Browse files
refactor(node-utils): extract isNodeOfType functions (#329)
* refactor(node-utils): extract isNodeOfType functions * refactor(node-utils): make isNodeOfType more type-safe
1 parent 9931a51 commit 445adc8

File tree

2 files changed

+50
-102
lines changed

2 files changed

+50
-102
lines changed

lib/node-utils.ts renamed to lib/node-utils/index.ts

Lines changed: 14 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ import {
77
} from '@typescript-eslint/experimental-utils';
88
import { RuleContext } from '@typescript-eslint/experimental-utils/dist/ts-eslint';
99

10+
import {
11+
isArrayExpression,
12+
isArrowFunctionExpression,
13+
isBlockStatement,
14+
isCallExpression,
15+
isExpressionStatement,
16+
isImportDeclaration,
17+
isLiteral,
18+
isMemberExpression,
19+
isReturnStatement,
20+
} from './is-node-of-type';
21+
22+
export * from './is-node-of-type';
23+
1024
const ValidLeftHandSideExpressions = [
1125
AST_NODE_TYPES.CallExpression,
1226
AST_NODE_TYPES.ClassExpression,
@@ -35,78 +49,6 @@ const ValidLeftHandSideExpressions = [
3549
AST_NODE_TYPES.ArrowFunctionExpression,
3650
];
3751

38-
export function isCallExpression(
39-
node: TSESTree.Node | null | undefined
40-
): node is TSESTree.CallExpression {
41-
return node?.type === AST_NODE_TYPES.CallExpression;
42-
}
43-
44-
export function isNewExpression(
45-
node: TSESTree.Node | null | undefined
46-
): node is TSESTree.NewExpression {
47-
return node?.type === 'NewExpression';
48-
}
49-
50-
export function isMemberExpression(
51-
node: TSESTree.Node | null | undefined
52-
): node is TSESTree.MemberExpression {
53-
return node?.type === AST_NODE_TYPES.MemberExpression;
54-
}
55-
56-
export function isLiteral(
57-
node: TSESTree.Node | null | undefined
58-
): node is TSESTree.Literal {
59-
return node?.type === AST_NODE_TYPES.Literal;
60-
}
61-
62-
export function isImportSpecifier(
63-
node: TSESTree.Node | null | undefined
64-
): node is TSESTree.ImportSpecifier {
65-
return node?.type === AST_NODE_TYPES.ImportSpecifier;
66-
}
67-
68-
export function isImportNamespaceSpecifier(
69-
node: TSESTree.Node | null | undefined
70-
): node is TSESTree.ImportNamespaceSpecifier {
71-
return node?.type === AST_NODE_TYPES.ImportNamespaceSpecifier;
72-
}
73-
74-
export function isImportDefaultSpecifier(
75-
node: TSESTree.Node | null | undefined
76-
): node is TSESTree.ImportDefaultSpecifier {
77-
return node?.type === AST_NODE_TYPES.ImportDefaultSpecifier;
78-
}
79-
80-
export function isBlockStatement(
81-
node: TSESTree.Node | null | undefined
82-
): node is TSESTree.BlockStatement {
83-
return node?.type === AST_NODE_TYPES.BlockStatement;
84-
}
85-
86-
export function isObjectPattern(
87-
node: TSESTree.Node | null | undefined
88-
): node is TSESTree.ObjectPattern {
89-
return node?.type === AST_NODE_TYPES.ObjectPattern;
90-
}
91-
92-
export function isProperty(
93-
node: TSESTree.Node | null | undefined
94-
): node is TSESTree.Property {
95-
return node?.type === AST_NODE_TYPES.Property;
96-
}
97-
98-
export function isJSXAttribute(
99-
node: TSESTree.Node | null | undefined
100-
): node is TSESTree.JSXAttribute {
101-
return node?.type === AST_NODE_TYPES.JSXAttribute;
102-
}
103-
104-
export function isExpressionStatement(
105-
node: TSESTree.Node | null | undefined
106-
): node is TSESTree.ExpressionStatement {
107-
return node?.type === AST_NODE_TYPES.ExpressionStatement;
108-
}
109-
11052
/**
11153
* Finds the closest CallExpression node for a given node.
11254
* @param node
@@ -153,12 +95,6 @@ export function findClosestCallNode(
15395
}
15496
}
15597

156-
export function isObjectExpression(
157-
node: TSESTree.Expression
158-
): node is TSESTree.ObjectExpression {
159-
return node?.type === AST_NODE_TYPES.ObjectExpression;
160-
}
161-
16298
export function hasThenProperty(node: TSESTree.Node): boolean {
16399
return (
164100
isMemberExpression(node) &&
@@ -167,30 +103,6 @@ export function hasThenProperty(node: TSESTree.Node): boolean {
167103
);
168104
}
169105

170-
export function isArrowFunctionExpression(
171-
node: TSESTree.Node
172-
): node is TSESTree.ArrowFunctionExpression {
173-
return node?.type === AST_NODE_TYPES.ArrowFunctionExpression;
174-
}
175-
176-
export function isReturnStatement(
177-
node: TSESTree.Node
178-
): node is TSESTree.ReturnStatement {
179-
return node?.type === AST_NODE_TYPES.ReturnStatement;
180-
}
181-
182-
export function isArrayExpression(
183-
node: TSESTree.Node
184-
): node is TSESTree.ArrayExpression {
185-
return node?.type === AST_NODE_TYPES.ArrayExpression;
186-
}
187-
188-
export function isImportDeclaration(
189-
node: TSESTree.Node | null | undefined
190-
): node is TSESTree.ImportDeclaration {
191-
return node?.type === AST_NODE_TYPES.ImportDeclaration;
192-
}
193-
194106
export function hasChainedThen(node: TSESTree.Node): boolean {
195107
const parent = node.parent;
196108

lib/node-utils/is-node-of-type.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {
2+
AST_NODE_TYPES,
3+
TSESTree,
4+
} from '@typescript-eslint/experimental-utils';
5+
6+
const isNodeOfType = <NodeType extends AST_NODE_TYPES>(nodeType: NodeType) => (
7+
node: TSESTree.Node | null | undefined
8+
): node is TSESTree.Node & { type: NodeType } => node?.type === nodeType;
9+
10+
export const isArrayExpression = isNodeOfType(AST_NODE_TYPES.ArrayExpression);
11+
export const isArrowFunctionExpression = isNodeOfType(
12+
AST_NODE_TYPES.ArrowFunctionExpression
13+
);
14+
export const isBlockStatement = isNodeOfType(AST_NODE_TYPES.BlockStatement);
15+
export const isCallExpression = isNodeOfType(AST_NODE_TYPES.CallExpression);
16+
export const isExpressionStatement = isNodeOfType(
17+
AST_NODE_TYPES.ExpressionStatement
18+
);
19+
export const isImportDeclaration = isNodeOfType(
20+
AST_NODE_TYPES.ImportDeclaration
21+
);
22+
export const isImportDefaultSpecifier = isNodeOfType(
23+
AST_NODE_TYPES.ImportDefaultSpecifier
24+
);
25+
export const isImportNamespaceSpecifier = isNodeOfType(
26+
AST_NODE_TYPES.ImportNamespaceSpecifier
27+
);
28+
export const isImportSpecifier = isNodeOfType(AST_NODE_TYPES.ImportSpecifier);
29+
export const isJSXAttribute = isNodeOfType(AST_NODE_TYPES.JSXAttribute);
30+
export const isLiteral = isNodeOfType(AST_NODE_TYPES.Literal);
31+
export const isMemberExpression = isNodeOfType(AST_NODE_TYPES.MemberExpression);
32+
export const isNewExpression = isNodeOfType(AST_NODE_TYPES.NewExpression);
33+
export const isObjectExpression = isNodeOfType(AST_NODE_TYPES.ObjectExpression);
34+
export const isObjectPattern = isNodeOfType(AST_NODE_TYPES.ObjectPattern);
35+
export const isProperty = isNodeOfType(AST_NODE_TYPES.Property);
36+
export const isReturnStatement = isNodeOfType(AST_NODE_TYPES.ReturnStatement);

0 commit comments

Comments
 (0)