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(functions): reset RegExp.lastIndex to zero when using cached RegExp objects #2079

Merged
merged 1 commit into from Mar 7, 2022

Conversation

pavelkornev
Copy link
Contributor

We have noticed that when g marker is used the Spectral becomes non-deterministic due to RegExp cache in pattern function. So, in our case, validator returns different results on every second run (please see added tests).

More details on RegExp.lastIndex:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex
https://stackoverflow.com/a/13587801/280989

Quick example:

const rg = new RegExp("^[\\w]+$", "gi");
rg.exec("123"); // ['123', index: 0, input: '123', groups: undefined]
rg.exec("123"); // null
rg.exec("123"); // ['123', index: 0, input: '123', groups: undefined]

Checklist

  • Tests added / updated
  • Docs added / updated

Does this PR introduce a breaking change?

  • Yes
  • No

@P0lip P0lip changed the title fix: Reset RegExp.lastIndex to zero when using cached RegExp objects fix(functions): reset RegExp.lastIndex to zero when using cached RegExp objects Mar 7, 2022
Copy link
Contributor

@P0lip P0lip left a comment

Choose a reason for hiding this comment

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

Nice find!
Thanks a lot.

@P0lip
Copy link
Contributor

P0lip commented Mar 7, 2022

I pushed a new commit so that CI starts. It got stuck for some reason.

@P0lip P0lip enabled auto-merge (squash) March 7, 2022 13:26
@P0lip P0lip merged commit 4839527 into stoplightio:develop Mar 7, 2022
stoplight-bot pushed a commit that referenced this pull request Mar 8, 2022
# [@stoplight/spectral-functions-v1.6.1](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-functions-v1.6.0...@stoplight/spectral-functions-v1.6.1) (2022-03-08)

### Bug Fixes

* **functions:** reset RegExp.lastIndex to zero when using cached RegExp objects ([#2079](#2079)) ([4839527](4839527))
@stoplight-bot
Copy link
Collaborator

🎉 This PR is included in version @stoplight/spectral-functions-v1.6.1 🎉

The release is available on npm package (@latest dist-tag)

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.

None yet

3 participants