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 4195102
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 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
62 changes: 46 additions & 16 deletions lib/util/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,16 @@ const isDefined = complement(isNil);
const isCallExpression = both(isDefined, propEq('type', 'CallExpression'));

const hooks = [
'before', 'after', 'beforeEach', 'afterEach', 'beforeAll', 'afterAll',
'setup', 'teardown', 'suiteSetup', 'suiteTeardown'
'before',
'after',
'beforeEach',
'afterEach',
'beforeAll',
'afterAll',
'setup',
'teardown',
'suiteSetup',
'suiteTeardown'
];
const suiteConfig = [ 'timeout', 'slow', 'retries' ];

Expand All @@ -26,15 +34,13 @@ function getPropertyName(property) {

function getNodeName(node) {
if (node.type === 'MemberExpression') {
return `${getNodeName(node.object) }.${ getPropertyName(node.property)}`;
return `${getNodeName(node.object)}.${getPropertyName(node.property)}`;
}
return node.name;
}

function isHookIdentifier(node) {
return node &&
node.type === 'Identifier' &&
hooks.includes(node.name);
return node && node.type === 'Identifier' && hooks.includes(node.name);
}

function isHookCall(node) {
Expand All @@ -50,11 +56,16 @@ function findReference(scope, node) {
function isShadowed(scope, identifier) {
const reference = findReference(scope, identifier);

return reference && reference.resolved && reference.resolved.defs.length > 0;
return (
reference && reference.resolved && reference.resolved.defs.length > 0
);
}

function isCallToShadowedReference(node, scope) {
const identifier = node.callee.type === 'MemberExpression' ? node.callee.object : node.callee;
const identifier =
node.callee.type === 'MemberExpression' ?
node.callee.object :
node.callee;

return isShadowed(scope, identifier);
}
Expand All @@ -67,9 +78,13 @@ function isFunctionCallWithName(node, names) {
function createAstUtils(settings) {
const additionalCustomNames = getAddtionalNames(settings);

function buildIsDescribeAnswerer(options) {
function buildIsDescribeAnswerer(options = {}) {
const { modifiers = [ 'skip', 'only' ], modifiersOnly = false } = options;
const describeAliases = getSuiteNames({ modifiersOnly, modifiers, additionalCustomNames });
const describeAliases = getSuiteNames({
modifiersOnly,
modifiers,
additionalCustomNames
});

return (node) => isFunctionCallWithName(node, describeAliases);
}
Expand All @@ -80,7 +95,11 @@ function createAstUtils(settings) {

function buildIsTestCaseAnswerer(options = {}) {
const { modifiers = [ 'skip', 'only' ], modifiersOnly = false } = options;
const testCaseNames = getTestCaseNames({ modifiersOnly, modifiers, additionalCustomNames });
const testCaseNames = getTestCaseNames({
modifiersOnly,
modifiers,
additionalCustomNames
});

return (node) => isFunctionCallWithName(node, testCaseNames);
}
Expand Down Expand Up @@ -108,12 +127,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 All @@ -126,7 +153,10 @@ function createAstUtils(settings) {
}

function hasParentMochaFunctionCall(functionExpression, options) {
return isTestCase(functionExpression.parent, options) || isHookCall(functionExpression.parent);
return (
isTestCase(functionExpression.parent, options) ||
isHookCall(functionExpression.parent)
);
}

function isExplicitUndefined(node) {
Expand Down

0 comments on commit 4195102

Please sign in to comment.