no-bitwise.js 3.5 KB

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