Skip to content

Commit

Permalink
Merge pull request #234 from brettz9/schemas-for-options
Browse files Browse the repository at this point in the history
Add schemas for options (and remove for files which are using settings)
  • Loading branch information
lo1tuma authored Feb 18, 2020
2 parents c1f8049 + fdb3843 commit 8cf8640
Show file tree
Hide file tree
Showing 9 changed files with 346 additions and 302 deletions.
83 changes: 49 additions & 34 deletions lib/rules/max-top-level-suites.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,58 @@ const { additionalSuiteNames } = require('../util/settings');

const defaultSuiteLimit = 1;

module.exports = function (context) {
const stack = [];
const topLevelDescribes = [];
const options = context.options[0] || {};
const settings = context.settings;
let suiteLimit;

if (isNil(options.limit)) {
suiteLimit = defaultSuiteLimit;
} else {
suiteLimit = options.limit;
}

return {
CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
stack.push(node);
module.exports = {
meta: {
schema: [
{
type: 'object',
properties: {
limit: {
type: 'integer'
}
},
additionalProperties: false
}
},
]
},
create(context) {
const stack = [];
const topLevelDescribes = [];
const options = context.options[0] || {};
const settings = context.settings;
let suiteLimit;

if (isNil(options.limit)) {
suiteLimit = defaultSuiteLimit;
} else {
suiteLimit = options.limit;
}

'CallExpression:exit'(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
if (stack.length === 1) {
topLevelDescribes.push(node);
return {
CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
stack.push(node);
}
},

stack.pop(node);
}
},

'Program:exit'() {
if (topLevelDescribes.length > suiteLimit) {
context.report({
node: topLevelDescribes[suiteLimit],
message: `The number of top-level suites is more than ${ suiteLimit }.`
});
'CallExpression:exit'(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
if (stack.length === 1) {
topLevelDescribes.push(node);
}

stack.pop(node);
}
},

'Program:exit'() {
if (topLevelDescribes.length > suiteLimit) {
context.report({
node: topLevelDescribes[suiteLimit],
message: `The number of top-level suites is more than ${ suiteLimit }.`
});
}
}
}
};
};
}
};
92 changes: 39 additions & 53 deletions lib/rules/no-exclusive-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,48 @@
const { getAdditionalTestFunctions } = require('../util/settings');
const astUtils = require('../util/ast');

module.exports = function (context) {
let mochaTestFunctions = [
'it',
'describe',
'suite',
'test',
'context',
'specify'
];
const settings = context.settings;
const additionalTestFunctions = getAdditionalTestFunctions(settings);

mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions);

function matchesMochaTestFunction(object) {
const name = astUtils.getNodeName(object);

return mochaTestFunctions.indexOf(name) !== -1;
}

function isPropertyNamedOnly(property) {
return property && astUtils.getPropertyName(property) === 'only';
}

function isCallToMochasOnlyFunction(callee) {
return callee.type === 'MemberExpression' &&
matchesMochaTestFunction(callee.object) &&
isPropertyNamedOnly(callee.property);
}
module.exports = {
create(context) {
let mochaTestFunctions = [
'it',
'describe',
'suite',
'test',
'context',
'specify'
];
const settings = context.settings;
const additionalTestFunctions = getAdditionalTestFunctions(settings);

mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions);

function matchesMochaTestFunction(object) {
const name = astUtils.getNodeName(object);

return mochaTestFunctions.indexOf(name) !== -1;
}

return {
CallExpression(node) {
const callee = node.callee;
function isPropertyNamedOnly(property) {
return property && astUtils.getPropertyName(property) === 'only';
}

if (callee && isCallToMochasOnlyFunction(callee)) {
context.report({
node: callee.property,
message: 'Unexpected exclusive mocha test.'
});
}
function isCallToMochasOnlyFunction(callee) {
return callee.type === 'MemberExpression' &&
matchesMochaTestFunction(callee.object) &&
isPropertyNamedOnly(callee.property);
}
};
};

module.exports.schema = [
{
type: 'object',
properties: {
additionalTestFunctions: {
type: 'array',
items: {
type: 'string'
},
minItems: 1,
uniqueItems: true
return {
CallExpression(node) {
const callee = node.callee;

if (callee && isCallToMochasOnlyFunction(callee)) {
context.report({
node: callee.property,
message: 'Unexpected exclusive mocha test.'
});
}
}
}
};
}
];
};
109 changes: 56 additions & 53 deletions lib/rules/no-hooks-for-single-case.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,70 @@ function newDescribeLayer(describeNode) {
};
}

