]> git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/padded-blocks.js
first commit
[pve-eslint.git] / eslint / lib / rules / padded-blocks.js
1 /**
2 * @fileoverview A rule to ensure blank lines within blocks.
3 * @author Mathias Schreck <https://github.com/lo1tuma>
4 */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Requirements
10 //------------------------------------------------------------------------------
11
12 const astUtils = require("./utils/ast-utils");
13
14 //------------------------------------------------------------------------------
15 // Rule Definition
16 //------------------------------------------------------------------------------
17
18 module.exports = {
19 meta: {
20 type: "layout",
21
22 docs: {
23 description: "require or disallow padding within blocks",
24 category: "Stylistic Issues",
25 recommended: false,
26 url: "https://eslint.org/docs/rules/padded-blocks"
27 },
28
29 fixable: "whitespace",
30
31 schema: [
32 {
33 oneOf: [
34 {
35 enum: ["always", "never"]
36 },
37 {
38 type: "object",
39 properties: {
40 blocks: {
41 enum: ["always", "never"]
42 },
43 switches: {
44 enum: ["always", "never"]
45 },
46 classes: {
47 enum: ["always", "never"]
48 }
49 },
50 additionalProperties: false,
51 minProperties: 1
52 }
53 ]
54 },
55 {
56 type: "object",
57 properties: {
58 allowSingleLineBlocks: {
59 type: "boolean"
60 }
61 }
62 }
63 ],
64
65 messages: {
66 alwaysPadBlock: "Block must be padded by blank lines.",
67 neverPadBlock: "Block must not be padded by blank lines."
68 }
69 },
70
71 create(context) {
72 const options = {};
73 const typeOptions = context.options[0] || "always";
74 const exceptOptions = context.options[1] || {};
75
76 if (typeof typeOptions === "string") {
77 const shouldHavePadding = typeOptions === "always";
78
79 options.blocks = shouldHavePadding;
80 options.switches = shouldHavePadding;
81 options.classes = shouldHavePadding;
82 } else {
83 if (Object.prototype.hasOwnProperty.call(typeOptions, "blocks")) {
84 options.blocks = typeOptions.blocks === "always";
85 }
86 if (Object.prototype.hasOwnProperty.call(typeOptions, "switches")) {
87 options.switches = typeOptions.switches === "always";
88 }
89 if (Object.prototype.hasOwnProperty.call(typeOptions, "classes")) {
90 options.classes = typeOptions.classes === "always";
91 }
92 }
93
94 if (Object.prototype.hasOwnProperty.call(exceptOptions, "allowSingleLineBlocks")) {
95 options.allowSingleLineBlocks = exceptOptions.allowSingleLineBlocks === true;
96 }
97
98 const sourceCode = context.getSourceCode();
99
100 /**
101 * Gets the open brace token from a given node.
102 * @param {ASTNode} node A BlockStatement or SwitchStatement node from which to get the open brace.
103 * @returns {Token} The token of the open brace.
104 */
105 function getOpenBrace(node) {
106 if (node.type === "SwitchStatement") {
107 return sourceCode.getTokenBefore(node.cases[0]);
108 }
109 return sourceCode.getFirstToken(node);
110 }
111
112 /**
113 * Checks if the given parameter is a comment node
114 * @param {ASTNode|Token} node An AST node or token
115 * @returns {boolean} True if node is a comment
116 */
117 function isComment(node) {
118 return node.type === "Line" || node.type === "Block";
119 }
120
121 /**
122 * Checks if there is padding between two tokens
123 * @param {Token} first The first token
124 * @param {Token} second The second token
125 * @returns {boolean} True if there is at least a line between the tokens
126 */
127 function isPaddingBetweenTokens(first, second) {
128 return second.loc.start.line - first.loc.end.line >= 2;
129 }
130
131
132 /**
133 * Checks if the given token has a blank line after it.
134 * @param {Token} token The token to check.
135 * @returns {boolean} Whether or not the token is followed by a blank line.
136 */
137 function getFirstBlockToken(token) {
138 let prev,
139 first = token;
140
141 do {
142 prev = first;
143 first = sourceCode.getTokenAfter(first, { includeComments: true });
144 } while (isComment(first) && first.loc.start.line === prev.loc.end.line);
145
146 return first;
147 }
148
149 /**
150 * Checks if the given token is preceded by a blank line.
151 * @param {Token} token The token to check
152 * @returns {boolean} Whether or not the token is preceded by a blank line
153 */
154 function getLastBlockToken(token) {
155 let last = token,
156 next;
157
158 do {
159 next = last;
160 last = sourceCode.getTokenBefore(last, { includeComments: true });
161 } while (isComment(last) && last.loc.end.line === next.loc.start.line);
162
163 return last;
164 }
165
166 /**
167 * Checks if a node should be padded, according to the rule config.
168 * @param {ASTNode} node The AST node to check.
169 * @returns {boolean} True if the node should be padded, false otherwise.
170 */
171 function requirePaddingFor(node) {
172 switch (node.type) {
173 case "BlockStatement":
174 return options.blocks;
175 case "SwitchStatement":
176 return options.switches;
177 case "ClassBody":
178 return options.classes;
179
180 /* istanbul ignore next */
181 default:
182 throw new Error("unreachable");
183 }
184 }
185
186 /**
187 * Checks the given BlockStatement node to be padded if the block is not empty.
188 * @param {ASTNode} node The AST node of a BlockStatement.
189 * @returns {void} undefined.
190 */
191 function checkPadding(node) {
192 const openBrace = getOpenBrace(node),
193 firstBlockToken = getFirstBlockToken(openBrace),
194 tokenBeforeFirst = sourceCode.getTokenBefore(firstBlockToken, { includeComments: true }),
195 closeBrace = sourceCode.getLastToken(node),
196 lastBlockToken = getLastBlockToken(closeBrace),
197 tokenAfterLast = sourceCode.getTokenAfter(lastBlockToken, { includeComments: true }),
198 blockHasTopPadding = isPaddingBetweenTokens(tokenBeforeFirst, firstBlockToken),
199 blockHasBottomPadding = isPaddingBetweenTokens(lastBlockToken, tokenAfterLast);
200
201 if (options.allowSingleLineBlocks && astUtils.isTokenOnSameLine(tokenBeforeFirst, tokenAfterLast)) {
202 return;
203 }
204
205 if (requirePaddingFor(node)) {
206 if (!blockHasTopPadding) {
207 context.report({
208 node,
209 loc: { line: tokenBeforeFirst.loc.start.line, column: tokenBeforeFirst.loc.start.column },
210 fix(fixer) {
211 return fixer.insertTextAfter(tokenBeforeFirst, "\n");
212 },
213 messageId: "alwaysPadBlock"
214 });
215 }
216 if (!blockHasBottomPadding) {
217 context.report({
218 node,
219 loc: { line: tokenAfterLast.loc.end.line, column: tokenAfterLast.loc.end.column - 1 },
220 fix(fixer) {
221 return fixer.insertTextBefore(tokenAfterLast, "\n");
222 },
223 messageId: "alwaysPadBlock"
224 });
225 }
226 } else {
227 if (blockHasTopPadding) {
228
229 context.report({
230 node,
231 loc: { line: tokenBeforeFirst.loc.start.line, column: tokenBeforeFirst.loc.start.column },
232 fix(fixer) {
233 return fixer.replaceTextRange([tokenBeforeFirst.range[1], firstBlockToken.range[0] - firstBlockToken.loc.start.column], "\n");
234 },
235 messageId: "neverPadBlock"
236 });
237 }
238
239 if (blockHasBottomPadding) {
240
241 context.report({
242 node,
243 loc: { line: tokenAfterLast.loc.end.line, column: tokenAfterLast.loc.end.column - 1 },
244 messageId: "neverPadBlock",
245 fix(fixer) {
246 return fixer.replaceTextRange([lastBlockToken.range[1], tokenAfterLast.range[0] - tokenAfterLast.loc.start.column], "\n");
247 }
248 });
249 }
250 }
251 }
252
253 const rule = {};
254
255 if (Object.prototype.hasOwnProperty.call(options, "switches")) {
256 rule.SwitchStatement = function(node) {
257 if (node.cases.length === 0) {
258 return;
259 }
260 checkPadding(node);
261 };
262 }
263
264 if (Object.prototype.hasOwnProperty.call(options, "blocks")) {
265 rule.BlockStatement = function(node) {
266 if (node.body.length === 0) {
267 return;
268 }
269 checkPadding(node);
270 };
271 }
272
273 if (Object.prototype.hasOwnProperty.call(options, "classes")) {
274 rule.ClassBody = function(node) {
275 if (node.body.length === 0) {
276 return;
277 }
278 checkPadding(node);
279 };
280 }
281
282 return rule;
283 }
284 };