]> git.proxmox.com Git - pve-eslint.git/blame - eslint/lib/rules/lines-between-class-members.js
import 8.23.1 source
[pve-eslint.git] / eslint / lib / rules / lines-between-class-members.js
CommitLineData
eb39fafa
DC
1/**
2 * @fileoverview Rule to check empty newline between class members
3 * @author 薛定谔的猫<hh_2013@foxmail.com>
4 */
5"use strict";
6
609c276f
TL
7//------------------------------------------------------------------------------
8// Requirements
9//------------------------------------------------------------------------------
10
eb39fafa
DC
11const astUtils = require("./utils/ast-utils");
12
13//------------------------------------------------------------------------------
14// Rule Definition
15//------------------------------------------------------------------------------
16
34eeec05 17/** @type {import('../shared/types').Rule} */
eb39fafa
DC
18module.exports = {
19 meta: {
20 type: "layout",
21
22 docs: {
8f9d1d4d 23 description: "Require or disallow an empty line between class members",
eb39fafa
DC
24 recommended: false,
25 url: "https://eslint.org/docs/rules/lines-between-class-members"
26 },
27
28 fixable: "whitespace",
29
30 schema: [
31 {
32 enum: ["always", "never"]
33 },
34 {
35 type: "object",
36 properties: {
37 exceptAfterSingleLine: {
38 type: "boolean",
39 default: false
40 }
41 },
42 additionalProperties: false
43 }
44 ],
45 messages: {
46 never: "Unexpected blank line between class members.",
47 always: "Expected blank line between class members."
48 }
49 },
50
51 create(context) {
52
53 const options = [];
54
55 options[0] = context.options[0] || "always";
56 options[1] = context.options[1] || { exceptAfterSingleLine: false };
57
58 const sourceCode = context.getSourceCode();
59
609c276f
TL
60 /**
61 * Gets a pair of tokens that should be used to check lines between two class member nodes.
62 *
63 * In most cases, this returns the very last token of the current node and
64 * the very first token of the next node.
65 * For example:
66 *
67 * class C {
68 * x = 1; // curLast: `;` nextFirst: `in`
69 * in = 2
70 * }
71 *
72 * There is only one exception. If the given node ends with a semicolon, and it looks like
73 * a semicolon-less style's semicolon - one that is not on the same line as the preceding
74 * token, but is on the line where the next class member starts - this returns the preceding
75 * token and the semicolon as boundary tokens.
76 * For example:
77 *
78 * class C {
79 * x = 1 // curLast: `1` nextFirst: `;`
80 * ;in = 2
81 * }
82 * When determining the desired layout of the code, we should treat this semicolon as
83 * a part of the next class member node instead of the one it technically belongs to.
84 * @param {ASTNode} curNode Current class member node.
85 * @param {ASTNode} nextNode Next class member node.
86 * @returns {Token} The actual last token of `node`.
87 * @private
88 */
89 function getBoundaryTokens(curNode, nextNode) {
90 const lastToken = sourceCode.getLastToken(curNode);
91 const prevToken = sourceCode.getTokenBefore(lastToken);
92 const nextToken = sourceCode.getFirstToken(nextNode); // skip possible lone `;` between nodes
93
94 const isSemicolonLessStyle = (
95 astUtils.isSemicolonToken(lastToken) &&
96 !astUtils.isTokenOnSameLine(prevToken, lastToken) &&
97 astUtils.isTokenOnSameLine(lastToken, nextToken)
98 );
99
100 return isSemicolonLessStyle
101 ? { curLast: prevToken, nextFirst: lastToken }
102 : { curLast: lastToken, nextFirst: nextToken };
103 }
104
eb39fafa
DC
105 /**
106 * Return the last token among the consecutive tokens that have no exceed max line difference in between, before the first token in the next member.
107 * @param {Token} prevLastToken The last token in the previous member node.
108 * @param {Token} nextFirstToken The first token in the next member node.
109 * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens.
110 * @returns {Token} The last token among the consecutive tokens.
111 */
112 function findLastConsecutiveTokenAfter(prevLastToken, nextFirstToken, maxLine) {
113 const after = sourceCode.getTokenAfter(prevLastToken, { includeComments: true });
114
115 if (after !== nextFirstToken && after.loc.start.line - prevLastToken.loc.end.line <= maxLine) {
116 return findLastConsecutiveTokenAfter(after, nextFirstToken, maxLine);
117 }
118 return prevLastToken;
119 }
120
121 /**
122 * Return the first token among the consecutive tokens that have no exceed max line difference in between, after the last token in the previous member.
123 * @param {Token} nextFirstToken The first token in the next member node.
124 * @param {Token} prevLastToken The last token in the previous member node.
125 * @param {number} maxLine The maximum number of allowed line difference between consecutive tokens.
126 * @returns {Token} The first token among the consecutive tokens.
127 */
128 function findFirstConsecutiveTokenBefore(nextFirstToken, prevLastToken, maxLine) {
129 const before = sourceCode.getTokenBefore(nextFirstToken, { includeComments: true });
130
131 if (before !== prevLastToken && nextFirstToken.loc.start.line - before.loc.end.line <= maxLine) {
132 return findFirstConsecutiveTokenBefore(before, prevLastToken, maxLine);
133 }
134 return nextFirstToken;
135 }
136
137 /**
138 * Checks if there is a token or comment between two tokens.
139 * @param {Token} before The token before.
140 * @param {Token} after The token after.
141 * @returns {boolean} True if there is a token or comment between two tokens.
142 */
143 function hasTokenOrCommentBetween(before, after) {
144 return sourceCode.getTokensBetween(before, after, { includeComments: true }).length !== 0;
145 }
146
147 return {
148 ClassBody(node) {
149 const body = node.body;
150
151 for (let i = 0; i < body.length - 1; i++) {
152 const curFirst = sourceCode.getFirstToken(body[i]);
609c276f 153 const { curLast, nextFirst } = getBoundaryTokens(body[i], body[i + 1]);
eb39fafa
DC
154 const isMulti = !astUtils.isTokenOnSameLine(curFirst, curLast);
155 const skip = !isMulti && options[1].exceptAfterSingleLine;
156 const beforePadding = findLastConsecutiveTokenAfter(curLast, nextFirst, 1);
157 const afterPadding = findFirstConsecutiveTokenBefore(nextFirst, curLast, 1);
158 const isPadded = afterPadding.loc.start.line - beforePadding.loc.end.line > 1;
159 const hasTokenInPadding = hasTokenOrCommentBetween(beforePadding, afterPadding);
160 const curLineLastToken = findLastConsecutiveTokenAfter(curLast, nextFirst, 0);
161
162 if ((options[0] === "always" && !skip && !isPadded) ||
163 (options[0] === "never" && isPadded)) {
164 context.report({
165 node: body[i + 1],
166 messageId: isPadded ? "never" : "always",
167 fix(fixer) {
168 if (hasTokenInPadding) {
169 return null;
170 }
171 return isPadded
172 ? fixer.replaceTextRange([beforePadding.range[1], afterPadding.range[0]], "\n")
173 : fixer.insertTextAfter(curLineLastToken, "\n");
174 }
175 });
176 }
177 }
178 }
179 };
180 }
181};