no-bitwise.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. module.exports = {
  20. meta: {
  21. type: "suggestion",
  22. docs: {
  23. description: "disallow bitwise operators",
  24. category: "Stylistic Issues",
  25. recommended: false,
  26. url: "https://eslint.org/docs/rules/no-bitwise"
  27. },
  28. schema: [
  29. {
  30. type: "object",
  31. properties: {
  32. allow: {
  33. type: "array",
  34. items: {
  35. enum: BITWISE_OPERATORS
  36. },
  37. uniqueItems: true
  38. },
  39. int32Hint: {
  40. type: "boolean",
  41. default: false
  42. }
  43. },
  44. additionalProperties: false
  45. }
  46. ],
  47. messages: {
  48. unexpected: "Unexpected use of '{{operator}}'."
  49. }
  50. },
  51. create(context) {
  52. const options = context.options[0] || {};
  53. const allowed = options.allow || [];
  54. const int32Hint = options.int32Hint === true;
  55. /**
  56. * Reports an unexpected use of a bitwise operator.
  57. * @param {ASTNode} node Node which contains the bitwise operator.
  58. * @returns {void}
  59. */
  60. function report(node) {
  61. context.report({ node, messageId: "unexpected", data: { operator: node.operator } });
  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. * Checks if exceptions were provided, e.g. `{ allow: ['~', '|'] }`.
  73. * @param {ASTNode} node The node to check.
  74. * @returns {boolean} Whether or not the node has a bitwise operator.
  75. */
  76. function allowedOperator(node) {
  77. return allowed.indexOf(node.operator) !== -1;
  78. }
  79. /**
  80. * Checks if the given bitwise operator is used for integer typecasting, i.e. "|0"
  81. * @param {ASTNode} node The node to check.
  82. * @returns {boolean} whether the node is used in integer typecasting.
  83. */
  84. function isInt32Hint(node) {
  85. return int32Hint && node.operator === "|" && node.right &&
  86. node.right.type === "Literal" && node.right.value === 0;
  87. }
  88. /**
  89. * Report if the given node contains a bitwise operator.
  90. * @param {ASTNode} node The node to check.
  91. * @returns {void}
  92. */
  93. function checkNodeForBitwiseOperator(node) {
  94. if (hasBitwiseOperator(node) && !allowedOperator(node) && !isInt32Hint(node)) {
  95. report(node);
  96. }
  97. }
  98. return {
  99. AssignmentExpression: checkNodeForBitwiseOperator,
  100. BinaryExpression: checkNodeForBitwiseOperator,
  101. UnaryExpression: checkNodeForBitwiseOperator
  102. };
  103. }
  104. };