Skip to content

Commit 8dbcee0

Browse files
authoredOct 17, 2024··
fix: replace use of deprecated methods (#925)
1 parent b7735da commit 8dbcee0

8 files changed

+110
-20
lines changed
 

‎lib/node-utils/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import {
66
TSESTree,
77
} from '@typescript-eslint/utils';
88

9+
import { getDeclaredVariables, getScope } from '../utils';
10+
911
import {
1012
isArrayExpression,
1113
isArrowFunctionExpression,
@@ -287,7 +289,7 @@ export function getVariableReferences(
287289
): TSESLint.Scope.Reference[] {
288290
if (ASTUtils.isVariableDeclarator(node)) {
289291
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
290-
return context.getDeclaredVariables(node)[0]?.references?.slice(1) ?? [];
292+
return getDeclaredVariables(context, node)[0]?.references?.slice(1) ?? [];
291293
}
292294

293295
return [];
@@ -305,7 +307,7 @@ export function getInnermostFunctionScope(
305307
asyncQueryNode: TSESTree.Identifier
306308
): InnermostFunctionScope | null {
307309
const innermostScope = ASTUtils.getInnermostScope(
308-
context.getScope(),
310+
getScope(context, asyncQueryNode),
309311
asyncQueryNode
310312
);
311313

‎lib/rules/consistent-data-testid.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createTestingLibraryRule } from '../create-testing-library-rule';
22
import { isJSXAttribute, isLiteral } from '../node-utils';
3+
import { getFilename } from '../utils';
34

45
export const RULE_NAME = 'consistent-data-testid';
56
export type MessageIds =
@@ -77,11 +78,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
7778
},
7879

7980
create: (context, [options]) => {
80-
const { getFilename } = context;
8181
const { testIdPattern, testIdAttribute: attr, customMessage } = options;
8282

8383
function getFileNameData() {
84-
const splitPath = getFilename().split('/');
84+
const splitPath = getFilename(context).split('/');
8585
const fileNameWithExtension = splitPath.pop() ?? '';
8686
if (
8787
fileNameWithExtension.includes('[') ||

‎lib/rules/no-debugging-utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
isObjectPattern,
1212
isProperty,
1313
} from '../node-utils';
14-
import { DEBUG_UTILS } from '../utils';
14+
import { DEBUG_UTILS, getDeclaredVariables } from '../utils';
1515

1616
type DebugUtilsToCheckForConfig = Record<(typeof DEBUG_UTILS)[number], boolean>;
1717
type DebugUtilsToCheckFor = Partial<DebugUtilsToCheckForConfig>;
@@ -175,7 +175,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
175175

176176
const isVariableFromBuiltInConsole = builtInConsoleNodes.some(
177177
(variableDeclarator) => {
178-
const variables = context.getDeclaredVariables(variableDeclarator);
178+
const variables = getDeclaredVariables(context, variableDeclarator);
179179
return variables.some(
180180
({ name }) =>
181181
name === callExpressionIdentifier.name &&

‎lib/rules/no-manual-cleanup.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
isObjectPattern,
1313
isProperty,
1414
} from '../node-utils';
15+
import { getDeclaredVariables } from '../utils';
1516

1617
export const RULE_NAME = 'no-manual-cleanup';
1718
export type MessageIds = 'noManualCleanup';
@@ -65,7 +66,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
6566
if (isImportDeclaration(moduleNode)) {
6667
// case: import utils from 'testing-library-module'
6768
if (isImportDefaultSpecifier(moduleNode.specifiers[0])) {
68-
const { references } = context.getDeclaredVariables(moduleNode)[0];
69+
const { references } = getDeclaredVariables(context, moduleNode)[0];
6970

7071
reportImportReferences(references);
7172
}

‎lib/rules/no-promise-in-fire-event.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
isNewExpression,
99
isPromiseIdentifier,
1010
} from '../node-utils';
11+
import { getScope } from '../utils';
1112

1213
export const RULE_NAME = 'no-promise-in-fire-event';
1314
export type MessageIds = 'noPromiseInFireEvent';
@@ -76,7 +77,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
7677

7778
if (ASTUtils.isIdentifier(node)) {
7879
const nodeVariable = ASTUtils.findVariable(
79-
context.getScope(),
80+
getScope(context, node),
8081
node.name
8182
);
8283
if (!nodeVariable) {

‎lib/rules/prefer-find-by.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
isObjectPattern,
1010
isProperty,
1111
} from '../node-utils';
12+
import { getScope, getSourceCode } from '../utils';
1213

1314
export const RULE_NAME = 'prefer-find-by';
1415
export type MessageIds = 'preferFindBy';
@@ -69,7 +70,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
6970
defaultOptions: [],
7071

7172
create(context, _, helpers) {
72-
const sourceCode = context.getSourceCode();
73+
const sourceCode = getSourceCode(context);
7374

7475
/**
7576
* Reports the invalid usage of wait* plus getBy/QueryBy methods and automatically fixes the scenario
@@ -118,7 +119,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
118119
isCallExpression(node.body.callee.object.arguments[0]) &&
119120
ASTUtils.isIdentifier(node.body.callee.object.arguments[0].callee)
120121
) {
121-
return node.body.callee.object.arguments[0].callee.name;
122+
return node.body.callee.object.arguments[0].callee;
122123
}
123124

124125
if (!ASTUtils.isIdentifier(node.body.callee.property)) {
@@ -134,7 +135,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
134135
node.body.callee.object.arguments[0].callee.property
135136
)
136137
) {
137-
return node.body.callee.object.arguments[0].callee.property.name;
138+
return node.body.callee.object.arguments[0].callee.property;
138139
}
139140

140141
// expect(screen.getByText).not shape
@@ -149,7 +150,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
149150
node.body.callee.object.object.arguments[0].callee.property
150151
)
151152
) {
152-
return node.body.callee.object.object.arguments[0].callee.property.name;
153+
return node.body.callee.object.object.arguments[0].callee.property;
153154
}
154155

155156
// expect(getByText).not shape
@@ -161,10 +162,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
161162
node.body.callee.object.object.arguments[0].callee
162163
)
163164
) {
164-
return node.body.callee.object.object.arguments[0].callee.name;
165+
return node.body.callee.object.object.arguments[0].callee;
165166
}
166167

167-
return node.body.callee.property.name;
168+
return node.body.callee.property;
168169
}
169170

170171
function getWrongQueryName(node: TSESTree.ArrowFunctionExpression) {
@@ -177,7 +178,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
177178
ASTUtils.isIdentifier(node.body.callee) &&
178179
helpers.isSyncQuery(node.body.callee)
179180
) {
180-
return node.body.callee.name;
181+
return node.body.callee;
181182
}
182183

183184
return getWrongQueryNameInAssertion(node);
@@ -353,12 +354,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
353354
}
354355

355356
// shape of () => screen.getByText
356-
const fullQueryMethod = getWrongQueryName(argument);
357+
const fullQueryMethodNode = getWrongQueryName(argument);
357358

358-
if (!fullQueryMethod) {
359+
if (!fullQueryMethodNode) {
359360
return;
360361
}
361362

363+
const fullQueryMethod = fullQueryMethodNode.name;
364+
362365
// if there is a second argument to AwaitExpression, it is the options
363366
const waitOptions = node.arguments[1];
364367
let waitOptionsSourceCode = '';
@@ -400,12 +403,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
400403
}
401404

402405
// shape of () => getByText
403-
const fullQueryMethod = getWrongQueryName(argument);
406+
const fullQueryMethodNode = getWrongQueryName(argument);
404407

405-
if (!fullQueryMethod) {
408+
if (!fullQueryMethodNode) {
406409
return;
407410
}
408411

412+
const fullQueryMethod = fullQueryMethodNode.name;
413+
409414
const queryMethod = fullQueryMethod.split('By')[1];
410415
const queryVariant = getFindByQueryVariant(fullQueryMethod);
411416
const callArguments = getQueryArguments(argument.body);
@@ -434,7 +439,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
434439

435440
// this adds the findBy* declaration - adding it to the list of destructured variables { findBy* } = render()
436441
const definition = findRenderDefinitionDeclaration(
437-
context.getScope(),
442+
getScope(context, fullQueryMethodNode),
438443
fullQueryMethod
439444
);
440445
// I think it should always find it, otherwise code should not be valid (it'd be using undeclared variables)

‎lib/utils/compat.ts

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
2+
3+
declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
4+
export interface RuleContext<
5+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
6+
TMessageIds extends string,
7+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
8+
TOptions extends readonly unknown[],
9+
> {
10+
/**
11+
* The filename associated with the source.
12+
*/
13+
filename: string;
14+
15+
/**
16+
* A SourceCode object that you can use to work with the source that
17+
* was passed to ESLint.
18+
*/
19+
sourceCode: Readonly<TSESLint.SourceCode>;
20+
}
21+
}
22+
23+
declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
24+
export interface SourceCode {
25+
/**
26+
* Returns the scope of the given node.
27+
* This information can be used track references to variables.
28+
* @since 8.37.0
29+
*/
30+
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
31+
/**
32+
* Returns an array of the ancestors of the given node, starting at
33+
* the root of the AST and continuing through the direct parent of the current node.
34+
* This array does not include the currently-traversed node itself.
35+
* @since 8.38.0
36+
*/
37+
getAncestors(node: TSESTree.Node): TSESTree.Node[];
38+
/**
39+
* Returns a list of variables declared by the given node.
40+
* This information can be used to track references to variables.
41+
* @since 8.38.0
42+
*/
43+
getDeclaredVariables(
44+
node: TSESTree.Node
45+
): readonly TSESLint.Scope.Variable[];
46+
}
47+
}
48+
49+
/* istanbul ignore next */
50+
export const getFilename = (
51+
context: TSESLint.RuleContext<string, unknown[]>
52+
) => {
53+
return context.filename ?? context.getFilename();
54+
};
55+
56+
/* istanbul ignore next */
57+
export const getSourceCode = (
58+
context: TSESLint.RuleContext<string, unknown[]>
59+
) => {
60+
return context.sourceCode ?? context.getSourceCode();
61+
};
62+
63+
/* istanbul ignore next */
64+
export const getScope = (
65+
context: TSESLint.RuleContext<string, unknown[]>,
66+
node: TSESTree.Node
67+
) => {
68+
return getSourceCode(context).getScope?.(node) ?? context.getScope();
69+
};
70+
71+
/* istanbul ignore next */
72+
export const getDeclaredVariables = (
73+
context: TSESLint.RuleContext<string, unknown[]>,
74+
node: TSESTree.Node
75+
) => {
76+
return (
77+
getSourceCode(context).getDeclaredVariables?.(node) ??
78+
context.getDeclaredVariables(node)
79+
);
80+
};

‎lib/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
export * from './compat';
12
export * from './file-import';
23
export * from './types';
34

0 commit comments

Comments
 (0)
Please sign in to comment.