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(utils): extract isNotTokenOfTypeWithConditions out of ast-utils' predicates #4502

Merged
merged 1 commit into from Feb 24, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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<
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, linting obliges me to add this type a second time 😢

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