Skip to content

Commit

Permalink
Check scope references as late as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
lo1tuma committed May 26, 2021
1 parent b5b97b3 commit 05e1275
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/rules/no-mocha-arrows.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ module.exports = {

return {
CallExpression(node) {
if (isMochaFunctionCall(node, context.getScope())) {
if (isMochaFunctionCall(node, context)) {
const amountOfArguments = node.arguments.length;

if (amountOfArguments > 0) {
Expand Down
18 changes: 15 additions & 3 deletions lib/rules/no-nested-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,20 @@ module.exports = {
return isNested && isTest;
}

function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) {
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)) {
} else if (
isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)
) {
const message = isHookCall ?
'Unexpected test hook nested within a test hook.' :
'Unexpected test nested within a test hook.';
Expand All @@ -50,7 +57,12 @@ module.exports = {
const isHookCall = astUtils.isHookCall(node);
const isDescribe = astUtils.isDescribe(node);

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

if (isTestCase) {
testNestingLevel += 1;
Expand Down
16 changes: 12 additions & 4 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,20 @@ function createAstUtils(settings) {
}

function buildIsMochaFunctionCallAnswerer(_isTestCase, _isDescribe) {
return (node, scope) => {
if (isCallToShadowedReference(node, scope)) {
return false;
function _isMochaFunctionCall(node) {
return _isTestCase(node) || _isDescribe(node) || isHookCall(node);
}

return (node, context) => {
if (_isMochaFunctionCall(node)) {
const scope = context.getScope();

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

return _isTestCase(node) || _isDescribe(node) || isHookCall(node);
return false;
};
}

Expand Down

0 comments on commit 05e1275

Please sign in to comment.