no-void.js 1.7 KB

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