942317f3d0eebdd5145c318defd63d66ff1cc51f
[platform/framework/web/crosswalk-tizen.git] /
1 /**
2  * @fileoverview Rule to flag bitwise identifiers
3  * @author Nicholas C. Zakas
4  */
5
6 "use strict";
7
8 //------------------------------------------------------------------------------
9 // Rule Definition
10 //------------------------------------------------------------------------------
11
12 module.exports = function(context) {
13
14     var BITWISE_OPERATORS = [
15         "^", "|", "&", "<<", ">>", ">>>",
16         "^=", "|=", "&=", "<<=", ">>=", ">>>=",
17         "~"
18     ];
19
20     /**
21      * Reports an unexpected use of a bitwise operator.
22      * @param   {ASTNode} node Node which contains the bitwise operator.
23      * @returns {void}
24      */
25     function report(node) {
26         context.report(node, "Unexpected use of '{{operator}}'.", { operator: node.operator });
27     }
28
29     /**
30      * Checks if the given node has a bitwise operator.
31      * @param   {ASTNode} node The node to check.
32      * @returns {boolean} Whether or not the node has a bitwise operator.
33      */
34     function hasBitwiseOperator(node) {
35         return BITWISE_OPERATORS.indexOf(node.operator) !== -1;
36     }
37
38     /**
39      * Report if the given node contains a bitwise operator.
40      * @param {ASTNode} node The node to check.
41      * @returns {void}
42      */
43     function checkNodeForBitwiseOperator(node) {
44         if (hasBitwiseOperator(node)) {
45             report(node);
46         }
47     }
48
49     return {
50         "AssignmentExpression": checkNodeForBitwiseOperator,
51         "BinaryExpression": checkNodeForBitwiseOperator,
52         "UnaryExpression": checkNodeForBitwiseOperator
53     };
54
55 };