eslint-plugin-yenz 1.1.0 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -0
- package/lib/rules/it-newline.js +68 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
meta: {
|
|
3
|
+
type: 'layout', // Since we're dealing with code formatting
|
|
4
|
+
docs: {
|
|
5
|
+
description: 'Ensure that `it()` calls in tests are preceded by a newline',
|
|
6
|
+
category: 'Stylistic Issues',
|
|
7
|
+
recommended: false, // Set to true if you want to enforce it by default
|
|
8
|
+
},
|
|
9
|
+
fixable: 'whitespace', // The rule can fix issues by modifying whitespace
|
|
10
|
+
schema: [], // No options
|
|
11
|
+
messages: {
|
|
12
|
+
missingNewline: 'There should be a blank line before `it()`.',
|
|
13
|
+
},
|
|
14
|
+
},
|
|
15
|
+
create(context) {
|
|
16
|
+
const sourceCode = context.getSourceCode();
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
const callee = node.callee;
|
|
21
|
+
|
|
22
|
+
// Check if the callee is `it`
|
|
23
|
+
const isIt =
|
|
24
|
+
(callee.type === 'Identifier' && callee.name === 'it') ||
|
|
25
|
+
// Also handle cases like `global.it` or `describe.it`
|
|
26
|
+
(callee.type === 'MemberExpression' &&
|
|
27
|
+
callee.property.type === 'Identifier' &&
|
|
28
|
+
callee.property.name === 'it');
|
|
29
|
+
|
|
30
|
+
if (!isIt) {
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// Get the token before the `it` call
|
|
35
|
+
const prevToken = sourceCode.getTokenBefore(node, {
|
|
36
|
+
includeComments: true,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
if (!prevToken) {
|
|
40
|
+
// If there's no previous token, it's likely the first statement; no need for a newline
|
|
41
|
+
return;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const prevTokenEndLine = prevToken.loc.end.line;
|
|
45
|
+
const currentTokenStartLine = node.loc.start.line;
|
|
46
|
+
|
|
47
|
+
// Calculate the number of newlines between the previous token and the current `it` call
|
|
48
|
+
const newlineCount = currentTokenStartLine - prevTokenEndLine - 1;
|
|
49
|
+
|
|
50
|
+
if (newlineCount < 1) {
|
|
51
|
+
// Report if there's no blank line before `it()`
|
|
52
|
+
context.report({
|
|
53
|
+
node,
|
|
54
|
+
messageId: 'missingNewline',
|
|
55
|
+
fix(fixer) {
|
|
56
|
+
// Insert a newline before the `it` call
|
|
57
|
+
const insertionPoint = prevToken.range[1];
|
|
58
|
+
return fixer.insertTextAfterRange(
|
|
59
|
+
[insertionPoint, insertionPoint],
|
|
60
|
+
'\n'
|
|
61
|
+
);
|
|
62
|
+
},
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
};
|
|
67
|
+
},
|
|
68
|
+
};
|