Skip to content

Commit

Permalink
feat(utils): extract isNotTokenOfTypeWithConditions out of `ast-uti…
Browse files Browse the repository at this point in the history
…ls`' `predicates` (typescript-eslint#4502)
  • Loading branch information
MichaelDeBoey authored and JoshuaKGoldberg committed Feb 27, 2022
1 parent 22dd165 commit 5934fbc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
21 changes: 21 additions & 0 deletions packages/utils/src/ast-utils/helpers.ts
Expand Up @@ -56,3 +56,24 @@ export const isTokenOfTypeWithConditions = <
token?.type === tokenType &&
entries.every(([key, value]) => token[key] === value);
};

export const isNotTokenOfTypeWithConditions =
<
TokenType extends AST_TOKEN_TYPES,
Conditions extends Partial<TSESTree.Token & { type: TokenType }>,
>(
tokenType: TokenType,
conditions: Conditions,
): ((
token: TSESTree.Token | null | undefined,
) => token is Exclude<
TSESTree.Token,
TSESTree.Token & { type: TokenType } & Conditions
>) =>
(
token,
): token is Exclude<
TSESTree.Token,
TSESTree.Token & { type: TokenType } & Conditions
> =>
!isTokenOfTypeWithConditions(tokenType, conditions)(token);
22 changes: 9 additions & 13 deletions packages/utils/src/ast-utils/predicates.ts
Expand Up @@ -4,6 +4,7 @@ import {
isNodeOfType,
isNodeOfTypes,
isNodeOfTypeWithConditions,
isNotTokenOfTypeWithConditions,
isTokenOfTypeWithConditions,
} from './helpers';

Expand All @@ -12,25 +13,20 @@ const isOptionalChainPunctuator = isTokenOfTypeWithConditions(
{ value: '?.' },
);

function isNotOptionalChainPunctuator(
token: TSESTree.Token,
): token is Exclude<
TSESTree.Token,
TSESTree.PunctuatorToken & { value: '?.' }
> {
return !isOptionalChainPunctuator(token);
}
const isNotOptionalChainPunctuator = isNotTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '?.' },
);

const isNonNullAssertionPunctuator = isTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '!' },
);

function isNotNonNullAssertionPunctuator(
token: TSESTree.Token,
): token is Exclude<TSESTree.Token, TSESTree.PunctuatorToken & { value: '!' }> {
return !isNonNullAssertionPunctuator(token);
}
const isNotNonNullAssertionPunctuator = isNotTokenOfTypeWithConditions(
AST_TOKEN_TYPES.Punctuator,
{ value: '!' },
);

/**
* Returns true if and only if the node represents: foo?.() or foo.bar?.()
Expand Down

0 comments on commit 5934fbc

Please sign in to comment.