Skip to content

Commit

Permalink
feat(experimental-utils): extract isNodeOfTypeWithConditions out of…
Browse files Browse the repository at this point in the history
… `ast-utils`' `predicates`
  • Loading branch information
MichaelDeBoey committed Sep 20, 2021
1 parent ebb33ed commit 0b29c93
Showing 1 changed file with 34 additions and 25 deletions.
59 changes: 34 additions & 25 deletions packages/experimental-utils/src/ast-utils/predicates.ts
Expand Up @@ -7,6 +7,26 @@ const isNodeOfType =
): node is TSESTree.Node & { type: NodeType } =>
node?.type === nodeType;

type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>;
const isNodeOfTypeWithConditions =
<
NodeType extends AST_NODE_TYPES,
Conditions extends Partial<TSESTree.Node & { type: NodeType }>,
>(
nodeType: NodeType,
conditions: Conditions,
) =>
(
node: TSESTree.Node | null | undefined,
): node is TSESTree.Node & { type: NodeType } & Conditions =>
node?.type === nodeType &&
(
Object.entries(conditions) as ObjectEntries<
TSESTree.Node & { type: NodeType }
>
).every(([key, value]) => node[key] === value);

function isOptionalChainPunctuator(
token: TSESTree.Token,
): token is TSESTree.PunctuatorToken & { value: '?.' } {
Expand Down Expand Up @@ -35,27 +55,20 @@ function isNotNonNullAssertionPunctuator(
/**
* Returns true if and only if the node represents: foo?.() or foo.bar?.()
*/
function isOptionalCallExpression(
node: TSESTree.Node,
): node is TSESTree.CallExpression & { optional: true } {
return (
node.type === AST_NODE_TYPES.CallExpression &&
// this flag means the call expression itself is option
// i.e. it is foo.bar?.() and not foo?.bar()
node.optional
);
}
const isOptionalCallExpression = isNodeOfTypeWithConditions(
AST_NODE_TYPES.CallExpression,
// this flag means the call expression itself is option
// i.e. it is foo.bar?.() and not foo?.bar()
{ optional: true },
);

/**
* Returns true if and only if the node represents logical OR
*/
function isLogicalOrOperator(
node: TSESTree.Node,
): node is TSESTree.LogicalExpression & { operator: '||' } {
return (
node.type === AST_NODE_TYPES.LogicalExpression && node.operator === '||'
);
}
const isLogicalOrOperator = isNodeOfTypeWithConditions(
AST_NODE_TYPES.LogicalExpression,
{ operator: '||' },
);

/**
* Checks if a node is a type assertion:
Expand Down Expand Up @@ -165,14 +178,10 @@ function isClassOrTypeElement(
/**
* Checks if a node is a constructor method.
*/
function isConstructor(
node: TSESTree.Node | undefined,
): node is TSESTree.MethodDefinition {
return (
node?.type === AST_NODE_TYPES.MethodDefinition &&
node.kind === 'constructor'
);
}
const isConstructor = isNodeOfTypeWithConditions(
AST_NODE_TYPES.MethodDefinition,
{ kind: 'constructor' },
);

/**
* Checks if a node is a setter method.
Expand Down

0 comments on commit 0b29c93

Please sign in to comment.