Skip to content

Commit 8cf8640

Browse files
authored
Merge pull request #234 from brettz9/schemas-for-options
Add schemas for options (and remove for files which are using settings)
2 parents c1f8049 + fdb3843 commit 8cf8640

9 files changed

+346
-302
lines changed

lib/rules/max-top-level-suites.js

+49-34
Original file line numberDiff line numberDiff line change
@@ -11,43 +11,58 @@ const { additionalSuiteNames } = require('../util/settings');
1111

1212
const defaultSuiteLimit = 1;
1313

14-
module.exports = function (context) {
15-
const stack = [];
16-
const topLevelDescribes = [];
17-
const options = context.options[0] || {};
18-
const settings = context.settings;
19-
let suiteLimit;
20-
21-
if (isNil(options.limit)) {
22-
suiteLimit = defaultSuiteLimit;
23-
} else {
24-
suiteLimit = options.limit;
25-
}
26-
27-
return {
28-
CallExpression(node) {
29-
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
30-
stack.push(node);
14+
module.exports = {
15+
meta: {
16+
schema: [
17+
{
18+
type: 'object',
19+
properties: {
20+
limit: {
21+
type: 'integer'
22+
}
23+
},
24+
additionalProperties: false
3125
}
32-
},
26+
]
27+
},
28+
create(context) {
29+
const stack = [];
30+
const topLevelDescribes = [];
31+
const options = context.options[0] || {};
32+
const settings = context.settings;
33+
let suiteLimit;
34+
35+
if (isNil(options.limit)) {
36+
suiteLimit = defaultSuiteLimit;
37+
} else {
38+
suiteLimit = options.limit;
39+
}
3340

34-
'CallExpression:exit'(node) {
35-
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
36-
if (stack.length === 1) {
37-
topLevelDescribes.push(node);
41+
return {
42+
CallExpression(node) {
43+
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
44+
stack.push(node);
3845
}
46+
},
3947

40-
stack.pop(node);
41-
}
42-
},
43-
44-
'Program:exit'() {
45-
if (topLevelDescribes.length > suiteLimit) {
46-
context.report({
47-
node: topLevelDescribes[suiteLimit],
48-
message: `The number of top-level suites is more than ${ suiteLimit }.`
49-
});
48+
'CallExpression:exit'(node) {
49+
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
50+
if (stack.length === 1) {
51+
topLevelDescribes.push(node);
52+
}
53+
54+
stack.pop(node);
55+
}
56+
},
57+
58+
'Program:exit'() {
59+
if (topLevelDescribes.length > suiteLimit) {
60+
context.report({
61+
node: topLevelDescribes[suiteLimit],
62+
message: `The number of top-level suites is more than ${ suiteLimit }.`
63+
});
64+
}
5065
}
51-
}
52-
};
66+
};
67+
}
5368
};

lib/rules/no-exclusive-tests.js

+39-53
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,48 @@
33
const { getAdditionalTestFunctions } = require('../util/settings');
44
const astUtils = require('../util/ast');
55

