Skip to content

Commit 9204a51

Browse files
authored
fix: replace use of deprecated methods (#1453)
* fix: replace usage of deprecated `Context#getScope` * chore: define new `SourceCode` methods * fix: replace usage of deprecated `context` methods * fix: replace usage of deprecated `Context#getSourceCode` * fix: replace usage of deprecated `Context#getFilename` * test: ignore coverage on `context` helpers
1 parent 0f8a33a commit 9204a51

18 files changed

+131
-25
lines changed

src/index.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readdirSync } from 'fs';
22
import { join, parse } from 'path';
3-
import type { TSESLint } from '@typescript-eslint/utils';
3+
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
44
import {
55
name as packageName,
66
version as packageVersion,
@@ -19,6 +19,32 @@ declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
1919
}
2020
}
2121

22+
declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
23+
export interface SourceCode {
24+
/**
25+
* Returns the scope of the given node.
26+
* This information can be used track references to variables.
27+
* @since 8.37.0
28+
*/
29+
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
30+
/**
31+
* Returns an array of the ancestors of the given node, starting at
32+
* the root of the AST and continuing through the direct parent of the current node.
33+
* This array does not include the currently-traversed node itself.
34+
* @since 8.38.0
35+
*/
36+
getAncestors(node: TSESTree.Node): TSESTree.Node[];
37+
/**
38+
* Returns a list of variables declared by the given node.
39+
* This information can be used to track references to variables.
40+
* @since 8.38.0
41+
*/
42+
getDeclaredVariables(
43+
node: TSESTree.Node,
44+
): readonly TSESLint.Scope.Variable[];
45+
}
46+
}
47+
2248
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
2349
/* istanbul ignore next */
2450
const interopRequireDefault = (obj: any): { default: any } =>

