You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

max-classes-per-file.js 1.6 KiB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. docs: {
  15. description: "enforce a maximum number of classes per file",
  16. category: "Best Practices",
  17. recommended: false,
  18. url: "https://eslint.org/docs/rules/max-classes-per-file"
  19. },
  20. schema: [
  21. {
  22. type: "integer",
  23. minimum: 1
  24. }
  25. ],
  26. messages: {
  27. maximumExceeded: "Number of classes per file must not exceed {{ max }}"
  28. }
  29. },
  30. create(context) {
  31. const maxClasses = context.options[0] || 1;
  32. let classCount = 0;
  33. return {
  34. Program() {
  35. classCount = 0;
  36. },
  37. "Program:exit"(node) {
  38. if (classCount > maxClasses) {
  39. context.report({
  40. node,
  41. messageId: "maximumExceeded",
  42. data: {
  43. max: maxClasses
  44. }
  45. });
  46. }
  47. },
  48. "ClassDeclaration, ClassExpression"() {
  49. classCount++;
  50. }
  51. };
  52. }
  53. };