module.exports = function (context) {
const options = context.options[0] || {};
const allowedHooks = options.allow || [];
const settings = context.settings;
const layers = [];
module.exports = {
meta: {
schema: [
{
type: 'object',
properties: {
allow: {
type: 'array',
items: {
type: 'string'
}
}
}
}
]
},
create(context) {
const options = context.options[0] || {};
const allowedHooks = options.allow || [];
const settings = context.settings;
const layers = [];

function popLayer(node) {
const layer = layers[layers.length - 1];
if (layer.describeNode === node) {
if (layer.testCount <= 1) {
layer.hookNodes
.filter(function (hookNode) {
return allowedHooks.indexOf(hookNode.name) === -1;
})
.forEach(function (hookNode) {
context.report({
node: hookNode,
message: `Unexpected use of Mocha \`${ hookNode.name }\` hook for a single test case`
function popLayer(node) {
const layer = layers[layers.length - 1];
if (layer.describeNode === node) {
if (layer.testCount <= 1) {
layer.hookNodes
.filter(function (hookNode) {
return allowedHooks.indexOf(hookNode.name) === -1;
})
.forEach(function (hookNode) {
context.report({
node: hookNode,
message: `Unexpected use of Mocha \`${ hookNode.name }\` hook for a single test case`
});
});
});
}
layers.pop();
}
layers.pop();
}
}

return {
Program(node) {
layers.push(newDescribeLayer(node));
},

CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
layers[layers.length - 1].testCount += 1;
return {
Program(node) {
layers.push(newDescribeLayer(node));
return;
}

if (astUtil.isTestCase(node)) {
layers[layers.length - 1].testCount += 1;
}
},

if (astUtil.isHookIdentifier(node.callee)) {
layers[layers.length - 1].hookNodes.push(node.callee);
}
},
CallExpression(node) {
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
layers[layers.length - 1].testCount += 1;
layers.push(newDescribeLayer(node));
return;
}

'CallExpression:exit': popLayer,
'Program:exit': popLayer
};
};
if (astUtil.isTestCase(node)) {
layers[layers.length - 1].testCount += 1;
}

module.exports.schema = [
{
type: 'object',
properties: {
allow: {
type: 'array',
items: {
type: 'string'
if (astUtil.isHookIdentifier(node.callee)) {
layers[layers.length - 1].hookNodes.push(node.callee);
}
}
}
},

'CallExpression:exit': popLayer,
'Program:exit': popLayer
};
}
];
};
26 changes: 0 additions & 26 deletions lib/rules/no-skipped-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,32 +47,6 @@ function isCallToMochaXFunction(callee) {
}

module.exports = {
meta: {
fixable: 'code',
schema: [
{
type: 'object',
properties: {
additionalTestFunctions: {
type: 'array',
items: {
type: 'string'
},
minItems: 1,
uniqueItems: true
},
additionalXFunctions: {
type: 'array',
items: {
type: 'string'
},
minItems: 1,
uniqueItems: true
}
}
}
]
},
create(context) {
const settings = context.settings;
const additionalTestFunctions = getAdditionalTestFunctions(settings);
Expand Down
Loading

0 comments on commit 8cf8640

Please sign in to comment.