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

42 行
1.1 KiB

'use strict';
const createAstUtils = require('../util/ast');
module.exports = {
meta: {
type: 'problem',
docs: {
description: 'Disallow top-level hooks'
}
},
create(context) {
const astUtils = createAstUtils(context.settings);
const testSuiteStack = [];
return {
CallExpression(node) {
if (astUtils.isDescribe(node)) {
testSuiteStack.push(node);
return;
}
if (!astUtils.isHookIdentifier(node.callee)) {
return;
}
if (testSuiteStack.length === 0) {
context.report({
node: node.callee,
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook outside of a test suite`
});
}
},
'CallExpression:exit'(node) {
if (testSuiteStack[testSuiteStack.length - 1] === node) {
testSuiteStack.pop();
}
}
};
}
};