1 |
|
2 |
|
3 |
|
4 |
|
5 | "use strict";
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | module.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 | };
|