Skip to content

feat(experimental-utils): extract isTokenOfTypeWithConditions out of ast-utils' predicates #3977

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

Merged
merged 1 commit into from
Nov 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions packages/experimental-utils/src/ast-utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { AST_NODE_TYPES, TSESTree } from '../ts-estree';
import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from '../ts-estree';

type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>;

export const isNodeOfType =
<NodeType extends AST_NODE_TYPES>(nodeType: NodeType) =>
Expand All @@ -14,8 +17,6 @@ export const isNodeOfTypes =
): node is TSESTree.Node & { type: NodeTypes[number] } =>
!!node && nodeTypes.includes(node.type);

type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]];
type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>;
export const isNodeOfTypeWithConditions = <
NodeType extends AST_NODE_TYPES,
Conditions extends Partial<TSESTree.Node & { type: NodeType }>,
Expand All @@ -35,3 +36,23 @@ export const isNodeOfTypeWithConditions = <
node?.type === nodeType &&
entries.every(([key, value]) => node[key] === value);
};

export const isTokenOfTypeWithConditions = <
TokenType extends AST_TOKEN_TYPES,
Conditions extends Partial<TSESTree.Token & { type: TokenType }>,
>(
tokenType: TokenType,
conditions: Conditions,
): ((
token: TSESTree.Token | null | undefined,
) => token is TSESTree.Token & { type: TokenType } & Conditions) => {
const entries = Object.entries(conditions) as ObjectEntries<
TSESTree.Token & { type: TokenType }
>;

return (
token: TSESTree.Token | null | undefined,
): token is TSESTree.Token & { type: TokenType } & Conditions =>
token?.type === tokenType &&
entries.every(([key, value]) => token[key] === value);
};
29 changes: 14 additions & 15 deletions packages/experimental-utils/src/ast-utils/predicates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import {
isNodeOfType,
isNodeOfTypes,
isNodeOfTypeWithConditions,
isTokenOfTypeWithConditions,
} from './helpers';

function isOptionalChainPunctuator(
token: TSESTree.Token,
): token is TSESTree.PunctuatorToken & { value: '?.' } {
return token.type === AST_TOKEN_TYPES.Punctuator && token.value === '?.';
}
const isOptionalChainPunctuator = isTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '?.' },
);

function isNotOptionalChainPunctuator(
token: TSESTree.Token,
): token is Exclude<
Expand All @@ -20,11 +21,11 @@ function isNotOptionalChainPunctuator(
return !isOptionalChainPunctuator(token);
}

function isNonNullAssertionPunctuator(
token: TSESTree.Token,
): token is TSESTree.PunctuatorToken & { value: '!' } {
return token.type === AST_TOKEN_TYPES.Punctuator && token.value === '!';
}
const isNonNullAssertionPunctuator = isTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '!' },
);

function isNotNonNullAssertionPunctuator(
token: TSESTree.Token,
): token is Exclude<TSESTree.Token, TSESTree.PunctuatorToken & { value: '!' }> {
Expand Down Expand Up @@ -138,11 +139,9 @@ const isAwaitExpression = isNodeOfType(AST_NODE_TYPES.AwaitExpression);
/**
* Checks if a possible token is the `await` keyword.
*/
function isAwaitKeyword(
node: TSESTree.Token | undefined | null,
): node is TSESTree.IdentifierToken & { value: 'await' } {
return node?.type === AST_TOKEN_TYPES.Identifier && node.value === 'await';
}
const isAwaitKeyword = isTokenOfTypeWithConditions(AST_TOKEN_TYPES.Identifier, {
value: 'await',
});

const isLoop = isNodeOfTypes([
AST_NODE_TYPES.DoWhileStatement,
Expand Down