Skip to content

Commit

Permalink
fix: replace usage of deprecated Context#getSourceCode
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jan 1, 2024
1 parent 55326e1 commit 86197f6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 14 deletions.
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
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/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
16 changes: 12 additions & 4 deletions 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 @@ -230,11 +230,19 @@ export const getFirstMatcherArg = (
return followTypeAssertionChain(firstArg);
};

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

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

if ('getScope' in sourceCode) {
return sourceCode.getScope(node);
Expand All @@ -247,7 +255,7 @@ export const getAncestors = (
context: TSESLint.RuleContext<string, unknown[]>,
node: TSESTree.Node,
) => {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

if ('getAncestors' in sourceCode) {
return sourceCode.getAncestors(node);
Expand All @@ -260,7 +268,7 @@ export const getDeclaredVariables = (
context: TSESLint.RuleContext<string, unknown[]>,
node: TSESTree.Node,
) => {
const sourceCode = context.getSourceCode();
const sourceCode = getSourceCode(context);

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

0 comments on commit 86197f6

Please sign in to comment.