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(waku/router): regex in file replacement to correctly handle all matches (fs-router) #678

Merged
merged 1 commit into from
Apr 27, 2024

Conversation

daanlenaerts
Copy link
Contributor

The current fs-router regex, /(^|\/|\\)_([^/]+)_(\/|\\|\.)/g, which is ran during build only replaces the first occurrence of underscores in scenarios where multiple underscore-surrounded segments are separated by single slashes. For example, in the string "app/_propertyId_/_surveyPartKey_.js", only _propertyId_ was being replaced, but _surveyPartKey_ was left unchanged due to the / being consumed during the first match.

This can easily be checked by running the following code:

const file = "app/_propertyId_/_surveyPartKey_.js";
console.log(file.replace(/(^|\/|\\)_([^/]+)_(\/|\\|\.)/g, '$1[$2]$3'));
// app/[propertyId]/_surveyPartKey_.js

Solution

The regex has been updated to /(?<=^|\/|\\)_([^/]+)_(?=\/|\\|\.)/g. This uses positive lookbehind (?<=^|\/|\\) and lookahead (?=\/|\\|\.) assertions to check for preceding and succeeding characters without consuming them. This allows every instance of the pattern to be replaced, regardless of their position in the string.

Copy link

vercel bot commented Apr 27, 2024

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

Name Status Preview Updated (UTC)
waku ✅ Ready (Inspect) Visit Preview Apr 27, 2024 11:45am

Copy link

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.

Copy link
Owner

@dai-shi dai-shi left a comment

Choose a reason for hiding this comment

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

Thanks so much for the fix!

@@ -41,7 +41,7 @@ export function fsRouter(
return [];
}
// HACK: replace "_slug_" to "[slug]" for build
file = file.replace(/(^|\/|\\)_([^/]+)_(\/|\\|\.)/g, '$1[$2]$3');
file = file.replace(/(?<=^|\/|\\)_([^/]+)_(?=\/|\\|\.)/g, '[$1]');
Copy link
Owner

Choose a reason for hiding this comment

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

Wow, nice. I should improve my regex skill.

(We need to avoid this HACK, but if it's not soon, we should probably extract the logic into a function and add a test for it.)

@dai-shi dai-shi changed the title Update regex in file replacement to correctly handle all matches fix(waku/router): regex in file replacement to correctly handle all matches (fs-router) Apr 27, 2024
@dai-shi dai-shi merged commit 61c0a53 into dai-shi:main Apr 27, 2024
28 checks passed
@dai-shi dai-shi mentioned this pull request Apr 27, 2024
@daanlenaerts
Copy link
Contributor Author

No worries, thanks for merging it!

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.

2 participants