From 0419585e41dcd53921c71dfc9cf0f63871f3b637 Mon Sep 17 00:00:00 2001 From: Mark Skelton Date: Thu, 30 Nov 2023 08:31:46 -0600 Subject: [PATCH] fix: Support older ESLint versions that don't support `context.sourceCode` (#180) --- src/rules/expect-expect.ts | 7 ++++++- src/rules/prefer-to-contain.ts | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/rules/expect-expect.ts b/src/rules/expect-expect.ts index ca23584..9696f83 100644 --- a/src/rules/expect-expect.ts +++ b/src/rules/expect-expect.ts @@ -15,6 +15,7 @@ function isAssertionCall( export default { create(context) { + const sourceCode = context.sourceCode ?? context.getSourceCode(); const unchecked: ESTree.CallExpression[] = []; const additionalAssertFunctionNames = getAdditionalAssertFunctionNames(context); @@ -36,7 +37,11 @@ export default { if (isTestCall(node, ['fixme', 'only', 'skip'])) { unchecked.push(node); } else if (isAssertionCall(node, additionalAssertFunctionNames)) { - checkExpressions(context.sourceCode.getAncestors(node)); + const ancestors = sourceCode.getAncestors + ? sourceCode.getAncestors(node) + : context.getAncestors(); + + checkExpressions(ancestors); } }, 'Program:exit'() { diff --git a/src/rules/prefer-to-contain.ts b/src/rules/prefer-to-contain.ts index 93ef98f..53ceb8c 100644 --- a/src/rules/prefer-to-contain.ts +++ b/src/rules/prefer-to-contain.ts @@ -48,7 +48,7 @@ export default { context.report({ fix(fixer) { - const sourceCode = context.getSourceCode(); + const sourceCode = context.sourceCode ?? context.getSourceCode(); // We need to negate the expectation if the current expected // value is itself negated by the "not" modifier