no-void.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /**
  2. * @fileoverview Rule to disallow use of void operator.
  3. * @author Mike Sidorov
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. /** @type {import('../types').Rule.RuleModule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. defaultOptions: [
  14. {
  15. allowAsStatement: false,
  16. },
  17. ],
  18. docs: {
  19. description: "Disallow `void` operators",
  20. recommended: false,
  21. frozen: true,
  22. url: "https://eslint.org/docs/latest/rules/no-void",
  23. },
  24. messages: {
  25. noVoid: "Expected 'undefined' and instead saw 'void'.",
  26. },
  27. schema: [
  28. {
  29. type: "object",
  30. properties: {
  31. allowAsStatement: {
  32. type: "boolean",
  33. },
  34. },
  35. additionalProperties: false,
  36. },
  37. ],
  38. },
  39. create(context) {
  40. const [{ allowAsStatement }] = context.options;
  41. //--------------------------------------------------------------------------
  42. // Public
  43. //--------------------------------------------------------------------------
  44. return {
  45. 'UnaryExpression[operator="void"]'(node) {
  46. if (
  47. allowAsStatement &&
  48. node.parent &&
  49. node.parent.type === "ExpressionStatement"
  50. ) {
  51. return;
  52. }
  53. context.report({
  54. node,
  55. messageId: "noVoid",
  56. });
  57. },
  58. };
  59. },
  60. };