Skip to content

Commit 36c9e67

Browse files
authored
Merge pull request #235 from brettz9/nondeprecated-rule-format
Nondeprecated rule format
2 parents 8cf8640 + 471e354 commit 36c9e67

11 files changed

+326
-304
lines changed

lib/rules/handle-done-callback.js

+34-32
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,48 @@
33
const find = require('ramda/src/find');
44
const astUtils = require('../util/ast');
55

6-
module.exports = function (context) {
7-
function isAsyncFunction(functionExpression) {
8-
return functionExpression.params.length === 1;
9-
}
6+
module.exports = {
7+
create(context) {
8+
function isAsyncFunction(functionExpression) {
9+
return functionExpression.params.length === 1;
10+
}
1011

11-
function findParamInScope(paramName, scope) {
12-
return find(function (variable) {
13-
return variable.name === paramName && variable.defs[0].type === 'Parameter';
14-
}, scope.variables);
15-
}
12+
function findParamInScope(paramName, scope) {
13+
return find(function (variable) {
14+
return variable.name === paramName && variable.defs[0].type === 'Parameter';
15+
}, scope.variables);
16+
}
1617

17-
function isReferenceHandled(reference) {
18-
const parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent;
18+
function isReferenceHandled(reference) {
19+
const parent = context.getNodeByRangeIndex(reference.identifier.range[0]).parent;
1920

20-
return parent.type === 'CallExpression';
21-
}
21+
return parent.type === 'CallExpression';
22+
}
2223

23-
function hasHandledReferences(references) {
24-
return references.some(isReferenceHandled);
25-
}
24+
function hasHandledReferences(references) {
25+
return references.some(isReferenceHandled);
26+
}
2627

27-
function checkAsyncMochaFunction(functionExpression) {
28-
const scope = context.getScope();
29-
const callback = functionExpression.params[0];
30-
const callbackName = callback.name;
31-
const callbackVariable = findParamInScope(callbackName, scope);
28+
function checkAsyncMochaFunction(functionExpression) {
29+
const scope = context.getScope();
30+
const callback = functionExpression.params[0];
31+
const callbackName = callback.name;
32+
const callbackVariable = findParamInScope(callbackName, scope);
3233

33-
if (callbackVariable && !hasHandledReferences(callbackVariable.references)) {
34-
context.report(callback, 'Expected "{{name}}" callback to be handled.', { name: callbackName });
34+
if (callbackVariable && !hasHandledReferences(callbackVariable.references)) {
35+
context.report(callback, 'Expected "{{name}}" callback to be handled.', { name: callbackName });
36+
}
3537
}
36-
}
3738

38-
function check(node) {
39-
if (astUtils.hasParentMochaFunctionCall(node) && isAsyncFunction(node)) {
40-
checkAsyncMochaFunction(node);
39+
function check(node) {
40+
if (astUtils.hasParentMochaFunctionCall(node) && isAsyncFunction(node)) {
41+
checkAsyncMochaFunction(node);
42+
}
4143
}
42-
}
4344

44-
return {
45-
FunctionExpression: check,
46-
ArrowFunctionExpression: check
47-
};
45+
return {
46+
FunctionExpression: check,
47+
ArrowFunctionExpression: check
48+
};
49+
}
4850
};

lib/rules/no-global-tests.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
const astUtils = require('../util/ast');
44

5-
module.exports = function (context) {
6-
function isGlobalScope(scope) {
7-
return scope.type === 'global' || scope.type === 'module';
8-
}
5+
module.exports = {
6+
create(context) {
7+
function isGlobalScope(scope) {
8+
return scope.type === 'global' || scope.type === 'module';
9+
}
910

10-
return {
11-
CallExpression(node) {
12-
const callee = node.callee;
13-
const scope = context.getScope();
11+
return {
12+
CallExpression(node) {
13+
const callee = node.callee;
14+
const scope = context.getScope();
1415

15-
if (astUtils.isTestCase(node) && isGlobalScope(scope)) {
16-
context.report(callee, 'Unexpected global mocha test.');
16+
if (astUtils.isTestCase(node) && isGlobalScope(scope)) {
17+
context.report(callee, 'Unexpected global mocha test.');
18+
}
1719
}
18-
}
19-
};
20+
};
21+
}
2022
};

lib/rules/no-hooks.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22

33
const astUtil = require('../util/ast');
44

5-
module.exports = function (context) {
6-
return {
7-
CallExpression(node) {
8-
if (astUtil.isHookIdentifier(node.callee)) {
9-
context.report({
10-
node: node.callee,
11-
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook`
12-
});
5+
module.exports = {
6+
create(context) {
7+
return {
8+
CallExpression(node) {
9+
if (astUtil.isHookIdentifier(node.callee)) {
10+
context.report({
11+
node: node.callee,
12+
message: `Unexpected use of Mocha \`${ node.callee.name }\` hook`
13+
});
14+
}
1315
}
14-
}
15-
};
16+
};
17+
}
1618
};

lib/rules/no-identical-title.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,31 @@ function isFirstArgLiteral(node) {
4141
return node.arguments && node.arguments[0] && node.arguments[0].type === 'Literal';
4242
}
4343

44-
module.exports = function (context) {
45-
const titleLayers = [ newLayer() ];
46-
const settings = context.settings;
44+
module.exports = {
45+
create(context) {
46+
const titleLayers = [ newLayer() ];
47+
const settings = context.settings;
4748

48-
return {
49-
CallExpression(node) {
50-
const currentLayer = titleLayers[titleLayers.length - 1];
49+
return {
50+
CallExpression(node) {
51+
const currentLayer = titleLayers[titleLayers.length - 1];
5152

52-
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
53-
titleLayers.push(newLayer());
54-
}
55-
if (!isFirstArgLiteral(node)) {
56-
return;
57-
}
53+
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
54+
titleLayers.push(newLayer());
55+
}
56+
if (!isFirstArgLiteral(node)) {
57+
return;
58+
}
5859

59-
const title = node.arguments[0].value;
60-
handlTestCaseTitles(context, currentLayer.testTitles, node, title);
61-
handlTestSuiteTitles(context, currentLayer.describeTitles, node, title);
62-
},
63-
'CallExpression:exit'(node) {
64-
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
65-
titleLayers.pop();
60+
const title = node.arguments[0].value;
61+
handlTestCaseTitles(context, currentLayer.testTitles, node, title);
62+
handlTestSuiteTitles(context, currentLayer.describeTitles, node, title);
63+
},
64+
'CallExpression:exit'(node) {
65+
if (astUtil.isDescribe(node, additionalSuiteNames(settings))) {
66+
titleLayers.pop();
67+
}
6668
}
67-
}
68-
};
69+
};
70+
}
6971
};

lib/rules/no-nested-tests.js

+48-46
Original file line numberDiff line numberDiff line change
@@ -5,60 +5,62 @@
55
const astUtils = require('../util/ast');
66
const { additionalSuiteNames } = require('../util/settings');
77

8-
module.exports = function noNestedTests(context) {
9-
const settings = context.settings;
10-
let testNestingLevel = 0;
11-
let hookCallNestingLevel = 0;
8+
module.exports = {
9+
create(context) {
10+
const settings = context.settings;
11+
let testNestingLevel = 0;
12+
let hookCallNestingLevel = 0;
1213

13-
function report(callExpression, message) {
14-
context.report({
15-
message,
16-
node: callExpression.callee
17-
});
18-
}
14+
function report(callExpression, message) {
15+
context.report({
16+
message,
17+
node: callExpression.callee
18+
});
19+
}
1920

20-
function isNestedTest(isTestCase, isDescribe, nestingLevel) {
21-
const isNested = nestingLevel > 0;
22-
const isTest = isTestCase || isDescribe;
21+
function isNestedTest(isTestCase, isDescribe, nestingLevel) {
22+
const isNested = nestingLevel > 0;
23+
const isTest = isTestCase || isDescribe;
2324

24-
return isNested && isTest;
25-
}
25+
return isNested && isTest;
26+
}
2627

27-
function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) {
28-
if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) {
29-
const message = isDescribe ?
30-
'Unexpected suite nested within a test.' :
31-
'Unexpected test nested within another test.';
32-
report(node, message);
33-
} else if (isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)) {
34-
const message = isHookCall ?
35-
'Unexpected test hook nested within a test hook.' :
36-
'Unexpected test nested within a test hook.';
37-
report(node, message);
28+
function checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall) {
29+
if (isNestedTest(isTestCase, isDescribe, testNestingLevel)) {
30+
const message = isDescribe ?
31+
'Unexpected suite nested within a test.' :
32+
'Unexpected test nested within another test.';
33+
report(node, message);
34+
} else if (isNestedTest(isTestCase, isHookCall, hookCallNestingLevel)) {
35+
const message = isHookCall ?
36+
'Unexpected test hook nested within a test hook.' :
37+
'Unexpected test nested within a test hook.';
38+
report(node, message);
39+
}
3840
}
39-
}
4041

41-
return {
42-
CallExpression(node) {
43-
const isTestCase = astUtils.isTestCase(node);
44-
const isHookCall = astUtils.isHookCall(node);
45-
const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings));
42+
return {
43+
CallExpression(node) {
44+
const isTestCase = astUtils.isTestCase(node);
45+
const isHookCall = astUtils.isHookCall(node);
46+
const isDescribe = astUtils.isDescribe(node, additionalSuiteNames(settings));
4647

47-
checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall);
48+
checkForAndReportErrors(node, isTestCase, isDescribe, isHookCall);
4849

49-
if (isTestCase) {
50-
testNestingLevel += 1;
51-
} else if (isHookCall) {
52-
hookCallNestingLevel += 1;
53-
}
54-
},
50+
if (isTestCase) {
51+
testNestingLevel += 1;
52+
} else if (isHookCall) {
53+
hookCallNestingLevel += 1;
54+
}
55+
},
5556

