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

New rule: no-global-regex-flag-to-query #559

Closed
timdeschryver opened this issue Mar 26, 2022 · 3 comments · Fixed by #560
Closed

New rule: no-global-regex-flag-to-query #559

timdeschryver opened this issue Mar 26, 2022 · 3 comments · Fixed by #560
Labels
new rule New rule to be included in the plugin released

Comments

@timdeschryver
Copy link
Member

Name for new rule

no-global-regex-flag-to-query

Description of the new rule

The rule would warn when a user uses the global regex flag to search for text/name.
A regex that uses the global flag holds the lastIndex state when it found a match, due to this it could be that a new match can't be found.

See testing-library/dom-testing-library#1116 for the fix within DTL, where we added a log when we detect this.

Testing Library feature

Queries that use a regex

Testing Library framework(s)

All

What category of rule is this?

Suggests an alternate way of doing something

Optional: other category of rule

No response

Code examples

screen.getAllByText(/hello world/g)
screen.findByRole('button', {name: /log in/ig})

Anything else?

No response

Do you want to submit a pull request to make the new rule?

Yes

@timdeschryver timdeschryver added the new rule New rule to be included in the plugin label Mar 26, 2022
@Belco90
Copy link
Member

Belco90 commented Mar 27, 2022

Hey @timdeschryver! This is a great idea, I read your explanation on Twitter the other day and was a TIL for me 🤯.

Let me know if you need help implementing this!

@Belco90
Copy link
Member

Belco90 commented Mar 27, 2022

Btw, if this has to be applied to every Testing Library util, you could just select all CallExpression and check if they are related to Testing Library with isTestingLibraryUtil, and then, if so, check the args given. Just an idea!

Belco90 pushed a commit that referenced this issue Mar 31, 2022
* feat: add no-global-regex-flag-in-query

* refactor: review feedback

* feat: add fixer

* test: add error details

* test: add within cases

* refactor: use getDeepestIdentifierNode

* refactor: review feedback

Closes #559
@github-actions
Copy link

🎉 This issue has been resolved in version 5.2.0 🎉

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
new rule New rule to be included in the plugin released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants