Skip to content

Commit f3feb11

Browse files
committed
Address review comments
1 parent 8415f93 commit f3feb11

2 files changed

Lines changed: 14 additions & 3 deletions

File tree

eslint-rules/eslint-prefer-assert-match.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@ function buildReplacementParts (match, sourceCode) {
176176
case 'test': {
177177
const stringNode = arg
178178
const regexNode = callee.object
179-
return { stringNode, regexText: sourceCode.getText(regexNode), autofixable: true }
179+
if (isRegexLiteral(regexNode)) {
180+
return { stringNode, regexText: sourceCode.getText(regexNode), autofixable: true }
181+
}
182+
return { stringNode, regexText: null, autofixable: false }
180183
}
181184

182185
default:

eslint-rules/eslint-prefer-assert-match.test.mjs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,20 @@ ruleTester.run('eslint-prefer-assert-match', /** @type {import('eslint').Rule.Ru
7171
output: "assert.doesNotMatch(x, /foo/, 'should not match')",
7272
errors: [{ messageId: 'preferDoesNotMatch' }],
7373
},
74-
// RegExp identifier (non-literal) is still fixable: just shuffles args.
74+
// Non-regex-literal receivers of `.test()` are reported but not auto-fixed:
75+
// the receiver might not be a RegExp (e.g. a Joi schema, AJV instance, or
76+
// any other helper with a `.test()` method), so blindly rewriting to
77+
// `assert.match(x, re)` could throw `TypeError: regexp must be a RegExp`.
7578
{
7679
code: 'assert.ok(re.test(x))',
77-
output: 'assert.match(x, re)',
80+
output: null,
7881
errors: [{ messageId: 'preferMatch' }],
7982
},
83+
{
84+
code: 'assert.ok(predicate.test(value))',
85+
output: null,
86+
errors: [{ messageId: 'preferMatch', data: { method: 'test' } }],
87+
},
8088

8189
// ── String.prototype.match() ──────────────────────────────────────────
8290
{

0 commit comments

Comments
 (0)