6-
module.exports = function (context) {
7-
let mochaTestFunctions = [
8-
'it',
9-
'describe',
10-
'suite',
11-
'test',
12-
'context',
13-
'specify'
14-
];
15-
const settings = context.settings;
16-
const additionalTestFunctions = getAdditionalTestFunctions(settings);
17-
18-
mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions);
19-
20-
function matchesMochaTestFunction(object) {
21-
const name = astUtils.getNodeName(object);
22-
23-
return mochaTestFunctions.indexOf(name) !== -1;
24-
}
25-
26-
function isPropertyNamedOnly(property) {
27-
return property && astUtils.getPropertyName(property) === 'only';
28-
}
29-
30-
function isCallToMochasOnlyFunction(callee) {
31-
return callee.type === 'MemberExpression' &&
32-
matchesMochaTestFunction(callee.object) &&
33-
isPropertyNamedOnly(callee.property);
34-
}
6+
module.exports = {
7+
create(context) {
8+
let mochaTestFunctions = [
9+
'it',
10+
'describe',
11+
'suite',
12+
'test',
13+
'context',
14+
'specify'
15+
];
16+
const settings = context.settings;
17+
const additionalTestFunctions = getAdditionalTestFunctions(settings);
18+
19+
mochaTestFunctions = mochaTestFunctions.concat(additionalTestFunctions);
20+
21+
function matchesMochaTestFunction(object) {
22+
const name = astUtils.getNodeName(object);
23+
24+
return mochaTestFunctions.indexOf(name) !== -1;
25+
}
3526

36-
return {
37-
CallExpression(node) {
38-
const callee = node.callee;
27+
function isPropertyNamedOnly(property) {
28+
return property && astUtils.getPropertyName(property) === 'only';
29+
}
3930

40-
if (callee && isCallToMochasOnlyFunction(callee)) {
41-
context.report({
42-
node: callee.property,
43-
message: 'Unexpected exclusive mocha test.'
44-
});
45-
}
31+
function isCallToMochasOnlyFunction(callee) {
32+
return callee.type === 'MemberExpression' &&
33+
matchesMochaTestFunction(callee.object) &&
34+
isPropertyNamedOnly(callee.property);
4635
}
47-
};
48-
};
4936

50-
module.exports.schema = [
51-
{
52-
type: 'object',
53-
properties: {
54-
additionalTestFunctions: {
55-
type: 'array',
56-
items: {
57-
type: 'string'
58-
},
59-
minItems: 1,
60-
uniqueItems: true
37+
return {
38+
CallExpression(node) {
39+
const callee = node.callee;
40+
41+
if (callee && isCallToMochasOnlyFunction(callee)) {
42+
context.report({
43+
node: callee.property,
44+
message: 'Unexpected exclusive mocha test.'
45+
});
46+
}
6147
}
62-
}
48+
};
6349
}
64-
];
50+
};

lib/rules/no-hooks-for-single-case.js

+56-53
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,70 @@ function newDescribeLayer(describeNode) {
1111
};
1212
}
1313

14-
module.exports = function (context) {
15-
const options = context.options[0] || {};
16-
const allowedHooks = options.allow || [];
17-
const settings = context.settings;
18-
const layers = [];
14+
module.exports = {
15+
meta: {
16+
schema: [
17+
{
18+
type: 'object',
19+
properties: {
20+
allow: {
21+
type: 'array',
22+
items: {
23+
type: 'string'
24+
}
25+
}
26+
}
27+
}
28+
]
29+
},
30+
create(context) {
31+
const options = context.options[0] || {};
32+
const allowedHooks = options.allow || [];
33+
const settings = context.settings;
34+
const layers = [];
1935

20-
function popLayer(node) {
21-
const layer = layers[layers.length - 1];
22-
if (layer.describeNode === node) {
23-
if (layer.testCount <= 1) {
24-
layer.hookNodes
25-
.filter(function (hookNode) {
26-
return allowedHooks.indexOf(hookNode.name) === -1;
27-
})
28-
.forEach(function (hookNode) {
29-
context.report({
30-
node: hookNode,
31-
message: `Unexpected use of Mocha \`${ hookNode.name }\` hook for a single test case`
36+
function popLayer(node) {
37+
const layer = layers[layers.length - 1];
38+
if (layer.describeNode === node) {
39+
if (layer.testCount <= 1) {
40+
layer.hookNodes
41+
.filter(function (hookNode) {
42+
return allowedHooks.indexOf(hookNode.name) === -1;
43+
})
44+
.forEach(function (hookNode) {
45+
context.report({
46+
node: hookNode,
47+
message: `Unexpected use of Mocha \`${ hookNode.name }\` hook for a single test case`
48+
});
3249
});
33-
});
50+
}
51+
layers.pop();
3452
}
35-
layers.pop();
3653
}
37-
}
3854

39-
return {
40-
Program(node) {
41-
layers.push(newDescribeLayer(node));
42-
},
43-
44-
CallExpression(node) {
45-
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
46-
layers[layers.length - 1].testCount += 1;
55+
return {
56+
Program(node) {
4757
layers.push(newDescribeLayer(node));
48-
return;
49-
}
50-
51-
if (astUtil.isTestCase(node)) {
52-
layers[layers.length - 1].testCount += 1;
53-
}
58+
},
5459

55-
if (astUtil.isHookIdentifier(node.callee)) {
56-
layers[layers.length - 1].hookNodes.push(node.callee);
57-
}
58-
},
60+
CallExpression(node) {
61+
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
62+
layers[layers.length - 1].testCount += 1;
63+
layers.push(newDescribeLayer(node));
64+
return;
65+
}
5966

60-
'CallExpression:exit': popLayer,
61-
'Program:exit': popLayer
62-
};
63-
};
67+
if (astUtil.isTestCase(node)) {
68+
layers[layers.length - 1].testCount += 1;
69+
}
6470

65-
module.exports.schema = [
66-
{
67-
type: 'object',
68-
properties: {
69-
allow: {
70-
type: 'array',
71-
items: {
72-
type: 'string'
71+
if (astUtil.isHookIdentifier(node.callee)) {
72+
layers[layers.length - 1].hookNodes.push(node.callee);
7373
}
74-
}
75-
}
74+
},
75+
76+
'CallExpression:exit': popLayer,
77+
'Program:exit': popLayer
78+
};
7679
}
77-
];
80+
};

lib/rules/no-skipped-tests.js

-26
Original file line numberDiff line numberDiff line change
@@ -47,32 +47,6 @@ function isCallToMochaXFunction(callee) {
4747
}
4848

4949
module.exports = {
50-
meta: {
51-
fixable: 'code',
52-
schema: [
53-
{
54-
type: 'object',
55-
properties: {
56-
additionalTestFunctions: {
57-
type: 'array',
58-
items: {
59-
type: 'string'
60-
},
61-
minItems: 1,
62-
uniqueItems: true
63-
},
64-
additionalXFunctions: {
65-
type: 'array',
66-
items: {
67-
type: 'string'
68-
},
69-
minItems: 1,
70-
uniqueItems: true
71-
}
72-
}
73-
}
74-
]
75-
},
7650
create(context) {
7751
const settings = context.settings;
7852
const additionalTestFunctions = getAdditionalTestFunctions(settings);

0 commit comments

Comments
 (0)