no-plusplus.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. docs: {
  13. description: "disallow the unary operators `++` and `--`",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-plusplus"
  17. },
  18. schema: [
  19. {
  20. type: "object",
  21. properties: {
  22. allowForLoopAfterthoughts: {
  23. type: "boolean"
  24. }
  25. },
  26. additionalProperties: false
  27. }
  28. ]
  29. },
  30. create(context) {
  31. const config = context.options[0];
  32. let allowInForAfterthought = false;
  33. if (typeof config === "object") {
  34. allowInForAfterthought = config.allowForLoopAfterthoughts === true;
  35. }
  36. return {
  37. UpdateExpression(node) {
  38. if (allowInForAfterthought && node.parent.type === "ForStatement") {
  39. return;
  40. }
  41. context.report({
  42. node,
  43. message: "Unary operator '{{operator}}' used.",
  44. data: {
  45. operator: node.operator
  46. }
  47. });
  48. }
  49. };
  50. }
  51. };