]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/max-classes-per-file.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / max-classes-per-file.md
CommitLineData
eb39fafa
DC
1# enforce a maximum number of classes per file (max-classes-per-file)
2
3Files containing multiple classes can often result in a less navigable
4and poorly structured codebase. Best practice is to keep each file
5limited to a single responsibility.
6
7## Rule Details
8
9This rule enforces that each file may contain only a particular number
10of classes and no more.
11
12Examples of **incorrect** code for this rule:
13
14```js
15/*eslint max-classes-per-file: "error"*/
16
17class Foo {}
18class Bar {}
19```
20
21Examples of **correct** code for this rule:
22
23```js
24/*eslint max-classes-per-file: "error"*/
25
26class Foo {}
27```
28
29## Options
30
609c276f
TL
31This rule may be configured with either an object or a number.
32
33If the option is an object, it may contain one or both of:
34
35- `ignoreExpressions`: a boolean option (defaulted to `false`) to ignore class expressions.
36- `max`: a numeric option (defaulted to 1) to specify the maximum number of classes.
eb39fafa
DC
37
38For example:
39
40```json
41{
42 "max-classes-per-file": ["error", 1]
43}
44```
45
609c276f
TL
46```json
47{
48 "max-classes-per-file": [
49 "error",
50 { "ignoreExpressions": true, "max": 2 }
51 ]
52}
53```
54
55Examples of **correct** code for this rule with the `max` option set to `2`:
eb39fafa
DC
56
57```js
58/* eslint max-classes-per-file: ["error", 2] */
59
60class Foo {}
61class Bar {}
62```
609c276f
TL
63
64Examples of **correct** code for this rule with the `ignoreExpressions` option set to `true`:
65
66```js
67/* eslint max-classes-per-file: ["error", { ignoreExpressions: true }] */
68
69class VisitorFactory {
70 forDescriptor(descriptor) {
71 return class {
72 visit(node) {
73 return `Visiting ${descriptor}.`;
74 }
75 };
76 }
77}
78```