1 | /**
|
2 | * @fileoverview Rule to flag bitwise identifiers
|
3 | * @author Nicholas C. Zakas
|
4 | */
|
5 |
|
6 | ;
|
7 |
|
8 | //
|
9 | // Set of bitwise operators.
|
10 | //
|
11 | const BITWISE_OPERATORS = [
|
12 | "^", "|", "&", "<<", ">>", ">>>",
|
13 | "^=", "|=", "&=", "<<=", ">>=", ">>>=",
|
14 | "~"
|
15 | ];
|
16 |
|
17 | //------------------------------------------------------------------------------
|
18 | // Rule Definition
|
19 | //------------------------------------------------------------------------------
|
20 |
|
21 | module.exports = {
|
22 | meta: {
|
23 | docs: {
|
24 | description: "disallow bitwise operators",
|
25 | category: "Stylistic Issues",
|
26 | recommended: false
|
27 | },
|
28 |
|
29 | schema: [
|
30 | {
|
31 | type: "object",
|
32 | properties: {
|
33 | allow: {
|
34 | type: "array",
|
35 | items: {
|
36 | enum: BITWISE_OPERATORS
|
37 | },
|
38 | uniqueItems: true
|
39 | },
|
40 | int32Hint: {
|
41 | type: "boolean"
|
42 | }
|
43 | },
|
44 | additionalProperties: false
|
45 | }
|
46 | ]
|
47 | },
|
48 |
|
49 | create(context) {
|
50 | const options = context.options[0] || {};
|
51 | const allowed = options.allow || [];
|
52 | const int32Hint = options.int32Hint === true;
|
53 |
|
54 | /**
|
55 | * Reports an unexpected use of a bitwise operator.
|
56 | * @param {ASTNode} node Node which contains the bitwise operator.
|
57 | * @returns {void}
|
58 | */
|
59 | function report(node) {
|
60 | context.report(node, "Unexpected use of '{{operator}}'.", { operator: node.operator });
|
61 | }
|
62 |
|
63 | /**
|
64 | * Checks if the given node has a bitwise operator.
|
65 | * @param {ASTNode} node The node to check.
|
66 | * @returns {boolean} Whether or not the node has a bitwise operator.
|
67 | */
|
68 | function hasBitwiseOperator(node) {
|
69 | return BITWISE_OPERATORS.indexOf(node.operator) !== -1;
|
70 | }
|
71 |
|
72 | /**
|
73 | * Checks if exceptions were provided, e.g. `{ allow: ['~', '|'] }`.
|
74 | * @param {ASTNode} node The node to check.
|
75 | * @returns {boolean} Whether or not the node has a bitwise operator.
|
76 | */
|
77 | function allowedOperator(node) {
|
78 | return allowed.indexOf(node.operator) !== -1;
|
79 | }
|
80 |
|
81 | /**
|
82 | * Checks if the given bitwise operator is used for integer typecasting, i.e. "|0"
|
83 | * @param {ASTNode} node The node to check.
|
84 | * @returns {boolean} whether the node is used in integer typecasting.
|
85 | */
|
86 | function isInt32Hint(node) {
|
87 | return int32Hint && node.operator === "|" && node.right &&
|
88 | node.right.type === "Literal" && node.right.value === 0;
|
89 | }
|
90 |
|
91 | /**
|
92 | * Report if the given node contains a bitwise operator.
|
93 | * @param {ASTNode} node The node to check.
|
94 | * @returns {void}
|
95 | */
|
96 | function checkNodeForBitwiseOperator(node) {
|
97 | if (hasBitwiseOperator(node) && !allowedOperator(node) && !isInt32Hint(node)) {
|
98 | report(node);
|
99 | }
|
100 | }
|
101 |
|
102 | return {
|
103 | AssignmentExpression: checkNodeForBitwiseOperator,
|
104 | BinaryExpression: checkNodeForBitwiseOperator,
|
105 | UnaryExpression: checkNodeForBitwiseOperator
|
106 | };
|
107 |
|
108 | }
|
109 | };
|