diff options
Diffstat (limited to 'tools/eslint/lib/rules/no-lonely-if.js')
-rw-r--r-- | tools/eslint/lib/rules/no-lonely-if.js | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/tools/eslint/lib/rules/no-lonely-if.js b/tools/eslint/lib/rules/no-lonely-if.js new file mode 100644 index 00000000000..59d807da30d --- /dev/null +++ b/tools/eslint/lib/rules/no-lonely-if.js @@ -0,0 +1,28 @@ +/** + * @fileoverview Rule to disallow if as the only statmenet in an else block + * @author Brandon Mills + */ +"use strict"; + +//------------------------------------------------------------------------------ +// Rule Definition +//------------------------------------------------------------------------------ + +module.exports = function(context) { + + return { + "IfStatement": function(node) { + var ancestors = context.getAncestors(), + parent = ancestors.pop(), + grandparent = ancestors.pop(); + + if (parent && parent.type === "BlockStatement" && + parent.body.length === 1 && grandparent && + grandparent.type === "IfStatement" && + parent === grandparent.alternate) { + context.report(node, "Unexpected if as the only statement in an else block."); + } + } + }; + +}; |