Skip to content

Commit 89ab1a0

Browse files
authored
fix: use correct scope for checking references (#1107)
1 parent 7dd707f commit 89ab1a0

29 files changed

+221
-66
lines changed

src/rules/__tests__/no-focused-tests.test.ts

+90
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { TSESLint } from '@typescript-eslint/utils';
2+
import dedent from 'dedent';
23
import rule from '../no-focused-tests';
34
import { espreeParser } from './test-utils';
45

@@ -319,3 +320,92 @@ ruleTester.run('no-focused-tests', rule, {
319320
},
320321
],
321322
});
323+
324+
ruleTester.run('no-focused-tests (with imports)', rule, {
325+
valid: [
326+
{
327+
code: dedent`
328+
import { fdescribe as describeJustThis } from '@jest/globals';
329+
330+
describeJustThis()
331+
`,
332+
parserOptions: { sourceType: 'module' },
333+
},
334+
],
335+
336+
invalid: [
337+
{
338+
code: dedent`
339+
const { describe } = require('@jest/globals');
340+
341+
describe.only()
342+
`,
343+
errors: [
344+
{
345+
messageId: 'focusedTest',
346+
column: 10,
347+
line: 3,
348+
suggestions: [
349+
{
350+
messageId: 'suggestRemoveFocus',
351+
output: dedent`
352+
const { describe } = require('@jest/globals');
353+
354+
describe()
355+
`,
356+
},
357+
],
358+
},
359+
],
360+
},
361+
{
362+
code: dedent`
363+
import { describe as describeThis } from '@jest/globals';
364+
365+
describeThis.only()
366+
`,
367+
parserOptions: { sourceType: 'module' },
368+
errors: [
369+
{
370+
messageId: 'focusedTest',
371+
column: 14,
372+
line: 3,
373+
suggestions: [
374+
{
375+
messageId: 'suggestRemoveFocus',
376+
output: dedent`
377+
import { describe as describeThis } from '@jest/globals';
378+
379+
describeThis()
380+
`,
381+
},
382+
],
383+
},
384+
],
385+
},
386+
{
387+
code: dedent`
388+
const { fdescribe } = require('@jest/globals');
389+
390+
fdescribe()
391+
`,
392+
errors: [
393+
{
394+
messageId: 'focusedTest',
395+
column: 1,
396+
line: 3,
397+
suggestions: [
398+
{
399+
messageId: 'suggestRemoveFocus',
400+
output: dedent`
401+
const { fdescribe } = require('@jest/globals');
402+
403+
describe()
404+
`,
405+
},
406+
],
407+
},
408+
],
409+
},
410+
],
411+
});

src/rules/__tests__/prefer-snapshot-hint.test.ts

