no-plusplus.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @fileoverview Rule to flag use of unary increment and decrement operators.
  3. * @author Ian Christian Myers
  4. * @author Brody McKee (github.com/mrmckeb)
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Helpers
  9. //------------------------------------------------------------------------------
  10. /**
  11. * Determines whether the given node is the update node of a `ForStatement`.
  12. * @param {ASTNode} node The node to check.
  13. * @returns {boolean} `true` if the node is `ForStatement` update.
  14. */
  15. function isForStatementUpdate(node) {
  16. const parent = node.parent;
  17. return parent.type === "ForStatement" && parent.update === node;
  18. }
  19. /**
  20. * Determines whether the given node is considered to be a for loop "afterthought" by the logic of this rule.
  21. * In particular, it returns `true` if the given node is either:
  22. * - The update node of a `ForStatement`: for (;; i++) {}
  23. * - An operand of a sequence expression that is the update node: for (;; foo(), i++) {}
  24. * - An operand of a sequence expression that is child of another sequence expression, etc.,
  25. * up to the sequence expression that is the update node: for (;; foo(), (bar(), (baz(), i++))) {}
  26. * @param {ASTNode} node The node to check.
  27. * @returns {boolean} `true` if the node is a for loop afterthought.
  28. */
  29. function isForLoopAfterthought(node) {
  30. const parent = node.parent;
  31. if (parent.type === "SequenceExpression") {
  32. return isForLoopAfterthought(parent);
  33. }
  34. return isForStatementUpdate(node);
  35. }
  36. //------------------------------------------------------------------------------
  37. // Rule Definition
  38. //------------------------------------------------------------------------------
  39. /** @type {import('../types').Rule.RuleModule} */
  40. module.exports = {
  41. meta: {
  42. type: "suggestion",
  43. defaultOptions: [
  44. {
  45. allowForLoopAfterthoughts: false,
  46. },
  47. ],
  48. docs: {
  49. description: "Disallow the unary operators `++` and `--`",
  50. recommended: false,
  51. frozen: true,
  52. url: "https://eslint.org/docs/latest/rules/no-plusplus",
  53. },
  54. schema: [
  55. {
  56. type: "object",
  57. properties: {
  58. allowForLoopAfterthoughts: {
  59. type: "boolean",
  60. },
  61. },
  62. additionalProperties: false,
  63. },
  64. ],
  65. messages: {
  66. unexpectedUnaryOp: "Unary operator '{{operator}}' used.",
  67. },
  68. },
  69. create(context) {
  70. const [{ allowForLoopAfterthoughts }] = context.options;
  71. return {
  72. UpdateExpression(node) {
  73. if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) {
  74. return;
  75. }
  76. context.report({
  77. node,
  78. messageId: "unexpectedUnaryOp",
  79. data: {
  80. operator: node.operator,
  81. },
  82. });
  83. },
  84. };
  85. },
  86. };