-
Notifications
You must be signed in to change notification settings - Fork 1
/
two-lines-between-class-members.js
44 lines (40 loc) 路 1.41 KB
/
two-lines-between-class-members.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
module.exports = {
meta: {
type: "layout",
docs: {
description: "enforce two line breaks between class members",
category: "Stylistic Issues",
recommended: false,
},
},
create(context) {
return {
MethodDefinition(node) {
const sourceCode = context.getSourceCode();
const lines = sourceCode.lines;
const parent = node.parent;
// Ensure we are in a class and not the first method
if (parent && parent.type === "ClassBody" && parent.body[0] !== node) {
const methodIndex = parent.body.indexOf(node);
const previousMethod = parent.body[methodIndex - 1];
const lineOfCurrentMethod = node.loc.start.line;
const lineOfPreviousMethodEnd = previousMethod.loc.end.line;
// Count the empty lines between the end of the previous method and the start of the current one
let emptyLineCount = 0;
for (let i = lineOfPreviousMethodEnd; i < lineOfCurrentMethod; i++) {
if (lines[i].trim() === "" && !lines[i].includes("*")) {
emptyLineCount++;
}
}
// Report an error if there are not exactly two empty lines
if (emptyLineCount !== 2) {
context.report({
node,
message: "Expected exactly two line breaks between class methods.",
});
}
}
},
};
},
};