Skip to content

Commit

Permalink
perf: only build regexp ones for detecting expectations (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghusse authored Dec 19, 2024
1 parent 0f4193c commit 448650c
Showing 1 changed file with 16 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/rules/expect-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,6 @@ type Options = [
}
]

function matchesAssertFunctionName(
nodeName: string,
patterns: readonly string[]
): boolean {
return patterns.some(p =>
new RegExp(
`^${p
.split('.')
.map((x) => {
if (x === '**')
return '[_a-z\\d\\.]*'
return x.replace(/\*/gu, '[a-z\\d]*')
})
.join('\\.')}(\\.|$)`,
'ui'
).test(nodeName)
)
}

export default createEslintRule<Options, MESSAGE_ID>({
name: RULE_NAME,
meta: {
Expand Down Expand Up @@ -66,6 +46,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
const settings = parsePluginSettings(context.settings)

if (settings.typecheck) assertFunctionNames.push('expectTypeOf', 'assertType')
const assertFunctionRegexps = assertFunctionNames.map(buildPatternRegexp)

function checkCallExpression(nodes: TSESTree.Node[]) {
for (const node of nodes) {
Expand Down Expand Up @@ -103,7 +84,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

unchecked.push(node)
}
else if (matchesAssertFunctionName(name, assertFunctionNames)) {
else if (assertFunctionRegexps.some(p =>p.test(name))) {
checkCallExpression(context.sourceCode.getAncestors(node))
}
},
Expand All @@ -118,3 +99,17 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
}
})

function buildPatternRegexp(pattern: string): RegExp {
const parts = pattern.split('.').map((x) => {
if (x === '**')
return '[_a-z\\d\\.]*'

return x.replace(/\*/gu, '[a-z\\d]*')
})

return new RegExp(
`^${parts.join('\\.')}(\\.|$)`,
'ui'
)
}

0 comments on commit 448650c

Please sign in to comment.