Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix: guard against null identifier nodes #309

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 8 commits into from
Apr 3, 2021
Merged

Conversation

Belco90
Copy link
Member

@Belco90 Belco90 commented Apr 2, 2021

Closes #305. Closes #306.

@Belco90 Belco90 self-assigned this Apr 2, 2021
Comment on lines 9 to 11
branches: 90,
functions: 90,
lines: 90,
Copy link
Member Author

Choose a reason for hiding this comment

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

I've dropped the coverage threshold a bit since I prefer to guard against null nodes everywhere even if there is no possible code for it. We'll see what I do with this after releasing v4.

@@ -83,13 +89,22 @@ export default createTestingLibraryRule<Options, MessageIds>({
},
CallExpression(node) {
const callExpressionIdentifier = getDeepestIdentifierNode(node);
Copy link
Member Author

@Belco90 Belco90 Apr 2, 2021

Choose a reason for hiding this comment

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

@timdeschryver This is really weird: getDeepestIdentifierNode (and some other utils for finding identifiers) are typed as returning TSESTree.Identifier | null. However, TS is assuming they always returning TSESTree.Identifier but never null:

CleanShot 2021-04-02 at 12 02 28

Then, if I update the function to return '' instead of return null and update the returned type to TSESTree.Identifier | string, TS starts complaining about returned value saved in all these rules might not be an Identifier. Is this a bug on TS? Am I missing something else? I don't get why it always assumes null won't be returned.

Copy link

Choose a reason for hiding this comment

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

I suspect turning on strictNullChecks in the tsconfig would fix that.

Copy link
Member

Choose a reason for hiding this comment

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

Oh wow... TIL...
@epmatsw is right, you'll have to turn the strict mode on.
I would encourage to turn on strict mode though, not just strict null checks.

Copy link
Member Author

Choose a reason for hiding this comment

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

Aah damn! I thought we had strict enabled, but anyway I didn't expect those nullable values to be silent. I'm gonna enable strict mode and fix all corresponding issues then. Thanks guys.

@@ -139,6 +139,10 @@ ruleTester.run(RULE_NAME, rule, {
debug()
`,
},

`// cover edge case for https://github.com/testing-library/eslint-plugin-testing-library/issues/306
thing.method.lastCall.args[0]();
Copy link
Member Author

Choose a reason for hiding this comment

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

Test for issue mentioned by @epmatsw

@Belco90
Copy link
Member Author

Belco90 commented Apr 3, 2021

Ok, so I've enabled TS strict mode and fix all issues. But now tests files generating invalid cases spreading an array of cases are flagged as errors by TS. I'm not sure what to do with that. Any ideas? @timdeschryver @epmatsw
CleanShot 2021-04-03 at 12 47 14
CleanShot 2021-04-03 at 12 47 24

@epmatsw
Copy link

epmatsw commented Apr 3, 2021

Hmm. Maybe throw an as const somewhere in there?

@Belco90
Copy link
Member Author

Belco90 commented Apr 3, 2021

Hmm. Maybe throw an as const somewhere in there?

That fixed almost all of them. The problem is the messageId for the errors, so TS complains about receiving an string but is declared as an enum. The as const does the trick for it. I'm learning more TS with this PR than in my whole life πŸ˜‚

Now I got some tests failing, I'm looking into them.

@Belco90
Copy link
Member Author

Belco90 commented Apr 3, 2021

Everything ok now, merging to test it on beta 3.

@Belco90 Belco90 merged commit eaca948 into beta Apr 3, 2021
@Belco90 Belco90 deleted the 306_null-identifiers branch April 3, 2021 16:33
@github-actions
Copy link

github-actions bot commented Apr 3, 2021

πŸŽ‰ This PR is included in version 4.0.0-beta.3 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€

@github-actions
Copy link

πŸŽ‰ This PR is included in version 4.0.0 πŸŽ‰

The release is available on:

Your semantic-release bot πŸ“¦πŸš€

@MichaelDeBoey MichaelDeBoey added this to the v4 milestone Apr 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants