You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: packages/eslint-plugin/docs/rules/explicit-member-accessibility.md
+227-8
Original file line number
Diff line number
Diff line change
@@ -10,28 +10,247 @@ be easier to use.
10
10
This rule aims to make code more readable and explicit about who can use
11
11
which properties.
12
12
13
-
The following patterns are considered warnings:
13
+
## Options
14
+
15
+
```ts
16
+
typeAccessibilityLevel=
17
+
|'explicit'// require an accessor (including public)
18
+
|'no-public'// don't require public
19
+
|'off'; // don't check
20
+
21
+
interfaceConfig {
22
+
accessibility?:AccessibilityLevel;
23
+
overrides?: {
24
+
accessors?:AccessibilityLevel;
25
+
constructors?:AccessibilityLevel;
26
+
methods?:AccessibilityLevel;
27
+
properties?:AccessibilityLevel;
28
+
parameterProperties?:AccessibilityLevel;
29
+
};
30
+
}
31
+
```
32
+
33
+
Default config:
34
+
35
+
```JSON
36
+
{ "accessibility": "explicit" }
37
+
```
38
+
39
+
This rule in it's default state requires no configuration and will enforce that every class member has an accessibility modifier. If you would like to allow for some implicit public members then you have the following options:
40
+
A possible configuration could be:
41
+
42
+
```ts
43
+
{
44
+
accessibility: 'explicit',
45
+
overrides {
46
+
accessors: 'explicit',
47
+
constructors: 'no-public',
48
+
methods: 'explicit',
49
+
properties: 'off',
50
+
parameterProperties: 'explicit'
51
+
}
52
+
}
53
+
```
54
+
55
+
The following patterns are considered incorrect code if no options are provided:
56
+
57
+
```ts
58
+
classAnimal {
59
+
constructor(name) {
60
+
// No accessibility modifier
61
+
this.animalName=name;
62
+
}
63
+
animalName:string; // No accessibility modifier
64
+
get name():string {
65
+
// No accessibility modifier
66
+
returnthis.animalName;
67
+
}
68
+
set name(value:string) {
69
+
// No accessibility modifier
70
+
this.animalName=value;
71
+
}
72
+
walk() {
73
+
// method
74
+
}
75
+
}
76
+
```
77
+
78
+
The following patterns are considered correct with the default options `{ accessibility: 'explicit' }`:
79
+
80
+
```ts
81
+
classAnimal {
82
+
publicconstructor(publicbreed, animalName) {
83
+
// Parameter property and constructor
84
+
this.animalName=name;
85
+
}
86
+
private animalName:string; // Property
87
+
get name():string {
88
+
// get accessor
89
+
returnthis.animalName;
90
+
}
91
+
set name(value:string) {
92
+
// set accessor
93
+
this.animalName=value;
94
+
}
95
+
public walk() {
96
+
// method
97
+
}
98
+
}
99
+
```
100
+
101
+
The following patterns are considered incorrect with the accessibility set to **no-public**`[{ accessibility: 'no-public' }]`:
102
+
103
+
```ts
104
+
classAnimal {
105
+
publicconstructor(publicbreed, animalName) {
106
+
// Parameter property and constructor
107
+
this.animalName=name;
108
+
}
109
+
public animalName:string; // Property
110
+
publicget name():string {
111
+
// get accessor
112
+
returnthis.animalName;
113
+
}
114
+
publicset name(value:string) {
115
+
// set accessor
116
+
this.animalName=value;
117
+
}
118
+
public walk() {
119
+
// method
120
+
}
121
+
}
122
+
```
123
+
124
+
The following patterns are considered correct with the accessibility set to **no-public**`[{ accessibility: 'no-public' }]`:
125
+
126
+
```ts
127
+
classAnimal {
128
+
constructor(protectedbreed, animalName) {
129
+
// Parameter property and constructor
130
+
this.name=name;
131
+
}
132
+
private animalName:string; // Property
133
+
get name():string {
134
+
// get accessor
135
+
returnthis.animalName;
136
+
}
137
+
privateset name(value:string) {
138
+
// set accessor
139
+
this.animalName=value;
140
+
}
141
+
protected walk() {
142
+
// method
143
+
}
144
+
}
145
+
```
146
+
147
+
### Overrides
148
+
149
+
There are three ways in which an override can be used.
150
+
151
+
- To disallow the use of public on a given member.
152
+
- To enforce explicit member accessibility when the root has allowed implicit public accessibility
153
+
- To disable any checks on given member type
154
+
155
+
#### Disallow the use of public on a given member
156
+
157
+
e.g. `[ { overrides: { constructor: 'no-public' } } ]`
158
+
159
+
The following patterns are considered incorrect with the example override
160
+
161
+
```ts
162
+
classAnimal {
163
+
publicconstructor(protectedanimalName) {}
164
+
publicget name() {
165
+
returnthis.animalName;
166
+
}
167
+
}
168
+
```
169
+
170
+
The following patterns are considered correct with the example override
171
+
172
+
```ts
173
+
classAnimal {
174
+
constructor(protectedanimalName) {}
175
+
publicget name() {
176
+
returnthis.animalName;
177
+
}
178
+
}
179
+
```
180
+
181
+
#### Require explicit accessibility for a given member
public getName():string {} // explicit accessibility modifier
241
+
publicconstructor(protectedanimalName) {}
242
+
publicget name() {
243
+
returnthis.animalName;
244
+
}
245
+
get legs() {
246
+
returnthis.legCount;
247
+
}
28
248
}
29
249
```
30
250
31
251
## When Not To Use It
32
252
33
-
If you think defaulting to public is a good default, then you will not need
34
-
this rule.
253
+
If you think defaulting to public is a good default, then you should consider using the `no-public` setting. If you want to mix implicit and explicit public members then disable this rule.
0 commit comments