Skip to content

Commit

Permalink
Escape all regex special characters in identifiers (fix #595)
Browse files Browse the repository at this point in the history
  • Loading branch information
oBusk committed May 30, 2024
1 parent 87850ea commit 8e2a4d3
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion packages/knip/src/typescript/getImportsAndExports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ const getImportsAndExports = (
const setRefs = (item: SerializableExport | SerializableExportMember) => {
if (!item.symbol) return;
const symbols = new Set<ts.Symbol>();
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'))) {
const isDeclaration = match.index === item.pos || match.index === item.pos + 1; // off-by-one from `stripQuotes`
if (!isDeclaration) {
// @ts-expect-error ts.getTokenAtPosition is internal fn
Expand Down

0 comments on commit 8e2a4d3

Please sign in to comment.