您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

68 行
1.8 KiB

'use strict';
/**
* @fileoverview Limit the number of top-level suites in a single file
* @author Alexander Afanasyev
*/
const isNil = require('ramda/src/isNil');
const createAstUtils = require('../util/ast');
const defaultSuiteLimit = 1;
function isTopLevelScope(scope) {
return scope.type === 'module' || scope.upper === null;
}
module.exports = {
meta: {
type: 'suggestion',
docs: {
description: 'Limit the number of top-level suites in a single file'
},
schema: [
{
type: 'object',
properties: {
limit: {
type: 'integer'
}
},
additionalProperties: false
}
]
},
create(context) {
const astUtils = createAstUtils(context.settings);
const topLevelDescribes = [];
const options = context.options[0] || {};
let suiteLimit;
if (isNil(options.limit)) {
suiteLimit = defaultSuiteLimit;
} else {
suiteLimit = options.limit;
}
return {
CallExpression(node) {
if (astUtils.isDescribe(node)) {
const scope = context.getScope();
if (isTopLevelScope(scope)) {
topLevelDescribes.push(node);
}
}
},
'Program:exit'() {
if (topLevelDescribes.length > suiteLimit) {
context.report({
node: topLevelDescribes[suiteLimit],
message: `The number of top-level suites is more than ${ suiteLimit }.`
});
}
}
};
}
};