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

Fix getUsageOfPattern to return unknown on unknown calls. #249

Merged
merged 1 commit into from
Jun 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions lib/utils/get-usage-of-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,29 @@ function* iterateUsageOfPattern(
} else if (ref.type === "unused") {
// noop
} else if (ref.type === "argument") {
// It could be a call to a known method that uses a regexp (`match`, `matchAll`, `split`, `replace`, `replaceAll`, and `search`),
// or it could use an unknown method, both of which are considered to have used a regexp.
yield UsageOfPattern.whole
if (
ref.callExpression.arguments[0] === ref.node &&
ref.callExpression.callee.type === "MemberExpression"
) {
const member = ref.callExpression.callee
const propName: string | null = !member.computed
? (member.property as Identifier).name
: getStringIfConstant(context, member.property)
if (
propName === "match" ||
propName === "matchAll" ||
propName === "split" ||
propName === "replace" ||
propName === "replaceAll" ||
propName === "search"
) {
yield UsageOfPattern.whole
} else {
yield UsageOfPattern.unknown
}
} else {
yield UsageOfPattern.unknown
}
} else {
yield UsageOfPattern.unknown
}
Expand Down
15 changes: 15 additions & 0 deletions tests/lib/utils/get-usage-of-pattern.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,21 @@ const TESTCASES: TestCase[] = [
results: [UsageOfPattern.unknown, UsageOfPattern.whole],
sourceType: "module",
},
{
code: `foo(/[a-zA-Z]\\w*/)`,
results: [UsageOfPattern.unknown],
},
{
code: `foo({ pattern: /[a-zA-Z]\\w*/ })`,
results: [UsageOfPattern.unknown],
},
{
code: `
const a = /a/
'str'.replace(b, a/*unknown*/)
`,
results: [UsageOfPattern.unknown],
},
]
describe("getUsageOfPattern", () => {
for (const testCase of TESTCASES) {
Expand Down