Skip to content

Commit 4fe0b65

Browse files
authored
Fix getUsageOfPattern to return unknown on unknown calls. (#249)
1 parent ac76e5c commit 4fe0b65

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/utils/get-usage-of-pattern.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,29 @@ function* iterateUsageOfPattern(
6767
} else if (ref.type === "unused") {
6868
// noop
6969
} else if (ref.type === "argument") {
70-
// It could be a call to a known method that uses a regexp (`match`, `matchAll`, `split`, `replace`, `replaceAll`, and `search`),
71-
// or it could use an unknown method, both of which are considered to have used a regexp.
72-
yield UsageOfPattern.whole
70+
if (
71+
ref.callExpression.arguments[0] === ref.node &&
72+
ref.callExpression.callee.type === "MemberExpression"
73+
) {
74+
const member = ref.callExpression.callee
75+
const propName: string | null = !member.computed
76+
? (member.property as Identifier).name
77+
: getStringIfConstant(context, member.property)
78+
if (
79+
propName === "match" ||
80+
propName === "matchAll" ||
81+
propName === "split" ||
82+
propName === "replace" ||
83+
propName === "replaceAll" ||
84+
propName === "search"
85+
) {
86+
yield UsageOfPattern.whole
87+
} else {
88+
yield UsageOfPattern.unknown
89+
}
90+
} else {
91+
yield UsageOfPattern.unknown
92+
}
7393
} else {
7494
yield UsageOfPattern.unknown
7595
}

tests/lib/utils/get-usage-of-pattern.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,21 @@ const TESTCASES: TestCase[] = [
232232
results: [UsageOfPattern.unknown, UsageOfPattern.whole],
233233
sourceType: "module",
234234
},
235+
{
236+
code: `foo(/[a-zA-Z]\\w*/)`,
237+
results: [UsageOfPattern.unknown],
238+
},
239+
{
240+
code: `foo({ pattern: /[a-zA-Z]\\w*/ })`,
241+
results: [UsageOfPattern.unknown],
242+
},
243+
{
244+
code: `
245+
const a = /a/
246+
'str'.replace(b, a/*unknown*/)
247+
`,
248+
results: [UsageOfPattern.unknown],
249+
},
235250
]
236251
describe("getUsageOfPattern", () => {
237252
for (const testCase of TESTCASES) {

0 commit comments

Comments
 (0)