Skip to content

Commit

Permalink
Merge pull request #235 from brettz9/nondeprecated-rule-format
Browse files Browse the repository at this point in the history
Nondeprecated rule format
  • Loading branch information
lo1tuma authored Feb 18, 2020
2 parents 8cf8640 + 471e354 commit 36c9e67
Show file tree
Hide file tree
Showing 11 changed files with 326 additions and 304 deletions.
66 changes: 34 additions & 32 deletions lib/rules/handle-done-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,48 @@
const find = require('ramda/src/find');
const astUtils = require('../util/ast');

module.exports = function (context) {
function isAsyncFunction(functionExpression) {
return functionExpression.params.length === 1;
}
module.exports = {
create(context) {
function isAsyncFunction(functionExpression) {
return functionExpression.params.length === 1;
}

function findParamInScope(paramName, scope) {
return find(function (variable) {
return variable.name === paramName && variable.defs[0].type === 'Parameter';
}, scope.variables);
}
function findParamInScope(paramName, scope) {
return find(function (variable) {
return variable.name === paramName && variable.defs[0].type === 'Parameter';
}, scope.variables);
}

function isReferenceHandled(reference) {
const parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent;
function isReferenceHandled(reference) {
const parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent;

return parent.type === 'CallExpression';
}
return parent.type === 'CallExpression';
}

function hasHandledReferences(references) {
return references.some(isReferenceHandled);
}
function hasHandledReferences(references) {
return references.some(isReferenceHandled);
}

function checkAsyncMochaFunction(functionExpression) {
const scope = context.getScope();
const callback = functionExpression.params[0];
const callbackName = callback.name;
const callbackVariable = findParamInScope(callbackName, scope);
function checkAsyncMochaFunction(functionExpression) {
const scope = context.getScope();
const callback = functionExpression.params[0];
const callbackName = callback.name;
const callbackVariable = findParamInScope(callbackName, scope);

if (callbackVariable && !hasHandledReferences(callbackVariable.references)) {
context.report(callback, 'Expected "{{name}}" callback to be handled.', { name: callbackName });
if (callbackVariable && !hasHandledReferences(callbackVariable.references)) {
context.report(callback, 'Expected "{{name}}" callback to be handled.', { name: callbackName });
}
}
}

function check(node) {
if (astUtils.hasParentMochaFunctionCall(node) && isAsyncFunction(node)) {
checkAsyncMochaFunction(node);
function check(node) {
if (astUtils.hasParentMochaFunctionCall(node) && isAsyncFunction(node)) {
checkAsyncMochaFunction(node);
}
}
}

return {
FunctionExpression: check,
ArrowFunctionExpression: check
};
return {
FunctionExpression: check,
ArrowFunctionExpression: check
};
}
};
26 changes: 14 additions & 12 deletions lib/rules/no-global-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

const astUtils = require('../util/ast');

module.exports = function (context) {
function isGlobalScope(scope) {
return scope.type === 'global' || scope.type === 'module';
}
module.exports = {
create(context) {
function isGlobalScope(scope) {
return scope.type === 'global' || scope.type === 'module';
}

return {
CallExpression(node) {
const callee = node.callee;
const scope = context.getScope();
return {
CallExpression(node) {
const callee = node.callee;
const scope = context.getScope();

if (astUtils.isTestCase(node) && isGlobalScope(scope)) {
context.report(callee, 'Unexpected global mocha test.');
if (astUtils.isTestCase(node) && isGlobalScope(scope)) {
context.report(callee, 'Unexpected global mocha test.');
}
}
}
};
};
}
};
22 changes: 12 additions & 10 deletions lib/rules/no-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

const astUtil = require('../util/ast');

module.exports = function (context) {
return {
CallExpression(node) {
if (astUtil.isHookIdentifier(node.callee)) {
context.report({
node: node.callee,
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook`
});
module.exports = {
create(context) {
return {
CallExpression(node) {
if (astUtil.isHookIdentifier(node.callee)) {
context.report({
node: node.callee,
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook`
});
}
}
}
};
};
}
};
44 changes: 23 additions & 21 deletions lib/rules/no-identical-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,29 +41,31 @@ function isFirstArgLiteral(node) {
return node.arguments && node.arguments[0] && node.arguments[0].type === 'Literal';
}

module.exports = function (context) {
const titleLayers = [ newLayer() ];
const settings = context.settings;
module.exports = {
create(context) {
const titleLayers = [ newLayer() ];
const settings = context.settings;

return {
CallExpression(node) {
const currentLayer = titleLayers[titleLayers.length - 1];
return {
CallExpression(node) {
const currentLayer = titleLayers[titleLayers.length - 1];

if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
titleLayers.push(newLayer());
}
if (!isFirstArgLiteral(node)) {
return;
}
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
titleLayers.push(newLayer());
}
if (!isFirstArgLiteral(node)) {
return;
}

const title = node.arguments[0].value;
handlTestCaseTitles(context, currentLayer.testTitles, node, title);
handlTestSuiteTitles(context, currentLayer.describeTitles, node, title);
},
'CallExpression:exit'(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
titleLayers.pop();
const title = node.arguments[0].value;
handlTestCaseTitles(context, currentLayer.testTitles, node, title);
handlTestSuiteTitles(context, currentLayer.describeTitles, node, title);
},
'CallExpression:exit'(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
titleLayers.pop();
}
}
}
};
};
}
};
94 changes: 48 additions & 46 deletions lib/rules/no-nested-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,62 @@
const astUtils = require('../util/ast');
const { additionalSuiteNames } = require('../util/settings');

