Skip to content

Commit

Permalink
fix(prefer-to-be): don't consider RegExp literals as toBe-able (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Oct 4, 2021
1 parent 546e837 commit 99b6d42
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/rules/__tests__/prefer-to-be.test.ts
Expand Up @@ -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: [
{
Expand Down
7 changes: 5 additions & 2 deletions src/rules/prefer-to-be.ts
Expand Up @@ -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]);
Expand Down

0 comments on commit 99b6d42

Please sign in to comment.