From 538165557c9ab3a0558fcc5112750edbb9c8b3f0 Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Wed, 26 May 2021 16:38:11 +0200 Subject: [PATCH] Improve no-nested-tests performance --- lib/rules/no-nested-tests.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/rules/no-nested-tests.js b/lib/rules/no-nested-tests.js index e623604..e554fb9 100644 --- a/lib/rules/no-nested-tests.js +++ b/lib/rules/no-nested-tests.js @@ -15,6 +15,8 @@ module.exports = { const astUtils = createAstUtils(context.settings); let testNestingLevel = 0; let hookCallNestingLevel = 0; + const isTestCase = astUtils.buildIsTestCaseAnswerer(); + const isDescribe = astUtils.buildIsDescribeAnswerer(); function report(callExpression, message) { context.report({ @@ -23,26 +25,26 @@ module.exports = { }); } - function isNestedTest(isTestCase, isDescribe, nestingLevel) { + function isNestedTest(_isTestCase, _isDescribe, nestingLevel) { const isNested = nestingLevel > 0; - const isTest = isTestCase || isDescribe; + const isTest = _isTestCase || _isDescribe; return isNested && isTest; } function checkForAndReportErrors( node, - isTestCase, - isDescribe, + _isTestCase, + _isDescribe, isHookCall ) { - if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) { - const message = isDescribe ? + 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) + isNestedTest(_isTestCase, isHookCall, hookCallNestingLevel) ) { const message = isHookCall ? 'Unexpected test hook nested within a test hook.' : @@ -53,18 +55,18 @@ module.exports = { return { CallExpression(node) { - const isTestCase = astUtils.isTestCase(node); + const _isTestCase = isTestCase(node); const isHookCall = astUtils.isHookCall(node); - const isDescribe = astUtils.isDescribe(node); + const _isDescribe = isDescribe(node); checkForAndReportErrors( node, - isTestCase, - isDescribe, + _isTestCase, + _isDescribe, isHookCall ); - if (isTestCase) { + if (_isTestCase) { testNestingLevel += 1; } else if (isHookCall) { hookCallNestingLevel += 1; @@ -72,7 +74,7 @@ module.exports = { }, 'CallExpression:exit'(node) { - if (astUtils.isTestCase(node)) { + if (isTestCase(node)) { testNestingLevel -= 1; } else if (astUtils.isHookCall(node)) { hookCallNestingLevel -= 1;