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 global regex before match #11132

Merged
merged 1 commit into from Nov 30, 2022
Merged

fix: reset global regex before match #11132

merged 1 commit into from Nov 30, 2022

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Nov 30, 2022

Description

I noticed we're not properly resetting the global regex when running while-loop regex match, which means the result we could be missing regex captures every other run. (StackOverflow explanation)

Additional context

I'm not sure how we haven't got a bug report for this before 😬

MDN has notes about how lastIndex is set. I also find that using global regex for str.replace or str.match are not affected by .lastIndex, and it would also always reset .lastIndex. (Maybe that's how the bug appear less often for some regex)


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the Commit Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy bluwy added the p3-minor-bug An edge case that only affects very specific usage (priority) label Nov 30, 2022
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === importsRE.lastIndex) {
importsRE.lastIndex++
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you explain why this isnt needed any more?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is used to safe-guard zero-width matches, e.g. the regex would match "". But since importsRE is guaranteed to never do that (it always captures some text), this can be removed.

I think this part was copied from https://regex101.com generated output before.

@patak-dev patak-dev merged commit db8df14 into main Nov 30, 2022
@patak-dev patak-dev deleted the reset-global-regex branch November 30, 2022 12:41
bluwy added a commit that referenced this pull request Dec 5, 2022
patak-dev pushed a commit that referenced this pull request Dec 5, 2022
* fix: glob import parsing (#10949) (#11056)

closes #10949
closes #11051

* fix: import.meta.env and process.env undefined variable replacement (fix #8663) (#10958)

Co-authored-by: bluwy <bjornlu.dev@gmail.com>
fix #8663

* fix(esbuild): handle inline sourcemap option (#11120)

* fix(importGlob): preserve line count for sourcemap (#11122)

* fix: Dev SSR dep optimization + respect optimizeDeps.include (#11123)

* fix: reset global regex before match (#11132)

* chore: fix test

Co-authored-by: gtmnayan <50981692+gtm-nayan@users.noreply.github.com>
Co-authored-by: julienv3 <julienv3@gmail.com>
Co-authored-by: 翠 / green <green@sapphi.red>
futurGH pushed a commit to futurGH/vite that referenced this pull request Feb 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants