Skip to content

Commit d4caa67

Browse files
committed
refactor: remove ESLint context fallbacks
1 parent 854cef4 commit d4caa67

20 files changed

+34
-91
lines changed

src/rules/expect-expect.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
77
import {
88
createRule,
9-
getAncestors,
10-
getDeclaredVariables,
119
getNodeName,
1210
getTestCallExpressionsFromDeclaredVariables,
1311
isSupportedAccessor,
@@ -94,7 +92,8 @@ export default createRule<
9492
: -1;
9593

9694
if (node.type === AST_NODE_TYPES.FunctionDeclaration) {
97-
const declaredVariables = getDeclaredVariables(context, node);
95+
const declaredVariables =
96+
context.sourceCode.getDeclaredVariables(node);
9897
const testCallExpressions =
9998
getTestCallExpressionsFromDeclaredVariables(
10099
declaredVariables,
@@ -129,7 +128,7 @@ export default createRule<
129128
unchecked.push(node);
130129
} else if (matchesAssertFunctionName(name, assertFunctionNames)) {
131130
// Return early in case of nested `it` statements.
132-
checkCallExpressionUsed(getAncestors(context, node));
131+
checkCallExpressionUsed(context.sourceCode.getAncestors(node));
133132
}
134133
},
135134
'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, getSourceCode } from './utils';
2+
import { createRule } from './utils';
33

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

2626
function checkNode(node: TSESTree.Comment) {
2727
if (!hasTests(node)) {

src/rules/no-conditional-expect.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
22
import {
33
type KnownCallExpression,
44
createRule,
5-
getDeclaredVariables,
65
getTestCallExpressionsFromDeclaredVariables,
76
isSupportedAccessor,
87
isTypeOfJestFnCall,
@@ -38,7 +37,7 @@ export default createRule({
3837

3938
return {
4039
FunctionDeclaration(node) {
41-
const declaredVariables = getDeclaredVariables(context, node);
40+
const declaredVariables = context.sourceCode.getDeclaredVariables(node);
4241
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
4342
declaredVariables,
4443
context,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
type ParsedJestFnCall,
33
createRule,
4-
getScope,
54
isIdentifier,
65
parseJestFnCall,
76
} from './utils';
@@ -49,7 +48,9 @@ export default createRule({
4948
return;
5049
}
5150

52-
if (!['global', 'module'].includes(getScope(context, node).type)) {
51+
if (
52+
!['global', 'module'].includes(context.sourceCode.getScope(node).type)
53+
) {
5354
context.report({ messageId: 'globalSetTimeout', node });
5455
}
5556

src/rules/no-disabled-tests.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import {
22
createRule,
33
getAccessorValue,
4-
getScope,
54
parseJestFnCall,
65
resolveScope,
76
} from './utils';
@@ -50,7 +49,7 @@ export default createRule({
5049
}
5150
},
5251
'CallExpression[callee.name="pending"]'(node) {
53-
if (resolveScope(getScope(context, node), 'pending')) {
52+
if (resolveScope(context.sourceCode.getScope(node), 'pending')) {
5453
return;
5554
}
5655

src/rules/no-done-callback.ts

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

158
const findCallbackArg = (
169
node: TSESTree.CallExpression,
@@ -109,7 +102,7 @@ export default createRule({
109102
fix(fixer) {
110103
const { body, params } = callback;
111104

112-
const sourceCode = getSourceCode(context);
105+
const { sourceCode } = context;
113106
const firstBodyToken = sourceCode.getFirstToken(body);
114107
const lastBodyToken = sourceCode.getLastToken(body);
115108

@@ -133,7 +126,7 @@ export default createRule({
133126
!tokenAfterLastParam
134127
) {
135128
throw new Error(
136-
`Unexpected null when attempting to fix ${getFilename(context)} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
129+
`Unexpected null when attempting to fix ${context.filename} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
137130
);
138131
}
139132

src/rules/no-jasmine-globals.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
22
import {
33
createRule,
44
getNodeName,
5-
getScope,
65
isSupportedAccessor,
76
resolveScope,
87
} from './utils';
@@ -45,7 +44,7 @@ export default createRule({
4544
calleeName === 'fail' ||
4645
calleeName === 'pending'
4746
) {
48-
if (resolveScope(getScope(context, node), calleeName)) {
47+
if (resolveScope(context.sourceCode.getScope(node), calleeName)) {
4948
// It's a local variable, not a jasmine global.
5049
return;
5150
}

src/rules/no-large-snapshots.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
import {
88
createRule,
99
getAccessorValue,
10-
getFilename,
1110
isSupportedAccessor,
1211
parseJestFnCall,
1312
} from './utils';
@@ -47,7 +46,7 @@ const reportOnViolation = (
4746
node.expression.left.type === AST_NODE_TYPES.MemberExpression &&
4847
isSupportedAccessor(node.expression.left.property)
4948
) {
50-
const fileName = getFilename(context);
49+
const fileName = context.filename;
5150
const allowedSnapshotsInFile = allowedSnapshots[fileName];
5251

5352
if (allowedSnapshotsInFile) {
@@ -106,7 +105,7 @@ export default createRule<[RuleOptions], MessageId>({
106105
},
107106
defaultOptions: [{}],
108107
create(context, [options]) {
109-
if (getFilename(context).endsWith('.snap')) {
108+
if (context.filename.endsWith('.snap')) {
110109
return {
111110
ExpressionStatement(node) {
112111
reportOnViolation(context, node, options);

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
22
import {
33
createRule,
4-
getDeclaredVariables,
54
getTestCallExpressionsFromDeclaredVariables,
65
isFunction,
76
isTypeOfJestFnCall,
@@ -53,7 +52,7 @@ export default createRule({
5352
context.report({ messageId: 'noReturnValue', node: returnStmt });
5453
},
5554
FunctionDeclaration(node) {
56-
const declaredVariables = getDeclaredVariables(context, node);
55+
const declaredVariables = context.sourceCode.getDeclaredVariables(node);
5756
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
5857
declaredVariables,
5958
context,

src/rules/prefer-comparison-matcher.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
createRule,
55
getAccessorValue,
66
getFirstMatcherArg,
7-
getSourceCode,
87
isBooleanLiteral,
98
isStringNode,
109
parseJestFnCall,
@@ -115,7 +114,7 @@ export default createRule({
115114

116115
context.report({
117116
fix(fixer) {
118-
const sourceCode = getSourceCode(context);
117+
const { sourceCode } = context;
119118

120119
// preserve the existing modifier if it's not a negation
121120
const modifierText =

0 commit comments

Comments
 (0)