项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.

for-direction.js 3.7 KiB

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