Skip to content

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

Merged
merged 2 commits into from
Apr 14, 2021
Merged

refactor(node-utils): extract isNodeOfType functions #329

merged 2 commits into from
Apr 14, 2021

Conversation

MichaelDeBoey
Copy link
Member

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 pass NodeOfType type anymore and we can do

export const isArrayExpression = isNodeOfType(AST_NODE_TYPES.ArrayExpression);

but I can't find it working atm.

timdeschryver
timdeschryver previously approved these changes Apr 12, 2021
Copy link
Member

@timdeschryver timdeschryver left a 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.

@MichaelDeBoey
Copy link
Member Author

MichaelDeBoey commented Apr 12, 2021

@timdeschryver There actually is a 'link', as TSESTree.Node will have a type of the given AST_NODE_TYPES value (as that's what every function is checking for).

I know it's a complex situation.
I know it is possible to do so, just can't figure out how to do so and was hoping you could help out here.
If not, I'll just leave it like this

@MichaelDeBoey
Copy link
Member Author

@timdeschryver I updated isNodeOfType in 85ad91c and that seems to do what I want it to do.
Would love to have your feedback though.

@MichaelDeBoey
Copy link
Member Author

MichaelDeBoey commented Apr 12, 2021

Looking a bit further into it, the type predicates for union types are incorrect now. 😕🤔

isLiteral will check for

| (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' })

Copy link
Member

@timdeschryver timdeschryver left a 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 🤔

@MichaelDeBoey
Copy link
Member Author

Creating a new isNodeOfUnionType generic function (which will probably be the first implement I had) whenever we need it sounds good to me 👍

@MichaelDeBoey
Copy link
Member Author

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

@Belco90
Copy link
Member

Belco90 commented Apr 13, 2021

@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.

@MichaelDeBoey MichaelDeBoey changed the title refactor(node-utils): extract isNodeOfType functions refactor(node-utils): extract isNodeOfType functions Apr 13, 2021
@MichaelDeBoey
Copy link
Member Author

MichaelDeBoey commented Apr 13, 2021

It seems like my second try is actually correct (even for union types)! 🎉

I was assuming BigIntLiteral & { type: 'Literal' } was incorrect and should be BigIntLiteral & { type: 'BigIntLiteral' }, but:

Thanks to @bmvantunes for pointing this out to me! 👊

@Belco90 Belco90 requested a review from timdeschryver April 14, 2021 14:32
Copy link
Member

@Belco90 Belco90 left a 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!

Copy link
Member

@timdeschryver timdeschryver left a 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 👏👏👏

@Belco90 Belco90 merged commit 445adc8 into testing-library:main Apr 14, 2021
@MichaelDeBoey MichaelDeBoey deleted the extract-is-node-of-type branch April 14, 2021 19:43
@github-actions
Copy link

🎉 This PR is included in version 4.0.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants