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(extract): escape special regex characters used in file-based routing systems #1984

Merged
merged 1 commit into from
Jul 29, 2024

Conversation

jjonesrs
Copy link
Contributor

Description

We recently migrated to TanStack Router, which uses file-based routing with several file naming conventions. The lingui extract --watch command stopped working on any routes with these special characters. However, lingui extract continued to work.

Debugging revealed that --watch sets options.files, and these paths are formed into a regular expression. If any of these paths contains a character with special regex meaning, like $, then paths won't be correctly filtered.

if (options.files) {
    options.files = options.files.map((p) =>normalize(p, false))
    const regex = new RegExp(options.files.join("|"), "i")
    paths = paths.filter((path: string) => regex.test(path))
}

To fix this, I created a new utility that escapes characters allowed in file paths, but with special regex meaning. I'm not sure if I captured all possible characters used in file-based routing conventions, or if I over specified too many.

I added tests for both the utility itself and the extractor's collect function. If you remove makePathRegexSafe from collect, you should see components C, D, and E removed from the snapshot because the extractor fails to handle them.

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Examples update

Checklist

  • I have read the CONTRIBUTING and CODE_OF_CONDUCT docs
  • I have added tests that prove my fix is effective or that my feature works
  • I have added the necessary documentation (if appropriate)

…ing systems when files are passed in options
Copy link

vercel bot commented Jul 24, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
js-lingui ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 24, 2024 6:13am

Copy link

size-limit report 📦

Path Size
./packages/core/dist/index.mjs 2.88 KB (0%)
./packages/detect-locale/dist/index.mjs 723 B (0%)
./packages/react/dist/index.mjs 1.67 KB (0%)
./packages/remote-loader/dist/index.mjs 7.26 KB (0%)

@timofei-iatsenko timofei-iatsenko self-requested a review July 29, 2024 08:51
Copy link
Collaborator

@timofei-iatsenko timofei-iatsenko left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for your work!

@andrii-bodnar andrii-bodnar merged commit fd92d20 into lingui:main Jul 29, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants