From 99b6d429e697d60645b4bc64ee4ae34d7016118c Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 4 Oct 2021 17:07:04 +1300 Subject: [PATCH] fix(prefer-to-be): don't consider RegExp literals as `toBe`-able (#922) --- src/rules/__tests__/prefer-to-be.test.ts | 2 ++ src/rules/prefer-to-be.ts | 7 +++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/rules/__tests__/prefer-to-be.test.ts b/src/rules/__tests__/prefer-to-be.test.ts index 2cf92e69b..bd9630d84 100644 --- a/src/rules/__tests__/prefer-to-be.test.ts +++ b/src/rules/__tests__/prefer-to-be.test.ts @@ -14,6 +14,8 @@ ruleTester.run('prefer-to-be', rule, { 'expect(value).toMatchSnapshot();', "expect(catchError()).toStrictEqual({ message: 'oh noes!' })", 'expect("something");', + 'expect(token).toStrictEqual(/[abc]+/g);', + "expect(token).toStrictEqual(new RegExp('[abc]+', 'g'));", ], invalid: [ { diff --git a/src/rules/prefer-to-be.ts b/src/rules/prefer-to-be.ts index 687d774b8..a67fdb54b 100644 --- a/src/rules/prefer-to-be.ts +++ b/src/rules/prefer-to-be.ts @@ -35,8 +35,11 @@ const isFirstArgumentIdentifier = ( name: string, ) => isIdentifier(getFirstArgument(matcher), name); -const isPrimitiveLiteral = (matcher: ParsedEqualityMatcherCall) => - getFirstArgument(matcher).type === AST_NODE_TYPES.Literal; +const isPrimitiveLiteral = (matcher: ParsedEqualityMatcherCall): boolean => { + const firstArg = getFirstArgument(matcher); + + return firstArg.type === AST_NODE_TYPES.Literal && !('regex' in firstArg); +}; const getFirstArgument = (matcher: ParsedEqualityMatcherCall) => { return followTypeAssertionChain(matcher.arguments[0]);