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 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 16 | /** @type {import('../shared/types').Rule} */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 17 | module.exports = { |
| 18 | meta: { |
| 19 | type: "suggestion", |
| 20 | |
| 21 | docs: { |
| 22 | description: "enforce a maximum number of classes per file", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 23 | recommended: false, |
| 24 | url: "https://eslint.org/docs/rules/max-classes-per-file" |
| 25 | }, |
| 26 | |
| 27 | schema: [ |
| 28 | { |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 29 | oneOf: [ |
| 30 | { |
| 31 | type: "integer", |
| 32 | minimum: 1 |
| 33 | }, |
| 34 | { |
| 35 | type: "object", |
| 36 | properties: { |
| 37 | ignoreExpressions: { |
| 38 | type: "boolean" |
| 39 | }, |
| 40 | max: { |
| 41 | type: "integer", |
| 42 | minimum: 1 |
| 43 | } |
| 44 | }, |
| 45 | additionalProperties: false |
| 46 | } |
| 47 | ] |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 48 | } |
| 49 | ], |
| 50 | |
| 51 | messages: { |
| 52 | maximumExceeded: "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}." |
| 53 | } |
| 54 | }, |
| 55 | create(context) { |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 56 | const [option = {}] = context.options; |
| 57 | const [ignoreExpressions, max] = typeof option === "number" |
| 58 | ? [false, option || 1] |
| 59 | : [option.ignoreExpressions, option.max || 1]; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 60 | |
| 61 | let classCount = 0; |
| 62 | |
| 63 | return { |
| 64 | Program() { |
| 65 | classCount = 0; |
| 66 | }, |
| 67 | "Program:exit"(node) { |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 68 | if (classCount > max) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 69 | context.report({ |
| 70 | node, |
| 71 | messageId: "maximumExceeded", |
| 72 | data: { |
| 73 | classCount, |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 74 | max |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 75 | } |
| 76 | }); |
| 77 | } |
| 78 | }, |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 79 | "ClassDeclaration"() { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 80 | classCount++; |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 81 | }, |
| 82 | "ClassExpression"() { |
| 83 | if (!ignoreExpressions) { |
| 84 | classCount++; |
| 85 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 86 | } |
| 87 | }; |
| 88 | } |
| 89 | }; |