-
-
Notifications
You must be signed in to change notification settings - Fork 135
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
Escape all regex special characters in identifiers #596
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Happy to merge, but I do have a question.
@@ -387,7 +387,7 @@ const getImportsAndExports = ( | |||
|
|||
const setRefs = (item: SerializableExport | SerializableExportMember) => { | |||
if (!item.symbol) return; | |||
for (const match of sourceFile.text.matchAll(new RegExp(item.identifier.replace(/\$/g, '\\$'), 'g'))) { | |||
for (const match of sourceFile.text.matchAll(new RegExp(item.identifier.replace(/[\\^$*+?.()|[\]{}]/g, '\\$&'), 'g'))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This targets (enum) members, perhaps it makes sense to only apply this to members at https://github.com/webpro/knip/blob/main/packages/knip/src/typescript/getImportsAndExports.ts#L43? Ideally we'd create the regex once and reuse it (I should've done that myself here too).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@webpro do you mean to make the escaping of special characters already when creating the serializableMember? I've just had a cursory look, and it looks like modifying the identifier
would have lot's of other effects, but perhaps adding another regex
property to the serializable members which does identifier->escape->regex and then we can use that pre-created regex here for the matchAll()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah ok I had another look, and I can't figure out what exactly you mean. I've moved the Regex into it's own file to be reused, also tried to search for anywhere in the code here a similiar regex was used, but couldn't find anywhere.
Let me know what you think!
3691e5b
to
9bf286f
Compare
|
Name | Link |
---|---|
🔨 Latest commit | 8e2a4d3 |
6531735
to
d2f304d
Compare
Rather than just escaping
$
, escape all regex special characters in any identifiers to make sure the RegExp that is created is valid.Used the list of characters to escape from the regex-escaping proposal
https://github.com/tc39/proposal-regex-escaping/blob/66d654ea6561ea064045c7ecf2da6870c892c9be/polyfill.js#L4
Closes #595