Skip to content

Commit

Permalink
fix: replace use of deprecated methods (#925)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Oct 17, 2024
1 parent b7735da commit 8dbcee0
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 20 deletions.
6 changes: 4 additions & 2 deletions lib/node-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
TSESTree,
} from '@typescript-eslint/utils';

import { getDeclaredVariables, getScope } from '../utils';

import {
isArrayExpression,
isArrowFunctionExpression,
Expand Down Expand Up @@ -287,7 +289,7 @@ export function getVariableReferences(
): TSESLint.Scope.Reference[] {
if (ASTUtils.isVariableDeclarator(node)) {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
return context.getDeclaredVariables(node)[0]?.references?.slice(1) ?? [];
return getDeclaredVariables(context, node)[0]?.references?.slice(1) ?? [];
}

return [];
Expand All @@ -305,7 +307,7 @@ export function getInnermostFunctionScope(
asyncQueryNode: TSESTree.Identifier
): InnermostFunctionScope | null {
const innermostScope = ASTUtils.getInnermostScope(
context.getScope(),
getScope(context, asyncQueryNode),
asyncQueryNode
);

Expand Down
4 changes: 2 additions & 2 deletions lib/rules/consistent-data-testid.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createTestingLibraryRule } from '../create-testing-library-rule';
import { isJSXAttribute, isLiteral } from '../node-utils';
import { getFilename } from '../utils';

export const RULE_NAME = 'consistent-data-testid';
export type MessageIds =
Expand Down Expand Up @@ -77,11 +78,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
},

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

function getFileNameData() {
const splitPath = getFilename().split('/');
const splitPath = getFilename(context).split('/');
const fileNameWithExtension = splitPath.pop() ?? '';
if (
fileNameWithExtension.includes('[') ||
Expand Down
4 changes: 2 additions & 2 deletions lib/rules/no-debugging-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isObjectPattern,
isProperty,
} from '../node-utils';
import { DEBUG_UTILS } from '../utils';
import { DEBUG_UTILS, getDeclaredVariables } from '../utils';

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

const isVariableFromBuiltInConsole = builtInConsoleNodes.some(
(variableDeclarator) => {
const variables = context.getDeclaredVariables(variableDeclarator);
const variables = getDeclaredVariables(context, variableDeclarator);
return variables.some(
({ name }) =>
name === callExpressionIdentifier.name &&
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-manual-cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isObjectPattern,
isProperty,
} from '../node-utils';
import { getDeclaredVariables } from '../utils';

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

reportImportReferences(references);
}
Expand Down
3 changes: 2 additions & 1 deletion lib/rules/no-promise-in-fire-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isNewExpression,
isPromiseIdentifier,
} from '../node-utils';
import { getScope } from '../utils';

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

if (ASTUtils.isIdentifier(node)) {
const nodeVariable = ASTUtils.findVariable(
context.getScope(),
getScope(context, node),
node.name
);
if (!nodeVariable) {
Expand Down
29 changes: 17 additions & 12 deletions lib/rules/prefer-find-by.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
isObjectPattern,
isProperty,
} from '../node-utils';
import { getScope, getSourceCode } from '../utils';

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

create(context, _, helpers) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

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

if (!ASTUtils.isIdentifier(node.body.callee.property)) {
Expand All @@ -134,7 +135,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
node.body.callee.object.arguments[0].callee.property
)
) {
return node.body.callee.object.arguments[0].callee.property.name;
return node.body.callee.object.arguments[0].callee.property;
}

// expect(screen.getByText).not shape
Expand All @@ -149,7 +150,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
node.body.callee.object.object.arguments[0].callee.property
)
) {
return node.body.callee.object.object.arguments[0].callee.property.name;
return node.body.callee.object.object.arguments[0].callee.property;
}

// expect(getByText).not shape
Expand All @@ -161,10 +162,10 @@ export default createTestingLibraryRule<Options, MessageIds>({
node.body.callee.object.object.arguments[0].callee
)
) {
return node.body.callee.object.object.arguments[0].callee.name;
return node.body.callee.object.object.arguments[0].callee;
}

