summaryrefslogtreecommitdiff
path: root/tools/eslint/lib/rules/no-lonely-if.js
diff options
context:
space:
mode:
Diffstat (limited to 'tools/eslint/lib/rules/no-lonely-if.js')
-rw-r--r--tools/eslint/lib/rules/no-lonely-if.js28
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 0000000000..59d807da30
--- /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.");
+ }
+ }
+ };
+
+};