UNPKG

1.19 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to disallow if as the only statmenet in an else block
3 * @author Brandon Mills
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = {
12 meta: {
13 docs: {
14 description: "disallow `if` statements as the only statement in `else` blocks",
15 category: "Stylistic Issues",
16 recommended: false
17 },
18
19 schema: []
20 },
21
22 create(context) {
23
24 return {
25 IfStatement(node) {
26 const ancestors = context.getAncestors(),
27 parent = ancestors.pop(),
28 grandparent = ancestors.pop();
29
30 if (parent && parent.type === "BlockStatement" &&
31 parent.body.length === 1 && grandparent &&
32 grandparent.type === "IfStatement" &&
33 parent === grandparent.alternate) {
34 context.report(node, "Unexpected if as the only statement in an else block.");
35 }
36 }
37 };
38
39 }
40};