-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
chore(utils): use Extract
generic for ast-utils
' predicates
' helper functions
#4545
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0dc5052
chore(utils): use `Extract` generic for `ast-utils`' `predicates`' he…
MichaelDeBoey 04da286
chore: enable for `isNodeOfTypes`
MichaelDeBoey 8921bd4
chore: move TS keywords to Node
MichaelDeBoey 98b4df0
chore: enable for `isNodeOfTypeWithConditions`
MichaelDeBoey 328a82f
chore: enable for `isTokenOfTypeWithConditions`
MichaelDeBoey 52db9d8
chore: enable for `isNotTokenOfTypeWithConditions`
MichaelDeBoey d3f4b5d
fix: fix `Conditions` type for `isNodeOfTypeWithConditions`
MichaelDeBoey 5b52f9b
fix: fix `entries.every` condition in `isNodeOfTypeWithConditions`
MichaelDeBoey 3da4a94
chore: extract `ExtractedNode` & `ExtractedToken` generics
MichaelDeBoey 8def652
fix: fix `getGroup`
MichaelDeBoey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
10000
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,74 @@ | ||
import { AST_NODE_TYPES, AST_TOKEN_TYPES, TSESTree } from '../ts-estree'; | ||
|
||
type ObjectEntry<BaseType> = [keyof BaseType, BaseType[keyof BaseType]]; | ||
type ObjectEntry<BaseType> = BaseType extends unknown | ||
? [keyof BaseType, BaseType[keyof BaseType]] | ||
: never; | ||
type ObjectEntries<BaseType> = Array<ObjectEntry<BaseType>>; | ||
|
||
export const isNodeOfType = | ||
<NodeType extends AST_NODE_TYPES>(nodeType: NodeType) => | ||
( | ||
node: TSESTree.Node | null | undefined, | ||
): node is TSESTree.Node & { type: NodeType } => | ||
): node is Extract<TSESTree.Node, { type: NodeType }> => | ||
node?.type === nodeType; | ||
|
||
export const isNodeOfTypes = | ||
<NodeTypes extends readonly AST_NODE_TYPES[]>(nodeTypes: NodeTypes) => | ||
( | ||
node: TSESTree.Node | null | undefined, | ||
): node is TSESTree.Node & { type: NodeTypes[number] } => | ||
): node is Extract<TSESTree.Node, { type: NodeTypes[number] }> => | ||
MichaelDeBoey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
!!node && nodeTypes.includes(node.type); | ||
|
||
export const isNodeOfTypeWithConditions = < | ||
NodeType extends AST_NODE_TYPES, | ||
Conditions extends Partial<TSESTree.Node & { type: NodeType }>, | ||
ExtractedNode extends Extract<TSESTree.Node, { type: NodeType }>, | ||
Conditions extends Partial<ExtractedNode>, | ||
>( | ||
nodeType: NodeType, | ||
conditions: Conditions, | ||
): (( | ||
node: TSESTree.Node | null | undefined, | ||
) => node is TSESTree.Node & { type: NodeType } & Conditions) => { | ||
) => node is ExtractedNode & Conditions) => { | ||
const entries = Object.entries(conditions) as ObjectEntries<TSESTree.Node>; | ||
|
||
return ( | ||
node: TSESTree.Node | null | undefined, | ||
): node is TSESTree.Node & { type: NodeType } & Conditions => | ||
): node is ExtractedNode & Conditions => | ||
node?.type === nodeType && | ||
entries.every(([key, value]) => node[key] === value); | ||
entries.every(([key, value]) => node[key as keyof TSESTree.Node] === value); | ||
}; | ||
|
||
export const isTokenOfTypeWithConditions = < | ||
TokenType extends AST_TOKEN_TYPES, | ||
ExtractedToken extends Extract<TSESTree.Token, { type: TokenType }>, | ||
Conditions extends Partial<TSESTree.Token & { type: TokenType }>, | ||
>( | ||
tokenType: TokenType, | ||
conditions: Conditions, | ||
): (( | ||
token: TSESTree.Token | null | undefined, | ||
) => token is TSESTree.Token & { type: TokenType } & Conditions) => { | ||
) => token is ExtractedToken & Conditions) => { | ||
const entries = Object.entries(conditions) as ObjectEntries<TSESTree.Token>; | ||
|
||
return ( | ||
token: TSESTree.Token | null | undefined, | ||
): token is TSESTree.Token & { type: TokenType } & Conditions => | ||
): token is ExtractedToken & Conditions => | ||
token?.type === tokenType && | ||
entries.every(([key, value]) => token[key] === value); | ||
entries.every( | ||
([key, value]) => token[key as keyof TSESTree.Token] === value, | ||
); | ||
}; | ||
|
||
export const isNotTokenOfTypeWithConditions = | ||
< | ||
TokenType extends AST_TOKEN_TYPES, | ||
Conditions extends Partial<TSESTree.Token & { type: TokenType }>, | ||
ExtractedToken extends Extract<TSESTree.Token, { type: TokenType }>, | ||
bradzacher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Conditions extends Partial<ExtractedToken>, | ||
>( | ||
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 | ||
> => | ||
) => token is Exclude<TSESTree.Token, ExtractedToken & Conditions>) => | ||
(token): token is Exclude<TSESTree.Token, ExtractedToken & Conditions> => | ||
!isTokenOfTypeWithConditions(tokenType, conditions)(token); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.