for-direction.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. // Requirements
  8. //------------------------------------------------------------------------------
  9. const { getStaticValue } = require("@eslint-community/eslint-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. /** @type {import('../types').Rule.RuleModule} */
  14. module.exports = {
  15. meta: {
  16. type: "problem",
  17. docs: {
  18. description:
  19. "Enforce `for` loop update clause moving the counter in the right direction",
  20. recommended: true,
  21. url: "https://eslint.org/docs/latest/rules/for-direction",
  22. },
  23. fixable: null,
  24. schema: [],
  25. messages: {
  26. incorrectDirection:
  27. "The update clause in this loop moves the variable in the wrong direction.",
  28. },
  29. },
  30. create(context) {
  31. const { sourceCode } = context;
  32. /**
  33. * report an error.
  34. * @param {ASTNode} node the node to report.
  35. * @returns {void}
  36. */
  37. function report(node) {
  38. context.report({
  39. loc: {
  40. start: node.loc.start,
  41. end: sourceCode.getTokenBefore(node.body).loc.end,
  42. },
  43. messageId: "incorrectDirection",
  44. });
  45. }
  46. /**
  47. * check the right side of the assignment
  48. * @param {ASTNode} update UpdateExpression to check
  49. * @param {number} dir expected direction that could either be turned around or invalidated
  50. * @returns {number} return dir, the negated dir, or zero if the counter does not change or the direction is not clear
  51. */
  52. function getRightDirection(update, dir) {
  53. const staticValue = getStaticValue(
  54. update.right,
  55. sourceCode.getScope(update),
  56. );
  57. if (
  58. staticValue &&
  59. ["bigint", "boolean", "number"].includes(
  60. typeof staticValue.value,
  61. )
  62. ) {
  63. const sign = Math.sign(Number(staticValue.value)) || 0; // convert NaN to 0
  64. return dir * sign;
  65. }
  66. return 0;
  67. }
  68. /**
  69. * check UpdateExpression add/sub the counter
  70. * @param {ASTNode} update UpdateExpression to check
  71. * @param {string} counter variable name to check
  72. * @returns {number} if add return 1, if sub return -1, if nochange, return 0
  73. */
  74. function getUpdateDirection(update, counter) {
  75. if (
  76. update.argument.type === "Identifier" &&
  77. update.argument.name === counter
  78. ) {
  79. if (update.operator === "++") {
  80. return 1;
  81. }
  82. if (update.operator === "--") {
  83. return -1;
  84. }
  85. }
  86. return 0;
  87. }
  88. /**
  89. * check AssignmentExpression add/sub the counter
  90. * @param {ASTNode} update AssignmentExpression to check
  91. * @param {string} counter variable name to check
  92. * @returns {number} if add return 1, if sub return -1, if nochange, return 0
  93. */
  94. function getAssignmentDirection(update, counter) {
  95. if (update.left.name === counter) {
  96. if (update.operator === "+=") {
  97. return getRightDirection(update, 1);
  98. }
  99. if (update.operator === "-=") {
  100. return getRightDirection(update, -1);
  101. }
  102. }
  103. return 0;
  104. }
  105. return {
  106. ForStatement(node) {
  107. if (
  108. node.test &&
  109. node.test.type === "BinaryExpression" &&
  110. node.update
  111. ) {
  112. for (const counterPosition of ["left", "right"]) {
  113. if (node.test[counterPosition].type !== "Identifier") {
  114. continue;
  115. }
  116. const counter = node.test[counterPosition].name;
  117. const operator = node.test.operator;
  118. const update = node.update;
  119. let wrongDirection;
  120. if (operator === "<" || operator === "<=") {
  121. wrongDirection =
  122. counterPosition === "left" ? -1 : 1;
  123. } else if (operator === ">" || operator === ">=") {
  124. wrongDirection =
  125. counterPosition === "left" ? 1 : -1;
  126. } else {
  127. return;
  128. }
  129. if (update.type === "UpdateExpression") {
  130. if (
  131. getUpdateDirection(update, counter) ===
  132. wrongDirection
  133. ) {
  134. report(node);
  135. }
  136. } else if (
  137. update.type === "AssignmentExpression" &&
  138. getAssignmentDirection(update, counter) ===
  139. wrongDirection
  140. ) {
  141. report(node);
  142. }
  143. }
  144. }
  145. },
  146. };
  147. },
  148. };