-
Notifications
You must be signed in to change notification settings - Fork 148
refactor(node-utils): extract isNodeOfType
functions
#329
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
refactor(node-utils): extract isNodeOfType
functions
#329
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmmm 🤔
I don't think that's possible because there's no "link" between AST_NODE_TYPES
and TSESTree.Node
.
@timdeschryver There actually is a 'link', as I know it's a complex situation. |
@timdeschryver I updated |
Looking a bit further into it, the type predicates for union types are incorrect now. 😕🤔
| (BigIntLiteral & { type: 'Literal' })
| (BooleanLiteral & { type: 'Literal' })
| (NumberLiteral & { type: 'Literal' })
| (NullLiteral & { type: 'Literal' })
| (RegExpLiteral & { type: 'Literal' })
| (StringLiteral & { type: 'Literal' }) which is incorrect of course, as that should be | (BigIntLiteral & { type: 'BigIntLiteral' })
| (BooleanLiteral & { type: 'BooleanLiteral' })
| (NumberLiteral & { type: 'NumberLiteral' })
| (NullLiteral & { type: 'NullLiteral' })
| (RegExpLiteral & { type: 'RegExpLiteral' })
| (StringLiteral & { type: 'StringLiteral' }) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯 Nice!
I learned something new. While it isn't 100% correct (as you mentioned), I think this covers our cases. Or we could create specific type utils for those cases if that's needed 🤔
Creating a new |
I just had a talk with @RobinMalfait and he suggested to have a look at https://www.typescriptlang.org/docs/handbook/2/mapped-types.html as this could possibly help us out here Will check it out later today when I got some time |
@MichaelDeBoey thanks for your effort around this! It's something we definitely wanted to do on v4 but didn't have time to work on. |
isNodeOfType
functions
It seems like my second try is actually correct (even for union types)! 🎉 I was assuming
Thanks to @bmvantunes for pointing this out to me! 👊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks so clean now 😮
I'd like to wait for @timdeschryver's approval, who is the TS expert!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does (of course) have my approval.
Good job @MichaelDeBoey 👏👏👏
🎉 This PR is included in version 4.0.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Since all these functions are doing almost exactly the same, I extracted
isNodeOfType
and put all these kind of functions in a separate file.@timdeschryver can probably help to make
isNodeOfType
more type-safe so we don't have to passNodeOfType
type anymore and we can dobut I can't find it working atm.