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 =

src/rules/prefer-equality-matcher.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
createRule,
66
getAccessorValue,
77
getFirstMatcherArg,
8-
getSourceCode,
98
isBooleanLiteral,
109
parseJestFnCall,
1110
} from './utils';
@@ -73,7 +72,7 @@ export default createRule({
7372
const buildFixer =
7473
(equalityMatcher: string): TSESLint.ReportFixFunction =>
7574
fixer => {
76-
const sourceCode = getSourceCode(context);
75+
const { sourceCode } = context;
7776

7877
// preserve the existing modifier if it's not a negation
7978
let modifierText =

src/rules/prefer-importing-jest-globals.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import {
33
type JestFnType,
44
createRule,
55
getAccessorValue,
6-
getSourceCode,
76
isIdentifier,
87
isStringNode,
98
isSupportedAccessor,
@@ -94,7 +93,7 @@ export default createRule({
9493
messageId: 'preferImportingJestGlobal',
9594
data: { jestFunctions: Array.from(functionsToImport).join(', ') },
9695
fix(fixer) {
97-
const sourceCode = getSourceCode(context);
96+
const { sourceCode } = context;
9897
const [firstNode] = sourceCode.ast.body;
9998

10099
// check if "use strict" directive exists

src/rules/prefer-jest-mocked.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
2-
import { createRule, followTypeAssertionChain, getSourceCode } from './utils';
2+
import { createRule, followTypeAssertionChain } from './utils';
33

44
const mockTypes = ['Mock', 'MockedFunction', 'MockedClass', 'MockedObject'];
55

@@ -42,7 +42,7 @@ export default createRule({
4242
return;
4343
}
4444

45-
const fnName = getSourceCode(context).text.slice(
45+
const fnName = context.sourceCode.text.slice(
4646
...followTypeAssertionChain(node.expression).range,
4747
);
4848

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
createRule,
66
getAccessorValue,
77
getNodeName,
8-
getSourceCode,
98
isFunction,
109
isSupportedAccessor,
1110
} from './utils';
@@ -69,7 +68,7 @@ export default createRule({
6968
messageId: 'useMockShorthand',
7069
data: { replacement },
7170
fix(fixer) {
72-
const sourceCode = getSourceCode(context);
71+
const { sourceCode } = context;
7372

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

src/rules/prefer-spy-on.ts

Lines changed: 2 additions & 2 deletions
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, getSourceCode } from './utils';
6+
import { createRule, getNodeName } 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 && getSourceCode(context).getText(arg);
60+
const argSource = arg && context.sourceCode.getText(arg);
6161

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

src/rules/prefer-to-contain.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
createRule,
88
getAccessorValue,
99
getFirstMatcherArg,
10-
getSourceCode,
1110
hasOnlyOneArgument,
1211
isBooleanLiteral,
1312
isSupportedAccessor,
@@ -88,7 +87,7 @@ export default createRule({
8887

8988
context.report({
9089
fix(fixer) {
91-
const sourceCode = getSourceCode(context);
90+
const { sourceCode } = context;
9291

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

src/rules/utils/misc.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export const removeExtraArgumentsFixer = (
174174
const firstArg = func.arguments[from];
175175
const lastArg = func.arguments[func.arguments.length - 1];
176176

177-
const sourceCode = getSourceCode(context);
177+
const { sourceCode } = context;
178178
let tokenAfterLastParam = sourceCode.getTokenAfter(lastArg)!;
179179

180180
if (tokenAfterLastParam.value === ',') {
@@ -225,44 +225,3 @@ export const getFirstMatcherArg = (
225225

226226
return followTypeAssertionChain(firstArg);
227227
};
228-
229-
/* istanbul ignore next */
230-
export const getFilename = (
231-
context: TSESLint.RuleContext<string, unknown[]>,
232-
) => {
233-
return context.filename ?? context.getFilename();
234-
};
235-
236-
/* istanbul ignore next */
237-
export const getSourceCode = (
238-
context: TSESLint.RuleContext<string, unknown[]>,
239-
) => {
240-
return context.sourceCode ?? context.getSourceCode();
241-
};
242-
243-
/* istanbul ignore next */
244-
export const getScope = (
245-
context: TSESLint.RuleContext<string, unknown[]>,
246-
node: TSESTree.Node,
247-
) => {
248-
return getSourceCode(context).getScope?.(node) ?? context.getScope();
249-
};
250-
251-
/* istanbul ignore next */
252-
export const getAncestors = (
253-
context: TSESLint.RuleContext<string, unknown[]>,
254-
node: TSESTree.Node,
255-
) => {
256-
return getSourceCode(context).getAncestors?.(node) ?? context.getAncestors();
257-
};
258-
259-
/* istanbul ignore next */
260-
export const getDeclaredVariables = (
261-
context: TSESLint.RuleContext<string, unknown[]>,
262-
node: TSESTree.Node,
263-
) => {
264-
return (
265-
getSourceCode(context).getDeclaredVariables?.(node) ??
266-
context.getDeclaredVariables(node)
267-
);
268-
};

src/rules/utils/padding.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
type TSESTree,
1616
} from '@typescript-eslint/utils';
1717
import * as astUtils from './ast-utils';
18-
import { createRule, getSourceCode } from './misc';
18+
import { createRule } from './misc';
1919

2020
// Statement types we'll respond to
2121
export const enum StatementType {
@@ -353,7 +353,7 @@ export const createPaddingRule = (
353353
create(context) {
354354
const paddingContext = {
355355
ruleContext: context,
356-
sourceCode: getSourceCode(context),
356+
sourceCode: context.sourceCode,
357357
scopeInfo: createScopeInfo(),
358358
configs,
359359
};

0 commit comments

Comments
 (0)