max-classes-per-file.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * @fileoverview Enforce a maximum number of classes per file
  3. * @author James Garbutt <https://github.com/43081j>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. //------------------------------------------------------------------------------
  10. // Rule Definition
  11. //------------------------------------------------------------------------------
  12. module.exports = {
  13. meta: {
  14. type: "suggestion",
  15. docs: {
  16. description: "enforce a maximum number of classes per file",
  17. category: "Best Practices",
  18. recommended: false,
  19. url: "https://eslint.org/docs/rules/max-classes-per-file"
  20. },
  21. schema: [
  22. {
  23. type: "integer",
  24. minimum: 1
  25. }
  26. ],
  27. messages: {
  28. maximumExceeded: "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}."
  29. }
  30. },
  31. create(context) {
  32. const maxClasses = context.options[0] || 1;
  33. let classCount = 0;
  34. return {
  35. Program() {
  36. classCount = 0;
  37. },
  38. "Program:exit"(node) {
  39. if (classCount > maxClasses) {
  40. context.report({
  41. node,
  42. messageId: "maximumExceeded",
  43. data: {
  44. classCount,
  45. max: maxClasses
  46. }
  47. });
  48. }
  49. },
  50. "ClassDeclaration, ClassExpression"() {
  51. classCount++;
  52. }
  53. };
  54. }
  55. };