src/rules/expect-expect.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
77
import {
88
createRule,
9+
getAncestors,
10+
getDeclaredVariables,
911
getNodeName,
1012
getTestCallExpressionsFromDeclaredVariables,
1113
isSupportedAccessor,
@@ -94,7 +96,7 @@ export default createRule<
9496
: -1;
9597

9698
if (node.type === AST_NODE_TYPES.FunctionDeclaration) {
97-
const declaredVariables = context.getDeclaredVariables(node);
99+
const declaredVariables = getDeclaredVariables(context, node);
98100
const testCallExpressions =
99101
getTestCallExpressionsFromDeclaredVariables(
100102
declaredVariables,
@@ -129,7 +131,7 @@ export default createRule<
129131
unchecked.push(node);
130132
} else if (matchesAssertFunctionName(name, assertFunctionNames)) {
131133
// Return early in case of nested `it` statements.
132-
checkCallExpressionUsed(context.getAncestors());
134+
checkCallExpressionUsed(getAncestors(context, node));
133135
}
134136
},
135137
'Program:exit'() {

src/rules/no-commented-out-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { TSESTree } from '@typescript-eslint/utils';
2-
import { createRule } from './utils';
2+
import { createRule, getSourceCode } from './utils';
33

44
function hasTests(node: TSESTree.Comment) {
55
return /^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/mu.test(
@@ -23,7 +23,7 @@ export default createRule({
2323
},
2424
defaultOptions: [],
2525
create(context) {
26-
const sourceCode = context.getSourceCode();
26+
const sourceCode = getSourceCode(context);
2727

2828
function checkNode(node: TSESTree.Comment) {
2929
if (!hasTests(node)) {

src/rules/no-conditional-expect.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
22
import {
33
type KnownCallExpression,
44
createRule,
5+
getDeclaredVariables,
56
getTestCallExpressionsFromDeclaredVariables,
67
isSupportedAccessor,
78
isTypeOfJestFnCall,
@@ -39,7 +40,7 @@ export default createRule({
3940

4041
return {
4142
FunctionDeclaration(node) {
42-
const declaredVariables = context.getDeclaredVariables(node);
43+
const declaredVariables = getDeclaredVariables(context, node);
4344
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
4445
declaredVariables,
4546
context,

src/rules/no-confusing-set-timeout.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
type ParsedJestFnCall,
33
createRule,
4+
getScope,
45
isIdentifier,
56
parseJestFnCall,
67
} from './utils';
@@ -38,7 +39,6 @@ export default createRule({
3839

3940
return {
4041
CallExpression(node) {
41-
const scope = context.getScope();
4242
const jestFnCall = parseJestFnCall(node, context);
4343

4444
if (!jestFnCall) {
@@ -51,7 +51,7 @@ export default createRule({
5151
return;
5252
}
5353

54-
if (!['global', 'module'].includes(scope.type)) {
54+
if (!['global', 'module'].includes(getScope(context, node).type)) {
5555
context.report({ messageId: 'globalSetTimeout', node });
5656
}
5757

src/rules/no-disabled-tests.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
createRule,
33
getAccessorValue,
4+
getScope,
45
parseJestFnCall,
56
resolveScope,
67
} from './utils';
@@ -80,7 +81,7 @@ export default createRule({
8081
}
8182
},
8283
'CallExpression[callee.name="pending"]'(node) {
83-
if (resolveScope(context.getScope(), 'pending')) {
84+
if (resolveScope(getScope(context, node), 'pending')) {
8485
return;
8586
}
8687

src/rules/no-done-callback.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@ import {
33
type TSESLint,
44
type TSESTree,
55
} from '@typescript-eslint/utils';
6-
import { createRule, getNodeName, isFunction, parseJestFnCall } from './utils';
6+
import {
7+
createRule,
8+
getNodeName,
9+
getSourceCode,
10+
isFunction,
11+
parseJestFnCall,
12+
} from './utils';
713

814
const findCallbackArg = (
915
node: TSESTree.CallExpression,
@@ -104,7 +110,7 @@ export default createRule({
104110
fix(fixer) {
105111
const { body, params } = callback;
106112

107-
const sourceCode = context.getSourceCode();
113+
const sourceCode = getSourceCode(context);
108114
const firstBodyToken = sourceCode.getFirstToken(body);
109115
const lastBodyToken = sourceCode.getLastToken(body);
110116

src/rules/no-if.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
TestCaseName,
44
createRule,
55
getAccessorValue,
6+
getDeclaredVariables,
67
getNodeName,
78
getTestCallExpressionsFromDeclaredVariables,
89
parseJestFnCall,
@@ -89,7 +90,7 @@ export default createRule({
8990
stack.push(isTestFunctionExpression(node));
9091
},
9192
FunctionDeclaration(node) {
92-
const declaredVariables = context.getDeclaredVariables(node);
93+
const declaredVariables = getDeclaredVariables(context, node);
9394
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
9495
declaredVariables,
9596
context,

src/rules/no-jasmine-globals.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
22
import {
33
createRule,
44
getNodeName,
5+
getScope,
56
isSupportedAccessor,
67
resolveScope,
78
} from './utils';
@@ -46,7 +47,7 @@ export default createRule({
4647
calleeName === 'fail' ||
4748
calleeName === 'pending'
4849
) {
49-
if (resolveScope(context.getScope(), calleeName)) {
50+
if (resolveScope(getScope(context, node), calleeName)) {
5051
// It's a local variable, not a jasmine global.
5152
return;
5253
}

src/rules/no-large-snapshots.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
import {
88
createRule,
99
getAccessorValue,
10+
getFilename,
1011
isSupportedAccessor,
1112
parseJestFnCall,
1213
} from './utils';
@@ -46,7 +47,7 @@ const reportOnViolation = (
4647
node.expression.left.type === AST_NODE_TYPES.MemberExpression &&
4748
isSupportedAccessor(node.expression.left.property)
4849
) {
49-
const fileName = context.getFilename();
50+
const fileName = getFilename(context);
5051
const allowedSnapshotsInFile = allowedSnapshots[fileName];
5152

5253
if (allowedSnapshotsInFile) {

0 commit comments

Comments
 (0)