56-
'CallExpression:exit'(node) {
57-
if (astUtils.isTestCase(node)) {
58-
testNestingLevel -= 1;
59-
} else if (astUtils.isHookCall(node)) {
60-
hookCallNestingLevel -= 1;
57+
'CallExpression:exit'(node) {
58+
if (astUtils.isTestCase(node)) {
59+
testNestingLevel -= 1;
60+
} else if (astUtils.isHookCall(node)) {
61+
hookCallNestingLevel -= 1;
62+
}
6163
}
62-
}
63-
};
64+
};
65+
}
6466
};

lib/rules/no-pending-tests.js

+17-15
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@
22

33
const astUtils = require('../util/ast');
44

5-
module.exports = function (context) {
6-
function isPendingMochaTest(node) {
7-
return astUtils.isTestCase(node) &&
8-
node.arguments.length === 1 &&
9-
node.arguments[0].type === 'Literal';
10-
}
5+
module.exports = {
6+
create(context) {
7+
function isPendingMochaTest(node) {
8+
return astUtils.isTestCase(node) &&
9+
node.arguments.length === 1 &&
10+
node.arguments[0].type === 'Literal';
11+
}
1112

12-
return {
13-
CallExpression(node) {
14-
if (node.callee && isPendingMochaTest(node)) {
15-
context.report({
16-
node,
17-
message: 'Unexpected pending mocha test.'
18-
});
13+
return {
14+
CallExpression(node) {
15+
if (node.callee && isPendingMochaTest(node)) {
16+
context.report({
17+
node,
18+
message: 'Unexpected pending mocha test.'
19+
});
20+
}
1921
}
20-
}
21-
};
22+
};
23+
}
2224
};

lib/rules/no-return-and-callback.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,21 @@ function reportIfFunctionWithBlock(context, node, doneName) {
3939
}
4040
}
4141

42-
module.exports = function (context) {
43-
function check(node) {
44-
if (node.params.length === 0 || !astUtils.hasParentMochaFunctionCall(node)) {
45-
return;
42+
module.exports = {
43+
create(context) {
44+
function check(node) {
45+
if (node.params.length === 0 || !astUtils.hasParentMochaFunctionCall(node)) {
46+
return;
47+
}
48+
49+
if (!reportIfShortArrowFunction(context, node)) {
50+
reportIfFunctionWithBlock(context, node, node.params[0].name);
51+
}
4652
}
4753

48-
if (!reportIfShortArrowFunction(context, node)) {
49-
reportIfFunctionWithBlock(context, node, node.params[0].name);
50-
}
54+
return {
55+
FunctionExpression: check,
56+
ArrowFunctionExpression: check
57+
};
5158
}
52-
53-
return {
54-
FunctionExpression: check,
55-
ArrowFunctionExpression: check
56-
};
5759
};

0 commit comments

Comments
 (0)