max-classes-per-file

强制每个文件的最大类数

包含多个类的文件通常会导致可导航性较差且结构不良的代码库。最佳实践是将每个文件限制为单一职责。

规则详情

该规则强制每个文件只能包含特定数量的类,不能更多。

此规则的错误代码示例:

/*eslint max-classes-per-file: "error"*/

class Foo {}
class Bar {}

此规则的正确代码示例:

/*eslint max-classes-per-file: "error"*/

class Foo {}

选项

wVd4kabGI1UdDXLgyyAwHm52M23Tu4KtVakPq5GNvmP7iNFHyxZHNN/JfcY73LRC7B/jhsYFCrCFnYyWZasCWQ==

JSKqLKVXZogLf84HEldXuHmejB2lBlBB9Q3g612xhIzqoEWwRLZcwspz0kiNLEIxlO2a7+vBr2tfsr4ZUxILPZSLJ9yfL44elEURPTRa06Q=

    aX/sYa+a/F2gKhe/xEzqOuIKbtCEbjh5GgILWtS2w1+lwccJNEV55h8vrpylj8Jg/V76udTWeBGWp7+a2qhBJoU8qPmIusd7qsGuw838cA1BlWvlPAo2Dp7Rm9FA5cULcadAZjw0ERZ3pmrgUY7i1Cm2wwSh9rIlCUgX6JDU4uRMpXbk08JI/Ih240gKs3QEZn9X4XnYWEg2GRQcO9ixWPSarQIcRmgic83ePuUqUTCHkT/wpngcQRS2YDYrtXVmShfkyn4eEMhs94o9ey8b272vfV9nsjyfNXqMgymjGUM=

PJ3RjirWzC+gCHcZnJlNLw==

{
    "max-classes-per-file": ["error", 1]
}
{
    "max-classes-per-file": [
        "error",
        { "ignoreExpressions": true, "max": 2 }
    ]
}

FphvHt5MK/G5o4JKMpo8wFZScNX7kGyQzE3xqEeYlFeN/Zh0wAwjlhiVt6c8SVBTv1eKuK740lpGTLUiPDY5ZiRT4Mdtj+dZH6r6ePBGX39zFSsfsIyyVZilZ8upcHr5

/* eslint max-classes-per-file: ["error", 2] */

class Foo {}
class Bar {}

FphvHt5MK/G5o4JKMpo8wFZScNX7kGyQzE3xqEeYlFfSOCo6boElU5F/9xjJlCpy+zGyzuHBL6a0XQipzRkJLM3poPCHpO+mMbAXg5OkMmoZzFJQObttShv0nImLlvkzWLsU8PtimMzV6j4Z889fYA==

/* eslint max-classes-per-file: ["error", { ignoreExpressions: true }] */

class VisitorFactory {
    forDescriptor(descriptor) {
        return class {
            visit(node) {
                return `Visiting ${descriptor}.`;
            }
        };
    }
}