no-void.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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('../shared/types').Rule} */
  10. module.exports = {
  11. meta: {
  12. type: "suggestion",
  13. defaultOptions: [{
  14. allowAsStatement: false
  15. }],
  16. docs: {
  17. description: "Disallow `void` operators",
  18. recommended: false,
  19. url: "https://eslint.org/docs/latest/rules/no-void"
  20. },
  21. messages: {
  22. noVoid: "Expected 'undefined' and instead saw 'void'."
  23. },
  24. schema: [
  25. {
  26. type: "object",
  27. properties: {
  28. allowAsStatement: {
  29. type: "boolean"
  30. }
  31. },
  32. additionalProperties: false
  33. }
  34. ]
  35. },
  36. create(context) {
  37. const [{ allowAsStatement }] = context.options;
  38. //--------------------------------------------------------------------------
  39. // Public
  40. //--------------------------------------------------------------------------
  41. return {
  42. 'UnaryExpression[operator="void"]'(node) {
  43. if (
  44. allowAsStatement &&
  45. node.parent &&
  46. node.parent.type === "ExpressionStatement"
  47. ) {
  48. return;
  49. }
  50. context.report({
  51. node,
  52. messageId: "noVoid"
  53. });
  54. }
  55. };
  56. }
  57. };