/
consistent-spacing-between-blocks.js
74 lines (66 loc) · 2.29 KB
/
consistent-spacing-between-blocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
'use strict';
/* eslint "complexity": [ "error", 6 ] */
exports.meta = {
type: 'suggestion',
fixable: 'whitespace',
schema: [],
docs: {
description: 'Require consistent spacing between blocks',
url:
'https://github.com/lo1tuma/eslint-plugin-mocha/blob/main/docs/rules/' +
'consistent-spacing-between-blocks.md'
}
};
// List of Mocha functions that should have a line break before them.
const MOCHA_FUNCTIONS = [
'before',
'after',
'describe',
'it',
'beforeEach',
'afterEach'
];
// Avoids enforcing line breaks at the beginning of a block.
function isFirstStatementInScope(node) {
return node.parent.parent.body[0] === node.parent;
}
// Ensure that the rule is applied only within the context of Mocha describe blocks.
function isInsideDescribeBlock(node) {
return (
node.parent.type === 'ExpressionStatement' &&
node.parent.parent.type === 'BlockStatement' &&
(node.parent.parent.parent.type === 'ArrowFunctionExpression' ||
node.parent.parent.parent.type === 'FunctionExpression') &&
node.parent.parent.parent.parent.type === 'CallExpression' &&
node.parent.parent.parent.parent.callee.name === 'describe'
);
}
exports.create = function (context) {
return {
CallExpression(node) {
if (
!MOCHA_FUNCTIONS.includes(node.callee.name) ||
!isInsideDescribeBlock(node) ||
isFirstStatementInScope(node)
) {
return;
}
// Retrieves the token before the current node, skipping comments.
const beforeToken = context.getSourceCode().getTokenBefore(node);
// And then count the number of lines between the two.
const linesBetween = node.loc.start.line - beforeToken.loc.end.line;
if (linesBetween < 2) {
context.report({
node,
message: 'Expected line break before this statement.',
fix(fixer) {
return fixer.insertTextAfter(
beforeToken,
linesBetween === 0 ? '\n\n' : '\n'
);
}
});
}
}
};
};