2 * @fileoverview A rule to ensure blank lines within blocks.
3 * @author Mathias Schreck <https://github.com/lo1tuma>
4 * @copyright 2014 Mathias Schreck. All rights reserved.
9 //------------------------------------------------------------------------------
11 //------------------------------------------------------------------------------
13 module.exports = function (context) {
14 var requirePadding = context.options[0] !== "never";
17 * Checks if the given non empty block node has a blank line before its first child node.
18 * @param {ASTNode} node The AST node of a BlockStatement.
19 * @returns {boolean} Whether or not the block starts with a blank line.
21 function isNonEmptyBlockTopPadded(node) {
22 var blockStart = node.loc.start.line,
24 firstLine = first.loc.start.line,
25 expectedFirstLine = blockStart + 2,
26 leadingComments = context.getComments(first).leading;
28 if (leadingComments.length > 0) {
29 firstLine = leadingComments[0].loc.start.line;
32 return expectedFirstLine <= firstLine;
36 * Checks if the given non empty block node has a blank line after its last child node.
37 * @param {ASTNode} node The AST node of a BlockStatement.
38 * @returns {boolean} Whether or not the block ends with a blank line.
40 function isNonEmptyBlockBottomPadded(node) {
41 var blockEnd = node.loc.end.line,
42 last = node.body[node.body.length - 1],
43 lastLine = last.loc.end.line,
44 expectedLastLine = blockEnd - 2,
45 trailingComments = context.getComments(last).trailing;
47 if (trailingComments.length > 0) {
48 lastLine = trailingComments[trailingComments.length - 1].loc.end.line;
51 return lastLine <= expectedLastLine;
55 * Checks if the given non empty block node starts AND ends with a blank line.
56 * @param {ASTNode} node The AST node of a BlockStatement.
57 * @returns {boolean} Whether or not the block starts and ends with a blank line.
59 function isNonEmptyBlockPadded(node) {
60 return isNonEmptyBlockTopPadded(node) && isNonEmptyBlockBottomPadded(node);
64 * Checks if the given non empty block node starts OR ends with a blank line.
65 * @param {ASTNode} node The AST node of a BlockStatement.
66 * @returns {boolean} Whether or not the block starts and ends with a blank line.
68 function hasNonEmptyBlockExtraPadding(node) {
69 return isNonEmptyBlockTopPadded(node) || isNonEmptyBlockBottomPadded(node);
73 * Checks the given BlockStatement node to be padded if the block is not empty.
74 * @param {ASTNode} node The AST node of a BlockStatement.
75 * @returns {void} undefined.
77 function checkPadding(node) {
78 if (node.body.length > 0) {
80 if (!isNonEmptyBlockPadded(node)) {
81 context.report(node, "Block must be padded by blank lines.");
84 if (hasNonEmptyBlockExtraPadding(node)) {
85 context.report(node, "Block must not be padded by blank lines.");
92 "BlockStatement": checkPadding