2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Enforce a maximum number of classes per file
|
|
|
|
* @author James Garbutt <https://github.com/43081j>
|
|
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Requirements
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Rule Definition
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
type: "suggestion",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Enforce a maximum number of classes per file",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/max-classes-per-file"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [
|
|
|
|
{
|
2024-08-21 06:34:30 +00:00
|
|
|
oneOf: [
|
|
|
|
{
|
|
|
|
type: "integer",
|
|
|
|
minimum: 1
|
|
|
|
},
|
|
|
|
{
|
|
|
|
type: "object",
|
|
|
|
properties: {
|
|
|
|
ignoreExpressions: {
|
|
|
|
type: "boolean"
|
|
|
|
},
|
|
|
|
max: {
|
|
|
|
type: "integer",
|
|
|
|
minimum: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
additionalProperties: false
|
|
|
|
}
|
|
|
|
]
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
|
|
|
|
messages: {
|
|
|
|
maximumExceeded: "File has too many classes ({{ classCount }}). Maximum allowed is {{ max }}."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
create(context) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const [option = {}] = context.options;
|
|
|
|
const [ignoreExpressions, max] = typeof option === "number"
|
|
|
|
? [false, option || 1]
|
|
|
|
: [option.ignoreExpressions, option.max || 1];
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
let classCount = 0;
|
|
|
|
|
|
|
|
return {
|
|
|
|
Program() {
|
|
|
|
classCount = 0;
|
|
|
|
},
|
|
|
|
"Program:exit"(node) {
|
2024-08-21 06:34:30 +00:00
|
|
|
if (classCount > max) {
|
2024-08-09 12:04:48 +00:00
|
|
|
context.report({
|
|
|
|
node,
|
|
|
|
messageId: "maximumExceeded",
|
|
|
|
data: {
|
|
|
|
classCount,
|
2024-08-21 06:34:30 +00:00
|
|
|
max
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2024-08-21 06:34:30 +00:00
|
|
|
"ClassDeclaration"() {
|
2024-08-09 12:04:48 +00:00
|
|
|
classCount++;
|
2024-08-21 06:34:30 +00:00
|
|
|
},
|
|
|
|
"ClassExpression"() {
|
|
|
|
if (!ignoreExpressions) {
|
|
|
|
classCount++;
|
|
|
|
}
|
2024-08-09 12:04:48 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|