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

+27-1
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

+4-2
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

+2-2
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

+2-1
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

+2-2
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

+2-1
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

+8-2
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

+2-1
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

+2-1
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

+2-1
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) {

src/rules/no-test-return-statement.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
22
import {
33
createRule,
4+
getDeclaredVariables,
45
getTestCallExpressionsFromDeclaredVariables,
56
isFunction,
67
isTypeOfJestFnCall,
@@ -54,7 +55,7 @@ export default createRule({
5455
context.report({ messageId: 'noReturnValue', node: returnStmt });
5556
},
5657
FunctionDeclaration(node) {
57-
const declaredVariables = context.getDeclaredVariables(node);
58+
const declaredVariables = getDeclaredVariables(context, node);
5859
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
5960
declaredVariables,
6061
context,

src/rules/prefer-comparison-matcher.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
createRule,
55
getAccessorValue,
66
getFirstMatcherArg,
7+
getSourceCode,
78
isBooleanLiteral,
89
isStringNode,
910
parseJestFnCall,
@@ -116,7 +117,7 @@ export default createRule({
116117

117118
context.report({
118119
fix(fixer) {
119-
const sourceCode = context.getSourceCode();
120+
const sourceCode = getSourceCode(context);
120121

121122
// preserve the existing modifier if it's not a negation
122123
const modifierText =

src/rules/prefer-equality-matcher.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createRule,
66
getAccessorValue,
77
getFirstMatcherArg,
8+
getSourceCode,
89
isBooleanLiteral,
910
parseJestFnCall,
1011
} from './utils';
@@ -74,7 +75,7 @@ export default createRule({
7475
const buildFixer =
7576
(equalityMatcher: string): TSESLint.ReportFixFunction =>
7677
fixer => {
77-
const sourceCode = context.getSourceCode();
78+
const sourceCode = getSourceCode(context);
7879

7980
// preserve the existing modifier if it's not a negation
8081
let modifierText =

src/rules/prefer-mock-promise-shorthand.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
createRule,
66
getAccessorValue,
77
getNodeName,
8+
getSourceCode,
89
isFunction,
910
isSupportedAccessor,
1011
} from './utils';
@@ -70,7 +71,7 @@ export default createRule({
7071
messageId: 'useMockShorthand',
7172
data: { replacement },
7273
fix(fixer) {
73-
const sourceCode = context.getSourceCode();
74+
const sourceCode = getSourceCode(context);
7475

7576
// there shouldn't be more than one argument, but if there is don't try
7677
// fixing since we have no idea what to do with the extra arguments

src/rules/prefer-spy-on.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
type TSESLint,
44
type TSESTree,
55
} from '@typescript-eslint/utils';
6-
import { createRule, getNodeName } from './utils';
6+
import { createRule, getNodeName, getSourceCode } from './utils';
77

88
const findNodeObject = (
99
node: TSESTree.CallExpression | TSESTree.MemberExpression,
@@ -57,7 +57,7 @@ const getAutoFixMockImplementation = (
5757
}
5858

5959
const [arg] = jestFnCall.arguments;
60-
const argSource = arg && context.getSourceCode().getText(arg);
60+
const argSource = arg && getSourceCode(context).getText(arg);
6161

6262
return argSource
6363
? `.mockImplementation(${argSource})`

src/rules/prefer-to-contain.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
createRule,
88
getAccessorValue,
99
getFirstMatcherArg,
10+
getSourceCode,
1011
hasOnlyOneArgument,
1112
isBooleanLiteral,
1213
isSupportedAccessor,
@@ -89,7 +90,7 @@ export default createRule({
8990

9091
context.report({
9192
fix(fixer) {
92-
const sourceCode = context.getSourceCode();
93+
const sourceCode = getSourceCode(context);
9394

9495
// we need to negate the expectation if the current expected
9596
// value is itself negated by the "not" modifier

src/rules/utils/misc.ts

+61-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ export const removeExtraArgumentsFixer = (
178178
const firstArg = func.arguments[from];
179179
const lastArg = func.arguments[func.arguments.length - 1];
180180

181-
const sourceCode = context.getSourceCode();
181+
const sourceCode = getSourceCode(context);
182182
let tokenAfterLastParam = sourceCode.getTokenAfter(lastArg)!;
183183

184184
if (tokenAfterLastParam.value === ',') {
@@ -229,3 +229,63 @@ export const getFirstMatcherArg = (
229229

230230
return followTypeAssertionChain(firstArg);
231231
};
232+
233+
/* istanbul ignore next */
234+
export const getFilename = (
235+
context: TSESLint.RuleContext<string, unknown[]>,
236+
) => {
237+
return 'filename' in context
238+
? (context.filename as string)
239+
: context.getFilename();
240+
};
241+
242+
/* istanbul ignore next */
243+
export const getSourceCode = (
244+
context: TSESLint.RuleContext<string, unknown[]>,
245+
) => {
246+
return 'sourceCode' in context
247+
? (context.sourceCode as TSESLint.SourceCode)
248+
: context.getSourceCode();
249+
};
250+
251+
/* istanbul ignore next */
252+
export const getScope = (
253+
context: TSESLint.RuleContext<string, unknown[]>,
254+
node: TSESTree.Node,
255+
) => {
256+
const sourceCode = getSourceCode(context);
257+
258+
if ('getScope' in sourceCode) {
259+
return sourceCode.getScope(node);
260+
}
261+
262+
return context.getScope();
263+
};
264+
265+
/* istanbul ignore next */
266+
export const getAncestors = (
267+
context: TSESLint.RuleContext<string, unknown[]>,
268+
node: TSESTree.Node,
269+
) => {
270+
const sourceCode = getSourceCode(context);
271+
272+
if ('getAncestors' in sourceCode) {
273+
return sourceCode.getAncestors(node);
274+
}
275+
276+
return context.getAncestors();
277+
};
278+
279+
/* istanbul ignore next */
280+
export const getDeclaredVariables = (
281+
context: TSESLint.RuleContext<string, unknown[]>,
282+
node: TSESTree.Node,
283+
) => {
284+
const sourceCode = getSourceCode(context);
285+
286+
if ('getDeclaredVariables' in sourceCode) {
287+
return sourceCode.getDeclaredVariables(node);
288+
}
289+
290+
return context.getDeclaredVariables(node);
291+
};

0 commit comments

Comments
 (0)