Skip to content

Commit cb52383

Browse files
committed
fix(require-hook): check variables are either const or declarations
1 parent 5278fcb commit cb52383

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

src/rules/__tests__/require-hook.test.ts

+46
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,47 @@ ruleTester.run('require-hook', rule, {
176176
},
177177
],
178178
},
179+
{
180+
code: dedent`
181+
let consoleErrorSpy = jest.spyOn(console, 'error');
182+
183+
describe('when loading cities from the api', () => {
184+
let consoleWarnSpy = jest.spyOn(console, 'warn');
185+
});
186+
`,
187+
errors: [
188+
{
189+
messageId: 'useHook',
190+
line: 1,
191+
column: 1,
192+
},
193+
{
194+
messageId: 'useHook',
195+
line: 4,
196+
column: 3,
197+
},
198+
],
199+
},
200+
{
201+
code: "let consoleErrorSpy, consoleWarnSpy = jest.spyOn(console, 'error');",
202+
errors: [
203+
{
204+
messageId: 'useHook',
205+
line: 1,
206+
column: 1,
207+
},
208+
],
209+
},
210+
{
211+
code: "let consoleErrorSpy = jest.spyOn(console, 'error'), consoleWarnSpy;",
212+
errors: [
213+
{
214+
messageId: 'useHook',
215+
line: 1,
216+
column: 1,
217+
},
218+
],
219+
},
179220
{
180221
code: dedent`
181222
import { database, isCity } from '../database';
@@ -236,6 +277,11 @@ ruleTester.run('require-hook', rule, {
236277
line: 16,
237278
column: 1,
238279
},
280+
{
281+
messageId: 'useHook',
282+
line: 31,
283+
column: 3,
284+
},
239285
{
240286
messageId: 'useHook',
241287
line: 33,

src/rules/require-hook.ts

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const shouldBeInHook = (node: TSESTree.Node): boolean => {
2525
return shouldBeInHook(node.expression);
2626
case AST_NODE_TYPES.CallExpression:
2727
return !isJestFnCall(node);
28+
case AST_NODE_TYPES.VariableDeclaration: {
29+
if (node.kind === 'const') {
30+
return false;
31+
}
32+
33+
return node.declarations.some(({ init }) => init !== null);
34+
}
2835

2936
default:
3037
return false;

0 commit comments

Comments
 (0)