return node.body.callee.property.name;
return node.body.callee.property;
}

function getWrongQueryName(node: TSESTree.ArrowFunctionExpression) {
Expand All @@ -177,7 +178,7 @@ export default createTestingLibraryRule<Options, MessageIds>({
ASTUtils.isIdentifier(node.body.callee) &&
helpers.isSyncQuery(node.body.callee)
) {
return node.body.callee.name;
return node.body.callee;
}

return getWrongQueryNameInAssertion(node);
Expand Down Expand Up @@ -353,12 +354,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
}

// shape of () => screen.getByText
const fullQueryMethod = getWrongQueryName(argument);
const fullQueryMethodNode = getWrongQueryName(argument);

if (!fullQueryMethod) {
if (!fullQueryMethodNode) {
return;
}

const fullQueryMethod = fullQueryMethodNode.name;

// if there is a second argument to AwaitExpression, it is the options
const waitOptions = node.arguments[1];
let waitOptionsSourceCode = '';
Expand Down Expand Up @@ -400,12 +403,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
}

// shape of () => getByText
const fullQueryMethod = getWrongQueryName(argument);
const fullQueryMethodNode = getWrongQueryName(argument);

if (!fullQueryMethod) {
if (!fullQueryMethodNode) {
return;
}

const fullQueryMethod = fullQueryMethodNode.name;

const queryMethod = fullQueryMethod.split('By')[1];
const queryVariant = getFindByQueryVariant(fullQueryMethod);
const callArguments = getQueryArguments(argument.body);
Expand Down Expand Up @@ -434,7 +439,7 @@ export default createTestingLibraryRule<Options, MessageIds>({

// this adds the findBy* declaration - adding it to the list of destructured variables { findBy* } = render()
const definition = findRenderDefinitionDeclaration(
context.getScope(),
getScope(context, fullQueryMethodNode),
fullQueryMethod
);
// I think it should always find it, otherwise code should not be valid (it'd be using undeclared variables)
Expand Down
80 changes: 80 additions & 0 deletions lib/utils/compat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';

declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
export interface RuleContext<
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TMessageIds extends string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
TOptions extends readonly unknown[],
> {
/**
* The filename associated with the source.
*/
filename: string;

/**
* A SourceCode object that you can use to work with the source that
* was passed to ESLint.
*/
sourceCode: Readonly<TSESLint.SourceCode>;
}
}

declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
export interface SourceCode {
/**
* Returns the scope of the given node.
* This information can be used track references to variables.
* @since 8.37.0
*/
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
/**
* Returns an array of the ancestors of the given node, starting at
* the root of the AST and continuing through the direct parent of the current node.
* This array does not include the currently-traversed node itself.
* @since 8.38.0
*/
getAncestors(node: TSESTree.Node): TSESTree.Node[];
/**
* Returns a list of variables declared by the given node.
* This information can be used to track references to variables.
* @since 8.38.0
*/
getDeclaredVariables(
node: TSESTree.Node
): readonly TSESLint.Scope.Variable[];
}
}

/* istanbul ignore next */
export const getFilename = (
context: TSESLint.RuleContext<string, unknown[]>
) => {
return context.filename ?? context.getFilename();
};

/* istanbul ignore next */
export const getSourceCode = (
context: TSESLint.RuleContext<string, unknown[]>
) => {
return context.sourceCode ?? context.getSourceCode();
};

/* istanbul ignore next */
export const getScope = (
context: TSESLint.RuleContext<string, unknown[]>,
node: TSESTree.Node
) => {
return getSourceCode(context).getScope?.(node) ?? context.getScope();
};

/* istanbul ignore next */
export const getDeclaredVariables = (
context: TSESLint.RuleContext<string, unknown[]>,
node: TSESTree.Node
) => {
return (
getSourceCode(context).getDeclaredVariables?.(node) ??
context.getDeclaredVariables(node)
);
};
1 change: 1 addition & 0 deletions lib/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './compat';
export * from './file-import';
export * from './types';

Expand Down

0 comments on commit 8dbcee0

Please sign in to comment.