no-plusplus.js 3.1 KB

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