Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 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 | //------------------------------------------------------------------------------ |
| 8 | // Requirements |
| 9 | //------------------------------------------------------------------------------ |
| 10 | |
| 11 | |
| 12 | //------------------------------------------------------------------------------ |
| 13 | // Rule Definition |
| 14 | //------------------------------------------------------------------------------ |
| 15 | |
| 16 | module.exports = { |
| 17 | meta: { |
| 18 | type: "suggestion", |
| 19 | |
| 20 | docs: { |
| 21 | description: "enforce a maximum number of classes per file", |
| 22 | category: "Best Practices", |
| 23 | recommended: false, |
| 24 | url: "https://eslint.org/docs/rules/max-classes-per-file" |
| 25 | }, |
| 26 | |
| 27 | schema: [ |
| 28 | { |
| 29 | type: "integer", |
| 30 | minimum: 1 |
| 31 | } |
| 32 | ], |
| 33 | |
| 34 | messages: { |
| 35 | maximumExceeded: "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}." |
| 36 | } |
| 37 | }, |
| 38 | create(context) { |
| 39 | |
| 40 | const maxClasses = context.options[0] || 1; |
| 41 | |
| 42 | let classCount = 0; |
| 43 | |
| 44 | return { |
| 45 | Program() { |
| 46 | classCount = 0; |
| 47 | }, |
| 48 | "Program:exit"(node) { |
| 49 | if (classCount > maxClasses) { |
| 50 | context.report({ |
| 51 | node, |
| 52 | messageId: "maximumExceeded", |
| 53 | data: { |
| 54 | classCount, |
| 55 | max: maxClasses |
| 56 | } |
| 57 | }); |
| 58 | } |
| 59 | }, |
| 60 | "ClassDeclaration, ClassExpression"() { |
| 61 | classCount++; |
| 62 | } |
| 63 | }; |
| 64 | } |
| 65 | }; |