From 7b072120e2f195ae991a9c4647d29f0e384bc54f Mon Sep 17 00:00:00 2001 From: Mathias Schreck Date: Fri, 29 May 2020 19:09:25 +0200 Subject: [PATCH] Fix max-top-level-suites to work with ES modules --- lib/rules/max-top-level-suites.js | 6 +++++- test/rules/max-top-level-suites.js | 27 ++++++++++++++++++++++++++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/lib/rules/max-top-level-suites.js b/lib/rules/max-top-level-suites.js index 67fb352..ce35b8f 100644 --- a/lib/rules/max-top-level-suites.js +++ b/lib/rules/max-top-level-suites.js @@ -11,6 +11,10 @@ const { additionalSuiteNames } = require('../util/settings'); const defaultSuiteLimit = 1; +function isTopLevelScope(scope) { + return scope.type === 'module' || scope.upper === null; +} + module.exports = { meta: { type: 'suggestion', @@ -43,7 +47,7 @@ module.exports = { if (astUtil.isDescribe(node, additionalSuiteNames(settings))) { const scope = context.getScope(); - if (scope.upper === null) { + if (isTopLevelScope(scope)) { topLevelDescribes.push(node); } } diff --git a/test/rules/max-top-level-suites.js b/test/rules/max-top-level-suites.js index d38bdfd..6ce54df 100644 --- a/test/rules/max-top-level-suites.js +++ b/test/rules/max-top-level-suites.js @@ -54,7 +54,21 @@ ruleTester.run('max-top-level-suites', rules['max-top-level-suites'], { } }, 'someOtherFunction();', - 'describe("top", function () {}); function foo() { describe("not necessarily top", function () {}); }' + 'describe("top", function () {}); function foo() { describe("not necessarily top", function () {}); }', + { + code: 'describe("", function () { });', + parserOptions: { + sourceType: 'module', + ecmaVersion: 2015 + } + }, + { + code: 'describe("", function () { describe("", function () {}); });', + parserOptions: { + sourceType: 'module', + ecmaVersion: 2015 + } + } ], invalid: [ @@ -65,6 +79,17 @@ ruleTester.run('max-top-level-suites', rules['max-top-level-suites'], { { message: 'The number of top-level suites is more than 1.' } ] }, + { + code: 'describe("", function () { });' + + 'describe("", function () { });', + parserOptions: { + sourceType: 'module', + ecmaVersion: 2015 + }, + errors: [ + { message: 'The number of top-level suites is more than 1.' } + ] + }, { code: 'describe("this is a test", function () { });' + 'describe("this is a different test", function () { });' +