1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | module.exports = function(context) {
|
11 |
|
12 | "use strict";
|
13 |
|
14 | return {
|
15 | "UnaryExpression": function (node) {
|
16 | var ancestors = context.getAncestors(),
|
17 | parent = ancestors.pop(),
|
18 | grandparent = ancestors.pop(),
|
19 | operator;
|
20 |
|
21 |
|
22 | if (node.operator !== "!" ||
|
23 | parent.type !== "UnaryExpression" ||
|
24 | parent.operator !== "!") {
|
25 | return;
|
26 | }
|
27 |
|
28 |
|
29 | if (grandparent.type === "IfStatement") {
|
30 | context.report(node, "Redundant double negation in an if statement condition.");
|
31 |
|
32 |
|
33 | } else if (grandparent.type === "DoWhileStatement") {
|
34 | context.report(node, "Redundant double negation in a do while loop condition.");
|
35 |
|
36 |
|
37 | } else if (grandparent.type === "WhileStatement") {
|
38 | context.report(node, "Redundant double negation in a while loop condition.");
|
39 |
|
40 |
|
41 | } else if ((grandparent.type === "ConditionalExpression" &&
|
42 | parent === grandparent.test)) {
|
43 | context.report(node, "Redundant double negation in a ternary condition.");
|
44 |
|
45 |
|
46 | } else if ((grandparent.type === "ForStatement" &&
|
47 | parent === grandparent.test)) {
|
48 | context.report(node, "Redundant double negation in a for loop condition.");
|
49 |
|
50 |
|
51 | } else if ((grandparent.type === "UnaryExpression" &&
|
52 | grandparent.operator === "!")) {
|
53 | context.report(node, "Redundant multiple negation.");
|
54 |
|
55 |
|
56 | } else if ((grandparent.type === "CallExpression" &&
|
57 | grandparent.callee.type === "Identifier" &&
|
58 | grandparent.callee.name === "Boolean")) {
|
59 | context.report(node, "Redundant double negation in call to Boolean().");
|
60 |
|
61 |
|
62 | } else if ((grandparent.type === "NewExpression" &&
|
63 | grandparent.callee.type === "Identifier" &&
|
64 | grandparent.callee.name === "Boolean")) {
|
65 | context.report(node, "Redundant double negation in Boolean constructor call.");
|
66 | }
|
67 | }
|
68 | };
|
69 |
|
70 | };
|