Replaces invalid JS characters with underscore #109
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Ciao!
Since Laravel route names are strings and can contain virtually any character, I've added a regex that replaces characters that are invalid in JS with an underscore.
Currently, Wayfinder generates the JS file without verifying that the function name is valid. No exception is thrown, but the generated file contains invalid JavaScript. With this change, a route name like
example@testwould generate a function namedexample_test. The regex used/[^\p{L}\p{Nd}_$]/ucovers the most common cases, but it could be made more permissive if desired to fully comply with the ECMAScript specification.What do you think about this approach? Alternatively, we could throw an exception when encountering invalid characters instead of silently replacing them, to make users aware that their route names won't work with Wayfinder.