for-direction.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 the right side of the assignment
  38. * @param {ASTNode} update UpdateExpression to check
  39. * @param {int} dir expected direction that could either be turned around or invalidated
  40. * @returns {int} return dir, the negated dir or zero if it's not clear for identifiers
  41. */
  42. function getRightDirection(update, dir) {
  43. if (update.right.type === "UnaryExpression") {
  44. if (update.right.operator === "-") {
  45. return -dir;
  46. }
  47. } else if (update.right.type === "Identifier") {
  48. return 0;
  49. }
  50. return dir;
  51. }
  52. /**
  53. * check UpdateExpression add/sub the counter
  54. * @param {ASTNode} update UpdateExpression to check
  55. * @param {string} counter variable name to check
  56. * @returns {int} if add return 1, if sub return -1, if nochange, return 0
  57. */
  58. function getUpdateDirection(update, counter) {
  59. if (update.argument.type === "Identifier" && update.argument.name === counter) {
  60. if (update.operator === "++") {
  61. return 1;
  62. }
  63. if (update.operator === "--") {
  64. return -1;
  65. }
  66. }
  67. return 0;
  68. }
  69. /**
  70. * check AssignmentExpression add/sub the counter
  71. * @param {ASTNode} update AssignmentExpression to check
  72. * @param {string} counter variable name to check
  73. * @returns {int} if add return 1, if sub return -1, if nochange, return 0
  74. */
  75. function getAssignmentDirection(update, counter) {
  76. if (update.left.name === counter) {
  77. if (update.operator === "+=") {
  78. return getRightDirection(update, 1);
  79. }
  80. if (update.operator === "-=") {
  81. return getRightDirection(update, -1);
  82. }
  83. }
  84. return 0;
  85. }
  86. return {
  87. ForStatement(node) {
  88. if (node.test && node.test.type === "BinaryExpression" && node.test.left.type === "Identifier" && node.update) {
  89. const counter = node.test.left.name;
  90. const operator = node.test.operator;
  91. const update = node.update;
  92. let wrongDirection;
  93. if (operator === "<" || operator === "<=") {
  94. wrongDirection = -1;
  95. } else if (operator === ">" || operator === ">=") {
  96. wrongDirection = 1;
  97. } else {
  98. return;
  99. }
  100. if (update.type === "UpdateExpression") {
  101. if (getUpdateDirection(update, counter) === wrongDirection) {
  102. report(node);
  103. }
  104. } else if (update.type === "AssignmentExpression" && getAssignmentDirection(update, counter) === wrongDirection) {
  105. report(node);
  106. }
  107. }
  108. }
  109. };
  110. }
  111. };