2 * @fileoverview A rule to ensure blank lines within blocks.
3 * @author Mathias Schreck <https://github.com/lo1tuma>
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "require or disallow padding within blocks",
24 category: "Stylistic Issues",
26 url: "https://eslint.org/docs/rules/padded-blocks"
29 fixable: "whitespace",
35 enum: ["always", "never"]
41 enum: ["always", "never"]
44 enum: ["always", "never"]
47 enum: ["always", "never"]
50 additionalProperties: false,
58 allowSingleLineBlocks: {
62 additionalProperties: false
67 alwaysPadBlock: "Block must be padded by blank lines.",
68 neverPadBlock: "Block must not be padded by blank lines."
74 const typeOptions = context.options[0] || "always";
75 const exceptOptions = context.options[1] || {};
77 if (typeof typeOptions === "string") {
78 const shouldHavePadding = typeOptions === "always";
80 options.blocks = shouldHavePadding;
81 options.switches = shouldHavePadding;
82 options.classes = shouldHavePadding;
84 if (Object.prototype.hasOwnProperty.call(typeOptions, "blocks")) {
85 options.blocks = typeOptions.blocks === "always";
87 if (Object.prototype.hasOwnProperty.call(typeOptions, "switches")) {
88 options.switches = typeOptions.switches === "always";
90 if (Object.prototype.hasOwnProperty.call(typeOptions, "classes")) {
91 options.classes = typeOptions.classes === "always";
95 if (Object.prototype.hasOwnProperty.call(exceptOptions, "allowSingleLineBlocks")) {
96 options.allowSingleLineBlocks = exceptOptions.allowSingleLineBlocks === true;
99 const sourceCode = context.getSourceCode();
102 * Gets the open brace token from a given node.
103 * @param {ASTNode} node A BlockStatement or SwitchStatement node from which to get the open brace.
104 * @returns {Token} The token of the open brace.
106 function getOpenBrace(node) {
107 if (node.type === "SwitchStatement") {
108 return sourceCode.getTokenBefore(node.cases[0]);
110 return sourceCode.getFirstToken(node);
114 * Checks if the given parameter is a comment node
115 * @param {ASTNode|Token} node An AST node or token
116 * @returns {boolean} True if node is a comment
118 function isComment(node) {
119 return node.type === "Line" || node.type === "Block";
123 * Checks if there is padding between two tokens
124 * @param {Token} first The first token
125 * @param {Token} second The second token
126 * @returns {boolean} True if there is at least a line between the tokens
128 function isPaddingBetweenTokens(first, second) {
129 return second.loc.start.line - first.loc.end.line >= 2;
134 * Checks if the given token has a blank line after it.
135 * @param {Token} token The token to check.
136 * @returns {boolean} Whether or not the token is followed by a blank line.
138 function getFirstBlockToken(token) {
144 first = sourceCode.getTokenAfter(first, { includeComments: true });
145 } while (isComment(first) && first.loc.start.line === prev.loc.end.line);
151 * Checks if the given token is preceded by a blank line.
152 * @param {Token} token The token to check
153 * @returns {boolean} Whether or not the token is preceded by a blank line
155 function getLastBlockToken(token) {
161 last = sourceCode.getTokenBefore(last, { includeComments: true });
162 } while (isComment(last) && last.loc.end.line === next.loc.start.line);
168 * Checks if a node should be padded, according to the rule config.
169 * @param {ASTNode} node The AST node to check.
170 * @returns {boolean} True if the node should be padded, false otherwise.
172 function requirePaddingFor(node) {
174 case "BlockStatement":
175 return options.blocks;
176 case "SwitchStatement":
177 return options.switches;
179 return options.classes;
181 /* istanbul ignore next */
183 throw new Error("unreachable");
188 * Checks the given BlockStatement node to be padded if the block is not empty.
189 * @param {ASTNode} node The AST node of a BlockStatement.
190 * @returns {void} undefined.
192 function checkPadding(node) {
193 const openBrace = getOpenBrace(node),
194 firstBlockToken = getFirstBlockToken(openBrace),
195 tokenBeforeFirst = sourceCode.getTokenBefore(firstBlockToken, { includeComments: true }),
196 closeBrace = sourceCode.getLastToken(node),
197 lastBlockToken = getLastBlockToken(closeBrace),
198 tokenAfterLast = sourceCode.getTokenAfter(lastBlockToken, { includeComments: true }),
199 blockHasTopPadding = isPaddingBetweenTokens(tokenBeforeFirst, firstBlockToken),
200 blockHasBottomPadding = isPaddingBetweenTokens(lastBlockToken, tokenAfterLast);
202 if (options.allowSingleLineBlocks && astUtils.isTokenOnSameLine(tokenBeforeFirst, tokenAfterLast)) {
206 if (requirePaddingFor(node)) {
208 if (!blockHasTopPadding) {
212 start: tokenBeforeFirst.loc.start,
213 end: firstBlockToken.loc.start
216 return fixer.insertTextAfter(tokenBeforeFirst, "\n");
218 messageId: "alwaysPadBlock"
221 if (!blockHasBottomPadding) {
225 end: tokenAfterLast.loc.start,
226 start: lastBlockToken.loc.end
229 return fixer.insertTextBefore(tokenAfterLast, "\n");
231 messageId: "alwaysPadBlock"
235 if (blockHasTopPadding) {
240 start: tokenBeforeFirst.loc.start,
241 end: firstBlockToken.loc.start
244 return fixer.replaceTextRange([tokenBeforeFirst.range[1], firstBlockToken.range[0] - firstBlockToken.loc.start.column], "\n");
246 messageId: "neverPadBlock"
250 if (blockHasBottomPadding) {
255 end: tokenAfterLast.loc.start,
256 start: lastBlockToken.loc.end
258 messageId: "neverPadBlock",
260 return fixer.replaceTextRange([lastBlockToken.range[1], tokenAfterLast.range[0] - tokenAfterLast.loc.start.column], "\n");
269 if (Object.prototype.hasOwnProperty.call(options, "switches")) {
270 rule.SwitchStatement = function(node) {
271 if (node.cases.length === 0) {
278 if (Object.prototype.hasOwnProperty.call(options, "blocks")) {
279 rule.BlockStatement = function(node) {
280 if (node.body.length === 0) {
287 if (Object.prototype.hasOwnProperty.call(options, "classes")) {
288 rule.ClassBody = function(node) {
289 if (node.body.length === 0) {