Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schemas for options (and remove for files which are using settings) #234

Merged
merged 2 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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