|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
2.6 KB
|
| Line | |
|---|
| 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 | /** @type {import('../shared/types').Rule} */
|
|---|
| 17 | module.exports = {
|
|---|
| 18 | meta: {
|
|---|
| 19 | type: "suggestion",
|
|---|
| 20 |
|
|---|
| 21 | docs: {
|
|---|
| 22 | description: "Enforce a maximum number of classes per file",
|
|---|
| 23 | recommended: false,
|
|---|
| 24 | url: "https://eslint.org/docs/latest/rules/max-classes-per-file"
|
|---|
| 25 | },
|
|---|
| 26 |
|
|---|
| 27 | schema: [
|
|---|
| 28 | {
|
|---|
| 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 | ]
|
|---|
| 48 | }
|
|---|
| 49 | ],
|
|---|
| 50 |
|
|---|
| 51 | messages: {
|
|---|
| 52 | maximumExceeded: "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}."
|
|---|
| 53 | }
|
|---|
| 54 | },
|
|---|
| 55 | create(context) {
|
|---|
| 56 | const [option = {}] = context.options;
|
|---|
| 57 | const [ignoreExpressions, max] = typeof option === "number"
|
|---|
| 58 | ? [false, option || 1]
|
|---|
| 59 | : [option.ignoreExpressions, option.max || 1];
|
|---|
| 60 |
|
|---|
| 61 | let classCount = 0;
|
|---|
| 62 |
|
|---|
| 63 | return {
|
|---|
| 64 | Program() {
|
|---|
| 65 | classCount = 0;
|
|---|
| 66 | },
|
|---|
| 67 | "Program:exit"(node) {
|
|---|
| 68 | if (classCount > max) {
|
|---|
| 69 | context.report({
|
|---|
| 70 | node,
|
|---|
| 71 | messageId: "maximumExceeded",
|
|---|
| 72 | data: {
|
|---|
| 73 | classCount,
|
|---|
| 74 | max
|
|---|
| 75 | }
|
|---|
| 76 | });
|
|---|
| 77 | }
|
|---|
| 78 | },
|
|---|
| 79 | "ClassDeclaration"() {
|
|---|
| 80 | classCount++;
|
|---|
| 81 | },
|
|---|
| 82 | "ClassExpression"() {
|
|---|
| 83 | if (!ignoreExpressions) {
|
|---|
| 84 | classCount++;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 | };
|
|---|
| 88 | }
|
|---|
| 89 | };
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.