Skip to content

Commit

Permalink
feat(experimental-utils): extract isTokenOfTypeWithConditions out o…
Browse files Browse the repository at this point in the history
…f `ast-utils`' `predicates`
  • Loading branch information
MichaelDeBoey committed Oct 8, 2021
1 parent 3650589 commit ad55cbb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
27 changes: 24 additions & 3 deletions packages/experimental-utils/src/ast-utils/helpers.ts
@@ -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
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

0 comments on commit ad55cbb

Please sign in to comment.