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

Escape all regex special characters in identifiers (fix #595) #596

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

oBusk
Copy link

@oBusk oBusk commented Apr 19, 2024

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

Copy link
Owner

@webpro webpro left a 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'))) {
Copy link
Owner

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).

@webpro webpro force-pushed the main branch 6 times, most recently from 3691e5b to 9bf286f Compare April 26, 2024 10:01
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.

None yet

2 participants