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

fix(prefer-in-document): handle toHaveLength without any arguments and with trailing commas #276

Merged
merged 3 commits into from
Nov 10, 2022

Conversation

berkinanik
Copy link
Contributor

@berkinanik berkinanik commented Nov 8, 2022

What:

  1. For toHaveLength:
  • Calling toHaveLength without any arguments causing .value on undefined object fixed.
  • if no argument provided, toHaveLength(), it is also considered as toHaveLengthZero together with toHaveLength(0).

image

image

  1. matcherArgument removal logic in eslint fixer is updated to remove the commas also, if there were multiple args in the matcher.

image

image

image

Why:

  1. toHaveLength method on expect is causing eslint to crash since no argument logic isn't handled properly in the prefer-in-document rule. If eslint is always watching and linting live, this issue is quite annoying.

Eslint plugin crush stack trace in VSCode:

image

Even after handling calling toHaveLength without any arguments, it won't work since toHaveLength expected argument is required. Thus, toHaveLength without any arguments considered as toHaveLength zero.

2. toHaveLength(0) and toHaveLength(1) on a RTL query replaced to proper toBeInTheDocument's. Logic is already applied to work with multiple arguments but, if there were multiple arguments after 0 or 1, commas aren't removed.

image

image

How:

  1. to fix eslint crashing issue (.value on undefined in matcherArguments[0].value): empty arg array is handled properly.

(matcherArguments.length === 0 || matcherArguments[0].value === 0)

  1. while removing arguments following lines added to remove commas also:
const sourceCode = context.getSourceCode();
const token = sourceCode.getTokenAfter(argument);
if (token.value === "," && token.type === "Punctuator") {
  // Remove commas if toHaveLength had more than one argument or a trailing comma
  operations.push(fixer.replaceText(token, ""));
}

Checklist:

  • Documentation
  • Tests
  • Ready to be merged

Fixes #277

@berkinanik berkinanik marked this pull request as ready for review November 8, 2022 19:12
Comment on lines 124 to 132
for (const argument of Array.from(matcherArguments)) {
const sourceCode = context.getSourceCode();
const token = sourceCode.getTokenAfter(argument);
if (token.value === "," && token.type === "Punctuator") {
// Remove commas if toHaveLength had more than one argument or a trailing comma
operations.push(fixer.replaceText(token, ""));
}
operations.push(fixer.remove(argument));
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

A better fix for this should be to use the starting location of the first argument and then the ending location of the last argument - that way we don't have to actually care about the commas at all.

Copy link
Contributor Author

@berkinanik berkinanik Nov 8, 2022

Choose a reason for hiding this comment

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

You might me right, I thought about it also (couldn't tried since I don't know the working mechanism of the eslint very well),
but I felt like comments would create problems if we handled it that way

Copy link
Collaborator

Choose a reason for hiding this comment

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

Problems with comments are not something we can really avoid - I'm happy to pick up getting the PR landed if you've not got the bandwidth :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

that would be great, thanks 🙂

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok so I was thinking about the handing for another situation (ESLint fixers are a bit mystic because of all the different ways code can be shaped 😂) - what you've got is good; it also turns out we're not handling trailing commas properly in eslint-plugin-jest either so thanks for being this to my attention!

@codecov
Copy link

codecov bot commented Nov 10, 2022

Codecov Report

Merging #276 (92ba227) into main (4299024) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main      #276   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           15        15           
  Lines          575       581    +6     
  Branches       165       167    +2     
=========================================
+ Hits           575       581    +6     
Impacted Files Coverage Δ
src/rules/prefer-in-document.js 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@G-Rath G-Rath changed the title fix(prefer-in-document): check toHaveLength has any args and remove commas in case of multiple args fix(prefer-in-document): handle toHaveLength without any arguments and with trailing commas Nov 10, 2022
@G-Rath G-Rath merged commit 5ab24d9 into testing-library:main Nov 10, 2022
@github-actions
Copy link

🎉 This PR is included in version 4.0.3 🎉

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
2 participants