Skip to content

Commit

Permalink
Fix support for ESLint versions < 8.40.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed Apr 19, 2024
1 parent bdf2006 commit e19e93e
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 98 deletions.
7 changes: 6 additions & 1 deletion lib/rules/handle-done-callback.js
Expand Up @@ -23,8 +23,10 @@ module.exports = {
}
]
},
// eslint-disable-next-line max-statements
create(context) {
const astUtils = createAstUtils(context.settings);
const { sourceCode = {} } = context;
const [ { ignoreSkipped = false } = {} ] = context.options;
const modifiersToCheck = ignoreSkipped ? [ 'only' ] : [ 'only', 'skip' ];

Expand All @@ -49,7 +51,10 @@ module.exports = {
}

function checkAsyncMochaFunction(functionExpression) {
const scope = context.sourceCode.getScope(functionExpression);
const scope = typeof sourceCode.getScope !== 'undefined' ?
sourceCode.getScope(functionExpression) :
context.getScope();

const callback = functionExpression.params[0];
const callbackName = callback.name;
const callbackVariable = findParamInScope(callbackName, scope);
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/max-top-level-suites.js
Expand Up @@ -34,6 +34,7 @@ module.exports = {
]
},
create(context) {
const { sourceCode = {} } = context;
const astUtils = createAstUtils(context.settings);
const topLevelDescribes = [];
const options = context.options[0] || {};
Expand All @@ -48,7 +49,9 @@ module.exports = {
return {
CallExpression(node) {
if (astUtils.isDescribe(node)) {
const scope = context.sourceCode.getScope(node);
const scope = typeof sourceCode.getScope !== 'undefined' ?
sourceCode.getScope(node) :
context.getScope();

if (isTopLevelScope(scope)) {
topLevelDescribes.push(node);
Expand Down
11 changes: 7 additions & 4 deletions lib/rules/no-empty-description.js
Expand Up @@ -66,24 +66,27 @@ module.exports = {
},
create(context) {
const options = context.options[0];
const { sourceCode = {} } = context;

const { testNames, message } = objectOptions(options);

function isTest(node) {
return node.callee && node.callee.name && testNames.includes(node.callee.name);
}

// eslint-disable-next-line complexity
function isNonEmptyDescription(mochaCallExpression) {
const description = mochaCallExpression.arguments[0];

if (!isValidDescriptionArgumentNode(description)) {
return false;
}

const text = getStringIfConstant(
description,
context.sourceCode.getScope(mochaCallExpression)
);
const scope = typeof sourceCode.getScope !== 'undefined' ?
sourceCode.getScope(mochaCallExpression) :
context.getScope();

const text = getStringIfConstant(description, scope);

if (!isStaticallyAnalyzableDescription(description, text)) {
return true;
Expand Down
5 changes: 4 additions & 1 deletion lib/rules/no-global-tests.js
Expand Up @@ -13,6 +13,7 @@ module.exports = {
},
create(context) {
const astUtils = createAstUtils(context.settings);
const { sourceCode = {} } = context;

function isGlobalScope(scope) {
return scope.type === 'global' || scope.type === 'module';
Expand All @@ -21,7 +22,9 @@ module.exports = {
return {
CallExpression(node) {
const callee = node.callee;
const scope = context.sourceCode.getScope(node);
const scope = typeof sourceCode.getScope !== 'undefined' ?
sourceCode.getScope(node) :
context.getScope();

if (astUtils.isTestCase(node) && isGlobalScope(scope)) {
context.report({ node: callee, message: 'Unexpected global mocha test.' });
Expand Down
10 changes: 8 additions & 2 deletions lib/rules/prefer-arrow-callback.js
Expand Up @@ -149,6 +149,7 @@ module.exports = {
const allowUnboundThis = options.allowUnboundThis !== false;
const allowNamedFunctions = options.allowNamedFunctions;
const sourceCode = context.getSourceCode();
const { sourceCode: sourceCodeFromContext = {} } = context;
const isTestCase = astUtils.buildIsTestCaseAnswerer();
const isDescribe = astUtils.buildIsDescribeAnswerer();
const isMochaFunctionCall = astUtils.buildIsMochaFunctionCallAnswerer(
Expand Down Expand Up @@ -286,14 +287,19 @@ module.exports = {
}

// Skip recursive functions.
const nameVar = context.sourceCode.getDeclaredVariables(node)[0];
const nameVar = typeof sourceCodeFromContext.getDeclaredVariables !== 'undefined' ?
sourceCodeFromContext.getDeclaredVariables(node)[0] :
context.getDeclaredVariables(node)[0];

if (isFunctionName(nameVar) && nameVar.references.length > 0) {
return;
}

// Skip if it's using arguments.
const variable = getVariableOfArguments(context.sourceCode.getScope(node));
const scope = typeof sourceCodeFromContext.getScope !== 'undefined' ?
sourceCodeFromContext.getScope(node) :
context.getScope();
const variable = getVariableOfArguments(scope);

if (variable && variable.references.length > 0) {
return;
Expand Down
10 changes: 6 additions & 4 deletions lib/rules/valid-suite-description.js
Expand Up @@ -71,6 +71,7 @@ module.exports = {
},
create(context) {
const options = context.options[0];
const { sourceCode = {} } = context;

const { pattern, suiteNames, message } = typeof options === 'object' ?
objectOptions(options) :
Expand All @@ -83,10 +84,11 @@ module.exports = {
function hasValidSuiteDescription(mochaCallExpression) {
const args = mochaCallExpression.arguments;
const descriptionArgument = args[0];
const description = getStringIfConstant(
descriptionArgument,
context.sourceCode.getScope(mochaCallExpression)
);
const scope = typeof sourceCode.getScope !== 'undefined' ?
sourceCode.getScope(mochaCallExpression) :
context.getScope();

const description = getStringIfConstant(descriptionArgument, scope);

if (description) {
return pattern.test(description);
Expand Down
10 changes: 6 additions & 4 deletions lib/rules/valid-test-description.js
Expand Up @@ -70,6 +70,7 @@ module.exports = {
},
create(context) {
const options = context.options[0];
const { sourceCode = {} } = context;

const { pattern, testNames, message } = typeof options === 'object' ?
objectOptions(options) :
Expand All @@ -82,10 +83,11 @@ module.exports = {
function hasValidTestDescription(mochaCallExpression) {
const args = mochaCallExpression.arguments;
const testDescriptionArgument = args[0];
const description = getStringIfConstant(
testDescriptionArgument,
context.sourceCode.getScope(mochaCallExpression)
);
const scope = typeof sourceCode.getScope !== 'undefined' ?
sourceCode.getScope(mochaCallExpression) :
context.getScope();

const description = getStringIfConstant(testDescriptionArgument, scope);

if (description) {
return pattern.test(description);
Expand Down
5 changes: 4 additions & 1 deletion lib/util/ast.js
Expand Up @@ -133,8 +133,11 @@ function createAstUtils(settings) {
}

return (node, context) => {
const { sourceCode = {} } = context;
if (isMochaFunctionCall(node)) {
const scope = context.sourceCode.getScope(node);
const scope = typeof sourceCode.getScope !== 'undefined' ?
sourceCode.getScope(node) :
context.getScope();

if (!isCallToShadowedReference(node, scope)) {
return true;
Expand Down

0 comments on commit e19e93e

Please sign in to comment.