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