no-plusplus.js 3.0 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('../shared/types').Rule} */
  40. module.exports = {
  41. meta: {
  42. type: "suggestion",
  43. defaultOptions: [{
  44. allowForLoopAfterthoughts: false
  45. }],
  46. docs: {
  47. description: "Disallow the unary operators `++` and `--`",
  48. recommended: false,
  49. url: "https://eslint.org/docs/latest/rules/no-plusplus"
  50. },
  51. schema: [
  52. {
  53. type: "object",
  54. properties: {
  55. allowForLoopAfterthoughts: {
  56. type: "boolean"
  57. }
  58. },
  59. additionalProperties: false
  60. }
  61. ],
  62. messages: {
  63. unexpectedUnaryOp: "Unary operator '{{operator}}' used."
  64. }
  65. },
  66. create(context) {
  67. const [{ allowForLoopAfterthoughts }] = context.options;
  68. return {
  69. UpdateExpression(node) {
  70. if (allowForLoopAfterthoughts && isForLoopAfterthought(node)) {
  71. return;
  72. }
  73. context.report({
  74. node,
  75. messageId: "unexpectedUnaryOp",
  76. data: {
  77. operator: node.operator
  78. }
  79. });
  80. }
  81. };
  82. }
  83. };