]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # enforce a maximum number of classes per file (max-classes-per-file) |
2 | ||
3 | Files containing multiple classes can often result in a less navigable | |
4 | and poorly structured codebase. Best practice is to keep each file | |
5 | limited to a single responsibility. | |
6 | ||
7 | ## Rule Details | |
8 | ||
9 | This rule enforces that each file may contain only a particular number | |
10 | of classes and no more. | |
11 | ||
12 | Examples of **incorrect** code for this rule: | |
13 | ||
14 | ```js | |
15 | /*eslint max-classes-per-file: "error"*/ | |
16 | ||
17 | class Foo {} | |
18 | class Bar {} | |
19 | ``` | |
20 | ||
21 | Examples of **correct** code for this rule: | |
22 | ||
23 | ```js | |
24 | /*eslint max-classes-per-file: "error"*/ | |
25 | ||
26 | class Foo {} | |
27 | ``` | |
28 | ||
29 | ## Options | |
30 | ||
609c276f TL |
31 | This rule may be configured with either an object or a number. |
32 | ||
33 | If 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 | |
38 | For 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 | ||
55 | Examples 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 | ||
60 | class Foo {} | |
61 | class Bar {} | |
62 | ``` | |
609c276f TL |
63 | |
64 | Examples 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 | ||
69 | class VisitorFactory { | |
70 | forDescriptor(descriptor) { | |
71 | return class { | |
72 | visit(node) { | |
73 | return `Visiting ${descriptor}.`; | |
74 | } | |
75 | }; | |
76 | } | |
77 | } | |
78 | ``` |