no-plusplus.js 1.7 KB

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