Skip to content

Commit 214f898

Browse files
feat(experimental-utils): extract isNodeOfTypeWithConditions out of ast-utils' predicates (typescript-eslint#3837)
1 parent 5a05362 commit 214f898

File tree

1 file changed

+36
-25
lines changed

1 file changed

+36
-25
lines changed

packages/experimental-utils/src/ast-utils/predicates.ts

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,28 @@ const isNodeOfType =
77
): node is TSESTree.Node & { type: NodeType } =>
88
node?.type === nodeType;
99

10+
type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
11+
type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>;
12+
const isNodeOfTypeWithConditions = <
13+
NodeType extends AST_NODE_TYPES,
14+
Conditions extends Partial<TSESTree.Node & { type: NodeType }>,
15+
>(
16+
nodeType: NodeType,
17+
conditions: Conditions,
18+
): ((
19+
node: TSESTree.Node | null | undefined,
20+
) => node is TSESTree.Node & { type: NodeType } & Conditions) => {
21+
const entries = Object.entries(conditions) as ObjectEntries<
22+
TSESTree.Node & { type: NodeType }
23+
>;
24+
25+
return (
26+
node: TSESTree.Node | null | undefined,
27+
): node is TSESTree.Node & { type: NodeType } & Conditions =>
28+
node?.type === nodeType &&
29+
entries.every(([key, value]) => node[key] === value);
30+
};
31+
1032
function isOptionalChainPunctuator(
1133
token: TSESTree.Token,
1234
): token is TSESTree.PunctuatorToken & { value: '?.' } {
@@ -35,27 +57,20 @@ function isNotNonNullAssertionPunctuator(
3557
/**
3658
* Returns true if and only if the node represents: foo?.() or foo.bar?.()
3759
*/
38-
function isOptionalCallExpression(
39-
node: TSESTree.Node,
40-
): node is TSESTree.CallExpression & { optional: true } {
41-
return (
42-
node.type === AST_NODE_TYPES.CallExpression &&
43-
// this flag means the call expression itself is option
44-
// i.e. it is foo.bar?.() and not foo?.bar()
45-
node.optional
46-
);
47-
}
60+
const isOptionalCallExpression = isNodeOfTypeWithConditions(
61+
AST_NODE_TYPES.CallExpression,
62+
// this flag means the call expression itself is option
63+
// i.e. it is foo.bar?.() and not foo?.bar()
64+
{ optional: true },
65+
);
4866

4967
/**
5068
* Returns true if and only if the node represents logical OR
5169
*/
52-
function isLogicalOrOperator(
53-
node: TSESTree.Node,
54-
): node is TSESTree.LogicalExpression & { operator: '||' } {
55-
return (
56-
node.type === AST_NODE_TYPES.LogicalExpression && node.operator === '||'
57-
);
58-
}
70+
const isLogicalOrOperator = isNodeOfTypeWithConditions(
71+
AST_NODE_TYPES.LogicalExpression,
72+
{ operator: '||' },
73+
);
5974

6075
/**
6176
* Checks if a node is a type assertion:
@@ -165,14 +180,10 @@ function isClassOrTypeElement(
165180
/**
166181
* Checks if a node is a constructor method.
167182
*/
168-
function isConstructor(
169-
node: TSESTree.Node | undefined,
170-
): node is TSESTree.MethodDefinition {
171-
return (
172-
node?.type === AST_NODE_TYPES.MethodDefinition &&
173-
node.kind === 'constructor'
174-
);
175-
}
183+
const isConstructor = isNodeOfTypeWithConditions(
184+
AST_NODE_TYPES.MethodDefinition,
185+
{ kind: 'constructor' },
186+
);
176187

177188
/**
178189
* Checks if a node is a setter method.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy