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.

for-direction.js 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /**
  2. * @fileoverview enforce "for" loop update clause moving the counter in the right direction.(for-direction)
  3. * @author Aladdin-ADD<hh_2013@foxmail.com>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "problem",
  12. docs: {
  13. description: "enforce \"for\" loop update clause moving the counter in the right direction.",
  14. category: "Possible Errors",
  15. recommended: true,
  16. url: "https://eslint.org/docs/rules/for-direction"
  17. },
  18. fixable: null,
  19. schema: [],
  20. messages: {
  21. incorrectDirection: "The update clause in this loop moves the variable in the wrong direction."
  22. }
  23. },
  24. create(context) {
  25. /**
  26. * report an error.
  27. * @param {ASTNode} node the node to report.
  28. * @returns {void}
  29. */
  30. function report(node) {
  31. context.report({
  32. node,
  33. messageId: "incorrectDirection"
  34. });
  35. }
  36. /**
  37. * check UpdateExpression add/sub the counter
  38. * @param {ASTNode} update UpdateExpression to check
  39. * @param {string} counter variable name to check
  40. * @returns {int} if add return 1, if sub return -1, if nochange, return 0
  41. */
  42. function getUpdateDirection(update, counter) {
  43. if (update.argument.type === "Identifier" && update.argument.name === counter) {
  44. if (update.operator === "++") {
  45. return 1;
  46. }
  47. if (update.operator === "--") {
  48. return -1;
  49. }
  50. }
  51. return 0;
  52. }
  53. /**
  54. * check AssignmentExpression add/sub the counter
  55. * @param {ASTNode} update AssignmentExpression to check
  56. * @param {string} counter variable name to check
  57. * @returns {int} if add return 1, if sub return -1, if nochange, return 0
  58. */
  59. function getAssignmentDirection(update, counter) {
  60. if (update.left.name === counter) {
  61. if (update.operator === "+=") {
  62. return 1;
  63. }
  64. if (update.operator === "-=") {
  65. return -1;
  66. }
  67. }
  68. return 0;
  69. }
  70. return {
  71. ForStatement(node) {
  72. if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) {
  73. const counter = node.test.left.name;
  74. const operator = node.test.operator;
  75. const update = node.update;
  76. if (operator === "<" || operator === "<=") {
  77. // report error if update sub the counter (--, -=)
  78. if (update.type === "UpdateExpression" && getUpdateDirection(update, counter) < 0) {
  79. report(node);
  80. }
  81. if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) < 0) {
  82. report(node);
  83. }
  84. } else if (operator === ">" || operator === ">=") {
  85. // report error if update add the counter (++, +=)
  86. if (update.type === "UpdateExpression" && getUpdateDirection(update, counter) > 0) {
  87. report(node);
  88. }
  89. if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) > 0) {
  90. report(node);
  91. }
  92. }
  93. }
  94. }
  95. };
  96. }
  97. };