Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(experimental-utils): extract isNodeOfTypeWithConditions out of ast-utils' predicates #3837

Merged
merged 1 commit into from Oct 3, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 36 additions & 25 deletions packages/experimental-utils/src/ast-utils/predicates.ts
Expand Up @@ -7,6 +7,28 @@ 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) => {
Comment on lines +18 to +20
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Due to the extraction of entries & @typescript-eslint/explicit-function-return-type, we have to duplicate this return type

If you want, I can extract this into a NodeIsOfTypeWithConditionsFunction type if you want, but this won't be much clearer I'm afraid 🤷‍♂️

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with the duplication on the util.
I'm also happy to just disable the linter on this check too.
We can merge this as is.

const entries = Object.entries(conditions) as ObjectEntries<
TSESTree.Node & { type: NodeType }
>;

return (
node: TSESTree.Node | null | undefined,
): node is TSESTree.Node & { type: NodeType } & Conditions =>
node?.type === nodeType &&
entries.every(([key, value]) => node[key] === value);
};

function isOptionalChainPunctuator(
token: TSESTree.Token,
): token is TSESTree.PunctuatorToken & { value: '?.' } {
Expand Down Expand Up @@ -35,27 +57,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 +180,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