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: reset regexg to prevent regexg with global flags to not match every element #1116

Merged
merged 2 commits into from
Mar 23, 2022

Conversation

timdeschryver
Copy link
Member

@timdeschryver timdeschryver commented Mar 22, 2022

What: Resolves #1115

Why:

The instance of a regex that's using the global flag hold a lastIndex state.
This might give false positive results.

For example:

const g = /foo/g
g.test('foo') // |> true
g.test('foo') // |> false because lastIndex is 3
g.test('foo') // |> true because lastIndex is 0 again
g.test('foo') // |> false because lastIndex is 3

How:

Added a reproduction test case.
Recreate a new regex instance from the original regex.

An alternative would be to reset the lastIndex to 0 after the check.

Checklist:

  • Documentation added to the
    docs site
  • Tests
  • TypeScript definitions updated
  • Ready to be merged

src/matches.ts Outdated Show resolved Hide resolved
@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 22, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 714007e:

Sandbox Source
react-testing-library-examples Configuration
react-testing-library demo Issue #1115

@codecov
Copy link

codecov bot commented Mar 22, 2022

Codecov Report

Merging #1116 (714007e) into main (90fba31) will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##              main     #1116   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           25        25           
  Lines          955       961    +6     
  Branches       311       312    +1     
=========================================
+ Hits           955       961    +6     
Flag Coverage Δ
node-12 ?
node-14 ?
node-16 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/matches.ts 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 90fba31...714007e. Read the comment docs.

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

This is fine but let's make sure we have a good follow-up strategy in case this doesn't work

src/__tests__/matches.js Outdated Show resolved Hide resolved
src/matches.ts Outdated Show resolved Hide resolved
eps1lon
eps1lon previously approved these changes Mar 23, 2022
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

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

Looks nice, thanks!

Blame me if somebody complains about noisy warnings added in a patch release 😄

@eps1lon
Copy link
Member

eps1lon commented Mar 23, 2022

@timdeschryver I think you need to push an empty commit or rebase and drop the merge commit. Codecov may not like merge commits which is a result of updating the branch from the GitHub UI.

@timdeschryver
Copy link
Member Author

@eps1lon Done.
No problem, it was a team effort so we have me to blame aswell
This change had a bit more catches than forseen 😅

@eps1lon eps1lon changed the title fix: recreate regex to prevent regex with global flags fix: reset regexg to prevent regexg with global flags to not match every element Mar 23, 2022
@eps1lon eps1lon merged commit 90d420d into main Mar 23, 2022
@eps1lon eps1lon deleted the global-regexp branch March 23, 2022 21:52
@github-actions
Copy link

🎉 This PR is included in version 8.11.4 🎉

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.

getAllByRole witha regex matcher is returning the wrong count
2 participants