+42
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,21 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
339339
`,
340340
options: ['multi'],
341341
},
342+
{
343+
code: dedent`
344+
import { it as itIs } from '@jest/globals';
345+
346+
it('is true', () => {
347+
expect(1).toMatchSnapshot();
348+
});
349+
350+
itIs('false', () => {
351+
expect(1).toMatchSnapshot();
352+
});
353+
`,
354+
options: ['multi'],
355+
parserOptions: { sourceType: 'module' },
356+
},
342357
{
343358
code: dedent`
344359
const myReusableTestBody = (value, snapshotHint) => {
@@ -731,6 +746,33 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
731746
},
732747
],
733748
},
749+
{
750+
code: dedent`
751+
import { describe as context, it as itIs } from '@jest/globals';
752+
753+
describe('my tests', () => {
754+
it('is true', () => {
755+
expect(1).toMatchSnapshot();
756+
});
757+
758+
context('more tests', () => {
759+
itIs('false', () => {
760+
expect(2).toMatchSnapshot();
761+
expect(2).toMatchSnapshot('hello world');
762+
});
763+
});
764+
});
765+
`,
766+
options: ['multi'],
767+
parserOptions: { sourceType: 'module' },
768+
errors: [
769+
{
770+
messageId: 'missingHint',
771+
column: 17,
772+
line: 10,
773+
},
774+
],
775+
},
734776
{
735777
code: dedent`
736778
const myReusableTestBody = (value, snapshotHint) => {

src/rules/__tests__/valid-describe-callback.test.ts

+9
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,15 @@ ruleTester.run('valid-describe-callback', rule, {
114114
code: 'fdescribe("foo", async function () {})',
115115
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 18 }],
116116
},
117+
{
118+
code: dedent`
119+
import { fdescribe } from '@jest/globals';
120+
121+
fdescribe("foo", async function () {})
122+
`,
123+
parserOptions: { sourceType: 'module' },
124+
errors: [{ messageId: 'noAsyncDescribeCallback', line: 3, column: 18 }],
125+
},
117126
{
118127
code: 'describe.only("foo", async function () {})',
119128
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 22 }],

src/rules/__tests__/valid-expect-in-promise.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -1387,6 +1387,21 @@ ruleTester.run('valid-expect-in-promise', rule, {
13871387
{ column: 16, endColumn: 5, messageId: 'expectInFloatingPromise' },
13881388
],
13891389
},
1390+
{
1391+
code: dedent`
1392+
import { test } from '@jest/globals';
1393+
1394+
test('later return', async () => {
1395+
const x = 1, promise = something().then(value => {
1396+
expect(value).toBe('red');
1397+
});
1398+
});
1399+
`,
1400+
parserOptions: { sourceType: 'module' },
1401+
errors: [
1402+
{ column: 16, endColumn: 5, messageId: 'expectInFloatingPromise' },
1403+
],
1404+
},
13901405
{
13911406
code: dedent`
13921407
it('promise test', () => {

src/rules/consistent-test-it.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ export default createRule<
6464
},
6565
defaultOptions: [{ fn: TestCaseName.test, withinDescribe: TestCaseName.it }],
6666
create(context) {
67-
const scope = context.getScope();
6867
const configObj = context.options[0] || {};
6968
const testKeyword = configObj.fn || TestCaseName.test;
7069
const testKeywordWithinDescribe =
@@ -74,6 +73,7 @@ export default createRule<
7473

7574
return {
7675
CallExpression(node: TSESTree.CallExpression) {
76+
const scope = context.getScope();
7777
const nodeName = getNodeName(node.callee);
7878

7979
if (!nodeName) {
@@ -124,7 +124,7 @@ export default createRule<
124124
}
125125
},
126126
'CallExpression:exit'(node) {
127-
if (isDescribeCall(node, scope)) {
127+
if (isDescribeCall(node, context.getScope())) {
128128
describeNestingLevel--;
129129
}
130130
},

src/rules/expect-expect.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export default createRule<
8282
context,
8383
[{ assertFunctionNames = ['expect'], additionalTestBlockFunctions = [] }],
8484
) {
85-
const scope = context.getScope();
8685
const unchecked: TSESTree.CallExpression[] = [];
8786

8887
function checkCallExpressionUsed(nodes: TSESTree.Node[]) {
@@ -97,7 +96,7 @@ export default createRule<
9796
const testCallExpressions =
9897
getTestCallExpressionsFromDeclaredVariables(
9998
declaredVariables,
100-
scope,
99+
context.getScope(),
101100
);
102101

103102
checkCallExpressionUsed(testCallExpressions);
@@ -115,7 +114,7 @@ export default createRule<
115114
const name = getNodeName(node.callee) ?? '';
116115

117116
if (
118-
isTestCaseCall(node, scope) ||
117+
isTestCaseCall(node, context.getScope()) ||
119118
additionalTestBlockFunctions.includes(name)
120119
) {
121120
if (

src/rules/max-nested-describe.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ export default createRule({
2929
},
3030
defaultOptions: [{ max: 5 }],
3131
create(context, [{ max }]) {
32-
const scope = context.getScope();
3332
const describeCallbackStack: number[] = [];
3433

3534
function pushDescribeCallback(
@@ -39,7 +38,7 @@ export default createRule({
3938

4039
if (
4140
parent?.type !== AST_NODE_TYPES.CallExpression ||
42-
!isDescribeCall(parent, scope)
41+
!isDescribeCall(parent, context.getScope())
4342
) {
4443
return;
4544
}
@@ -62,7 +61,7 @@ export default createRule({
6261

6362
if (
6463
parent?.type === AST_NODE_TYPES.CallExpression &&
65-
isDescribeCall(parent, scope)
64+
isDescribeCall(parent, context.getScope())
6665
) {
6766
describeCallbackStack.pop();
6867
}

src/rules/no-conditional-expect.ts

+3-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export default createRule({
3030
},
3131
defaultOptions: [],
3232
create(context) {
33-
const scope = context.getScope();
3433
let conditionalDepth = 0;
3534
let inTestCase = false;
3635
let inPromiseCatch = false;
@@ -43,15 +42,15 @@ export default createRule({
4342
const declaredVariables = context.getDeclaredVariables(node);
4443
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
4544
declaredVariables,
46-
scope,
45+
context.getScope(),
4746
);
4847

4948
if (testCallExpressions.length > 0) {
5049
inTestCase = true;
5150
}
5251
},
5352
CallExpression(node: TSESTree.CallExpression) {
54-
if (isTestCaseCall(node, scope)) {
53+
if (isTestCaseCall(node, context.getScope())) {
5554
inTestCase = true;
5655
}
5756

@@ -74,7 +73,7 @@ export default createRule({
7473
}
7574
},
7675
'CallExpression:exit'(node) {
77-
if (isTestCaseCall(node, scope)) {
76+
if (isTestCaseCall(node, context.getScope())) {
7877
inTestCase = false;
7978
}
8079

src/rules/no-conditional-in-test.ts

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export default createRule({
1717
},
1818
defaultOptions: [],
1919
create(context) {
20-
const scope = context.getScope();
2120
let inTestCase = false;
2221

2322
const maybeReportConditional = (node: TSESTree.Node) => {
@@ -31,12 +30,12 @@ export default createRule({
3130

3231
return {
3332
CallExpression(node: TSESTree.CallExpression) {
34-
if (isTestCaseCall(node, scope)) {
33+
if (isTestCaseCall(node, context.getScope())) {
3534
inTestCase = true;
3635
}
3736
},
3837
'CallExpression:exit'(node) {
39-
if (isTestCaseCall(node, scope)) {
38+
if (isTestCaseCall(node, context.getScope())) {
4039
inTestCase = false;
4140
}
4241
},

src/rules/no-done-callback.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ export default createRule({
4949
},
5050
defaultOptions: [],
5151
create(context) {
52-
const scope = context.getScope();
53-
5452
return {
5553
CallExpression(node) {
5654
// done is the second argument for it.each, not the first
@@ -66,7 +64,7 @@ export default createRule({
6664
return;
6765
}
6866

69-
const callback = findCallbackArg(node, isJestEach, scope);
67+
const callback = findCallbackArg(node, isJestEach, context.getScope());
7068
const callbackArgIndex = Number(isJestEach);
7169

7270
if (

src/rules/no-duplicate-hooks.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ export default createRule({
2323
},
2424
defaultOptions: [],
2525
create(context) {
26-
const scope = context.getScope();
2726
const hookContexts = [newHookContext()];
2827

2928
return {
3029
CallExpression(node) {
30+
const scope = context.getScope();
31+
3132
if (isDescribeCall(node, scope)) {
3233
hookContexts.push(newHookContext());
3334
}
@@ -46,7 +47,7 @@ export default createRule({
4647
}
4748
},
4849
'CallExpression:exit'(node) {
49-
if (isDescribeCall(node, scope)) {
50+
if (isDescribeCall(node, context.getScope())) {
5051
hookContexts.pop();
5152
}
5253
},

src/rules/no-export.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ export default createRule({
1717
},
1818
defaultOptions: [],
1919
create(context) {
20-
const scope = context.getScope();
2120
const exportNodes: Array<
2221
| TSESTree.ExportNamedDeclaration
2322
| TSESTree.ExportDefaultDeclaration
@@ -35,7 +34,7 @@ export default createRule({
3534
},
3635

3736
CallExpression(node) {
38-
if (isTestCaseCall(node, scope)) {
37+
if (isTestCaseCall(node, context.getScope())) {
3938
hasTestCase = true;
4039
}
4140
},

src/rules/no-focused-tests.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ export default createRule({
5353
},
5454
defaultOptions: [],
5555
create(context) {
56-
const scope = context.getScope();
57-
5856
return {
5957
CallExpression(node) {
58+
const scope = context.getScope();
59+
6060
if (!isDescribeCall(node, scope) && !isTestCaseCall(node, scope)) {
6161
return;
6262
}

0 commit comments

Comments
 (0)