Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace use of deprecated methods #1453

Merged
merged 6 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readdirSync } from 'fs';
import { join, parse } from 'path';
import type { TSESLint } from '@typescript-eslint/utils';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import {
name as packageName,
version as packageVersion,
Expand All @@ -19,6 +19,32 @@ declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
}
}

declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we avoid this by upgrading to v6?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I implemented the types in typescript-eslint/typescript-eslint#7812

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sweet! we should probably do that in a major

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[];
}
}

// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
/* istanbul ignore next */
const interopRequireDefault = (obj: any): { default: any } =>
Expand Down
6 changes: 4 additions & 2 deletions src/rules/expect-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
createRule,
getAncestors,
getDeclaredVariables,
getNodeName,
getTestCallExpressionsFromDeclaredVariables,
isSupportedAccessor,
Expand Down Expand Up @@ -94,7 +96,7 @@ export default createRule<
: -1;

if (node.type === AST_NODE_TYPES.FunctionDeclaration) {
const declaredVariables = context.getDeclaredVariables(node);
const declaredVariables = getDeclaredVariables(context, node);
const testCallExpressions =
getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
Expand Down Expand Up @@ -129,7 +131,7 @@ export default createRule<
unchecked.push(node);
} else if (matchesAssertFunctionName(name, assertFunctionNames)) {
// Return early in case of nested `it` statements.
checkCallExpressionUsed(context.getAncestors());
checkCallExpressionUsed(getAncestors(context, node));
}
},
'Program:exit'() {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-commented-out-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { createRule } from './utils';
import { createRule, getSourceCode } from './utils';

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

function checkNode(node: TSESTree.Comment) {
if (!hasTests(node)) {
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-conditional-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
type KnownCallExpression,
createRule,
getDeclaredVariables,
getTestCallExpressionsFromDeclaredVariables,
isSupportedAccessor,
isTypeOfJestFnCall,
Expand Down Expand Up @@ -39,7 +40,7 @@ export default createRule({

return {
FunctionDeclaration(node) {
const declaredVariables = context.getDeclaredVariables(node);
const declaredVariables = getDeclaredVariables(context, node);
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
context,
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-confusing-set-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
type ParsedJestFnCall,
createRule,
getScope,
isIdentifier,
parseJestFnCall,
} from './utils';
Expand Down Expand Up @@ -38,7 +39,6 @@ export default createRule({

return {
CallExpression(node) {
const scope = context.getScope();
const jestFnCall = parseJestFnCall(node, context);

if (!jestFnCall) {
Expand All @@ -51,7 +51,7 @@ export default createRule({
return;
}

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

Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-disabled-tests.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
createRule,
getAccessorValue,
getScope,
parseJestFnCall,
resolveScope,
} from './utils';
Expand Down Expand Up @@ -80,7 +81,7 @@ export default createRule({
}
},
'CallExpression[callee.name="pending"]'(node) {
if (resolveScope(context.getScope(), 'pending')) {
if (resolveScope(getScope(context, node), 'pending')) {
return;
}

Expand Down
10 changes: 8 additions & 2 deletions src/rules/no-done-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ import {
type TSESLint,
type TSESTree,
} from '@typescript-eslint/utils';
import { createRule, getNodeName, isFunction, parseJestFnCall } from './utils';
import {
createRule,
getNodeName,
getSourceCode,
isFunction,
parseJestFnCall,
} from './utils';

const findCallbackArg = (
node: TSESTree.CallExpression,
Expand Down Expand Up @@ -104,7 +110,7 @@ export default createRule({
fix(fixer) {
const { body, params } = callback;

const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
const firstBodyToken = sourceCode.getFirstToken(body);
const lastBodyToken = sourceCode.getLastToken(body);

Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-if.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
TestCaseName,
createRule,
getAccessorValue,
getDeclaredVariables,
getNodeName,
getTestCallExpressionsFromDeclaredVariables,
parseJestFnCall,
Expand Down Expand Up @@ -89,7 +90,7 @@ export default createRule({
stack.push(isTestFunctionExpression(node));
},
FunctionDeclaration(node) {
const declaredVariables = context.getDeclaredVariables(node);
const declaredVariables = getDeclaredVariables(context, node);
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
context,
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import {
createRule,
getNodeName,
getScope,
isSupportedAccessor,
resolveScope,
} from './utils';
Expand Down Expand Up @@ -46,7 +47,7 @@ export default createRule({
calleeName === 'fail' ||
calleeName === 'pending'
) {
if (resolveScope(context.getScope(), calleeName)) {
if (resolveScope(getScope(context, node), calleeName)) {
// It's a local variable, not a jasmine global.
return;
}
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-large-snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import {
createRule,
getAccessorValue,
getFilename,
isSupportedAccessor,
parseJestFnCall,
} from './utils';
Expand Down Expand Up @@ -46,7 +47,7 @@ const reportOnViolation = (
node.expression.left.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.expression.left.property)
) {
const fileName = context.getFilename();
const fileName = getFilename(context);
const allowedSnapshotsInFile = allowedSnapshots[fileName];

if (allowedSnapshotsInFile) {
Expand Down
3 changes: 2 additions & 1 deletion src/rules/no-test-return-statement.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
createRule,
getDeclaredVariables,
getTestCallExpressionsFromDeclaredVariables,
isFunction,
isTypeOfJestFnCall,
Expand Down Expand Up @@ -54,7 +55,7 @@ export default createRule({
context.report({ messageId: 'noReturnValue', node: returnStmt });
},
FunctionDeclaration(node) {
const declaredVariables = context.getDeclaredVariables(node);
const declaredVariables = getDeclaredVariables(context, node);
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
context,
Expand Down
3 changes: 2 additions & 1 deletion src/rules/prefer-comparison-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
createRule,
getAccessorValue,
getFirstMatcherArg,
getSourceCode,
isBooleanLiteral,
isStringNode,
parseJestFnCall,
Expand Down Expand Up @@ -116,7 +117,7 @@ export default createRule({

context.report({
fix(fixer) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

// preserve the existing modifier if it's not a negation
const modifierText =
Expand Down
3 changes: 2 additions & 1 deletion src/rules/prefer-equality-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createRule,
getAccessorValue,
getFirstMatcherArg,
getSourceCode,
isBooleanLiteral,
parseJestFnCall,
} from './utils';
Expand Down Expand Up @@ -74,7 +75,7 @@ export default createRule({
const buildFixer =
(equalityMatcher: string): TSESLint.ReportFixFunction =>
fixer => {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

// preserve the existing modifier if it's not a negation
let modifierText =
Expand Down
3 changes: 2 additions & 1 deletion src/rules/prefer-mock-promise-shorthand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
createRule,
getAccessorValue,
getNodeName,
getSourceCode,
isFunction,
isSupportedAccessor,
} from './utils';
Expand Down Expand Up @@ -70,7 +71,7 @@ export default createRule({
messageId: 'useMockShorthand',
data: { replacement },
fix(fixer) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

// there shouldn't be more than one argument, but if there is don't try
// fixing since we have no idea what to do with the extra arguments
Expand Down
4 changes: 2 additions & 2 deletions src/rules/prefer-spy-on.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
type TSESLint,
type TSESTree,
} from '@typescript-eslint/utils';
import { createRule, getNodeName } from './utils';
import { createRule, getNodeName, getSourceCode } from './utils';

const findNodeObject = (
node: TSESTree.CallExpression | TSESTree.MemberExpression,
Expand Down Expand Up @@ -57,7 +57,7 @@ const getAutoFixMockImplementation = (
}

const [arg] = jestFnCall.arguments;
const argSource = arg && context.getSourceCode().getText(arg);
const argSource = arg && getSourceCode(context).getText(arg);

return argSource
? `.mockImplementation(${argSource})`
Expand Down
3 changes: 2 additions & 1 deletion src/rules/prefer-to-contain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
createRule,
getAccessorValue,
getFirstMatcherArg,
getSourceCode,
hasOnlyOneArgument,
isBooleanLiteral,
isSupportedAccessor,
Expand Down Expand Up @@ -89,7 +90,7 @@ export default createRule({

context.report({
fix(fixer) {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

// we need to negate the expectation if the current expected
// value is itself negated by the "not" modifier
Expand Down
62 changes: 61 additions & 1 deletion src/rules/utils/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ export const removeExtraArgumentsFixer = (
const firstArg = func.arguments[from];
const lastArg = func.arguments[func.arguments.length - 1];

const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);
let tokenAfterLastParam = sourceCode.getTokenAfter(lastArg)!;

if (tokenAfterLastParam.value === ',') {
Expand Down Expand Up @@ -229,3 +229,63 @@ export const getFirstMatcherArg = (

return followTypeAssertionChain(firstArg);
};

/* istanbul ignore next */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why do we need to ignore coverage?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because only one branch in each function should ever get used depending on what ESLint version you're using, and coverage is only collected against a single ESLint version at a time (technically we could merge reports for CI but that'd still be a pain locally)

export const getFilename = (
context: TSESLint.RuleContext<string, unknown[]>,
) => {
return 'filename' in context
? (context.filename as string)
: context.getFilename();
};

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

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

if ('getScope' in sourceCode) {
return sourceCode.getScope(node);
}

return context.getScope();
};

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

if ('getAncestors' in sourceCode) {
return sourceCode.getAncestors(node);
}

return context.getAncestors();
};

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

if ('getDeclaredVariables' in sourceCode) {
return sourceCode.getDeclaredVariables(node);
}

return context.getDeclaredVariables(node);
};
Loading