Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-bitwise.js 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  42. },
  43. additionalProperties: false
  44. }
  45. ],
  46. messages: {
  47. unexpected: "Unexpected use of '{{operator}}'."
  48. }
  49. },
  50. create(context) {
  51. const options = context.options[0] || {};
  52. const allowed = options.allow || [];
  53. const int32Hint = options.int32Hint === true;
  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, messageId: "unexpected", data: { operator: node.operator } });
  61. }
  62. /**
  63. * Checks if the given node has a bitwise operator.
  64. * @param {ASTNode} node The node to check.
  65. * @returns {boolean} Whether or not the node has a bitwise operator.
  66. */
  67. function hasBitwiseOperator(node) {
  68. return BITWISE_OPERATORS.indexOf(node.operator) !== -1;
  69. }
  70. /**
  71. * Checks if exceptions were provided, e.g. `{ allow: ['~', '|'] }`.
  72. * @param {ASTNode} node The node to check.
  73. * @returns {boolean} Whether or not the node has a bitwise operator.
  74. */
  75. function allowedOperator(node) {
  76. return allowed.indexOf(node.operator) !== -1;
  77. }
  78. /**
  79. * Checks if the given bitwise operator is used for integer typecasting, i.e. "|0"
  80. * @param {ASTNode} node The node to check.
  81. * @returns {boolean} whether the node is used in integer typecasting.
  82. */
  83. function isInt32Hint(node) {
  84. return int32Hint && node.operator === "|" && node.right &&
  85. node.right.type === "Literal" && node.right.value === 0;
  86. }
  87. /**
  88. * Report if the given node contains a bitwise operator.
  89. * @param {ASTNode} node The node to check.
  90. * @returns {void}
  91. */
  92. function checkNodeForBitwiseOperator(node) {
  93. if (hasBitwiseOperator(node) && !allowedOperator(node) && !isInt32Hint(node)) {
  94. report(node);
  95. }
  96. }
  97. return {
  98. AssignmentExpression: checkNodeForBitwiseOperator,
  99. BinaryExpression: checkNodeForBitwiseOperator,
  100. UnaryExpression: checkNodeForBitwiseOperator
  101. };
  102. }
  103. };