605d7b1210ef9d9c4a3d3119037f50e952fde1fe
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to flag unnecessary double negation in Boolean contexts
3  * @author Brandon Mills
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     return {
15         "UnaryExpression": function (node) {
16             var ancestors = context.getAncestors(),
17                 parent = ancestors.pop(),
18                 grandparent = ancestors.pop();
19
20             // Exit early if it's guaranteed not to match
21             if (node.operator !== "!" ||
22                     parent.type !== "UnaryExpression" ||
23                     parent.operator !== "!") {
24                 return;
25             }
26
27             // if (<bool>) ...
28             if (grandparent.type === "IfStatement") {
29                 context.report(node, "Redundant double negation in an if statement condition.");
30
31             // do ... while (<bool>)
32             } else if (grandparent.type === "DoWhileStatement") {
33                 context.report(node, "Redundant double negation in a do while loop condition.");
34
35             // while (<bool>) ...
36             } else if (grandparent.type === "WhileStatement") {
37                 context.report(node, "Redundant double negation in a while loop condition.");
38
39             // <bool> ? ... : ...
40             } else if ((grandparent.type === "ConditionalExpression" &&
41                     parent === grandparent.test)) {
42                 context.report(node, "Redundant double negation in a ternary condition.");
43
44             // for (...; <bool>; ...) ...
45             } else if ((grandparent.type === "ForStatement" &&
46                     parent === grandparent.test)) {
47                 context.report(node, "Redundant double negation in a for loop condition.");
48
49             // !<bool>
50             } else if ((grandparent.type === "UnaryExpression" &&
51                     grandparent.operator === "!")) {
52                 context.report(node, "Redundant multiple negation.");
53
54             // Boolean(<bool>)
55             } else if ((grandparent.type === "CallExpression" &&
56                     grandparent.callee.type === "Identifier" &&
57                     grandparent.callee.name === "Boolean")) {
58                 context.report(node, "Redundant double negation in call to Boolean().");
59
60             // new Boolean(<bool>)
61             } else if ((grandparent.type === "NewExpression" &&
62                     grandparent.callee.type === "Identifier" &&
63                     grandparent.callee.name === "Boolean")) {
64                 context.report(node, "Redundant double negation in Boolean constructor call.");
65             }
66         }
67     };
68
69 };