module.exports = function noNestedTests(context) {
const settings = context.settings;
let testNestingLevel = 0;
let hookCallNestingLevel = 0;
module.exports = {
create(context) {
const settings = context.settings;
let testNestingLevel = 0;
let hookCallNestingLevel = 0;

function report(callExpression, message) {
context.report({
message,
node: callExpression.callee
});
}
function report(callExpression, message) {
context.report({
message,
node: callExpression.callee
});
}

function isNestedTest(isTestCase, isDescribe, nestingLevel) {
const isNested = nestingLevel > 0;
const isTest = isTestCase || isDescribe;
function isNestedTest(isTestCase, isDescribe, nestingLevel) {
const isNested = nestingLevel > 0;
const isTest = isTestCase || isDescribe;

return isNested && isTest;
}
return isNested && isTest;
}

function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) {
if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) {
const message = isDescribe ?
'Unexpected suite nested within a test.' :
'Unexpected test nested within another test.';
report(node, message);
} else if (isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)) {
const message = isHookCall ?
'Unexpected test hook nested within a test hook.' :
'Unexpected test nested within a test hook.';
report(node, message);
function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) {
if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) {
const message = isDescribe ?
'Unexpected suite nested within a test.' :
'Unexpected test nested within another test.';
report(node, message);
} else if (isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)) {
const message = isHookCall ?
'Unexpected test hook nested within a test hook.' :
'Unexpected test nested within a test hook.';
report(node, message);
}
}
}

return {
CallExpression(node) {
const isTestCase = astUtils.isTestCase(node);
const isHookCall = astUtils.isHookCall(node);
const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings));
return {
CallExpression(node) {
const isTestCase = astUtils.isTestCase(node);
const isHookCall = astUtils.isHookCall(node);
const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings));

checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall);
checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall);

if (isTestCase) {
testNestingLevel += 1;
} else if (isHookCall) {
hookCallNestingLevel += 1;
}
},
if (isTestCase) {
testNestingLevel += 1;
} else if (isHookCall) {
hookCallNestingLevel += 1;
}
},

'CallExpression:exit'(node) {
if (astUtils.isTestCase(node)) {
testNestingLevel -= 1;
} else if (astUtils.isHookCall(node)) {
hookCallNestingLevel -= 1;
'CallExpression:exit'(node) {
if (astUtils.isTestCase(node)) {
testNestingLevel -= 1;
} else if (astUtils.isHookCall(node)) {
hookCallNestingLevel -= 1;
}
}
}
};
};
}
};
32 changes: 17 additions & 15 deletions lib/rules/no-pending-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

const astUtils = require('../util/ast');

module.exports = function (context) {
function isPendingMochaTest(node) {
return astUtils.isTestCase(node) &&
node.arguments.length === 1 &&
node.arguments[0].type === 'Literal';
}
module.exports = {
create(context) {
function isPendingMochaTest(node) {
return astUtils.isTestCase(node) &&
node.arguments.length === 1 &&
node.arguments[0].type === 'Literal';
}

return {
CallExpression(node) {
if (node.callee && isPendingMochaTest(node)) {
context.report({
node,
message: 'Unexpected pending mocha test.'
});
return {
CallExpression(node) {
if (node.callee && isPendingMochaTest(node)) {
context.report({
node,
message: 'Unexpected pending mocha test.'
});
}
}
}
};
};
}
};
26 changes: 14 additions & 12 deletions lib/rules/no-return-and-callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,21 @@ function reportIfFunctionWithBlock(context, node, doneName) {
}
}

module.exports = function (context) {
function check(node) {
if (node.params.length === 0 || !astUtils.hasParentMochaFunctionCall(node)) {
return;
module.exports = {
create(context) {
function check(node) {
if (node.params.length === 0 || !astUtils.hasParentMochaFunctionCall(node)) {
return;
}

if (!reportIfShortArrowFunction(context, node)) {
reportIfFunctionWithBlock(context, node, node.params[0].name);
}
}

if (!reportIfShortArrowFunction(context, node)) {
reportIfFunctionWithBlock(context, node, node.params[0].name);
}
return {
FunctionExpression: check,
ArrowFunctionExpression: check
};
}

return {
FunctionExpression: check,
ArrowFunctionExpression: check
};
};
Loading

0 comments on commit 36c9e67

Please sign in to comment.