Skip to content

Commit

Permalink
fix(require-hook): check variables are either const or declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Oct 16, 2021
1 parent 5278fcb commit cb52383
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/rules/__tests__/require-hook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,47 @@ ruleTester.run('require-hook', rule, {
},
],
},
{
code: dedent`
let consoleErrorSpy = jest.spyOn(console, 'error');
describe('when loading cities from the api', () => {
let consoleWarnSpy = jest.spyOn(console, 'warn');
});
`,
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
{
messageId: 'useHook',
line: 4,
column: 3,
},
],
},
{
code: "let consoleErrorSpy, consoleWarnSpy = jest.spyOn(console, 'error');",
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
],
},
{
code: "let consoleErrorSpy = jest.spyOn(console, 'error'), consoleWarnSpy;",
errors: [
{
messageId: 'useHook',
line: 1,
column: 1,
},
],
},
{
code: dedent`
import { database, isCity } from '../database';
Expand Down Expand Up @@ -236,6 +277,11 @@ ruleTester.run('require-hook', rule, {
line: 16,
column: 1,
},
{
messageId: 'useHook',
line: 31,
column: 3,
},
{
messageId: 'useHook',
line: 33,
Expand Down
7 changes: 7 additions & 0 deletions src/rules/require-hook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ const shouldBeInHook = (node: TSESTree.Node): boolean => {
return shouldBeInHook(node.expression);
case AST_NODE_TYPES.CallExpression:
return !isJestFnCall(node);
case AST_NODE_TYPES.VariableDeclaration: {
if (node.kind === 'const') {
return false;
}

return node.declarations.some(({ init }) => init !== null);
}

default:
return false;
Expand Down

0 comments on commit cb52383

Please sign in to comment.