Skip to content

Commit 4b3d820

Browse files
gavinbarronbradzacher
authored andcommitted
feat(eslint-plugin): [member-accessibility] add more options (#322)
1 parent 2521b85 commit 4b3d820

File tree

3 files changed

+719
-37
lines changed

3 files changed

+719
-37
lines changed

packages/eslint-plugin/docs/rules/explicit-member-accessibility.md

+227-8
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,247 @@ be easier to use.
1010
This rule aims to make code more readable and explicit about who can use
1111
which properties.
1212

13-
The following patterns are considered warnings:
13+
## Options
14+
15+
```ts
16+
type AccessibilityLevel =
17+
| 'explicit' // require an accessor (including public)
18+
| 'no-public' // don't require public
19+
| 'off'; // don't check
20+
21+
interface Config {
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+
class Animal {
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+
return this.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+
class Animal {
82+
public constructor(public breed, animalName) {
83+
// Parameter property and constructor
84+
this.animalName = name;
85+
}
86+
private animalName: string; // Property
87+
get name(): string {
88+
// get accessor
89+
return this.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+
class Animal {
105+
public constructor(public breed, animalName) {
106+
// Parameter property and constructor
107+
this.animalName = name;
108+
}
109+
public animalName: string; // Property
110+
public get name(): string {
111+
// get accessor
112+
return this.animalName;
113+
}
114+
public set 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+
class Animal {
128+
constructor(protected breed, animalName) {
129+
// Parameter property and constructor
130+
this.name = name;
131+
}
132+
private animalName: string; // Property
133+
get name(): string {
134+
// get accessor
135+
return this.animalName;
136+
}
137+
private set 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+
class Animal {
163+
public constructor(protected animalName) {}
164+
public get name() {
165+
return this.animalName;
166+
}
167+
}
168+
```
169+
170+
The following patterns are considered correct with the example override
171+
172+
```ts
173+
class Animal {
174+
constructor(protected animalName) {}
175+
public get name() {
176+
return this.animalName;
177+
}
178+
}
179+
```
180+
181+
#### Require explicit accessibility for a given member
182+
183+
e.g. `[ { accessibility: 'no-public', overrides: { properties: 'explicit' } } ]`
184+
185+
The following patterns are considered incorrect with the example override
186+
187+
```ts
188+
class Animal {
189+
constructor(protected animalName) {}
190+
get name() {
191+
return this.animalName;
192+
}
193+
protected set name(value: string) {
194+
this.animalName = value;
195+
}
196+
legs: number;
197+
private hasFleas: boolean;
198+
}
199+
```
200+
201+
The following patterns are considered correct with the example override
202+
203+
```ts
204+
class Animal {
205+
constructor(protected animalName) {}
206+
get name() {
207+
return this.animalName;
208+
}
209+
protected set name(value: string) {
210+
this.animalName = value;
211+
}
212+
public legs: number;
213+
private hasFleas: boolean;
214+
}
215+
```
216+
217+
#### Disable any checks on given member type
218+
219+
e.g. `[{ overrides: { accessors : 'off' } } ]`
220+
221+
As no checks on the overridden member type are performed all permutations of visibility are permitted for that member type
222+
223+
The follow pattern is considered incorrect for the given configuration
14224

15225
```ts
16226
class Animal {
17-
name: string; // No accessibility modifier
18-
getName(): string {} // No accessibility modifier
227+
constructor(protected animalName) {}
228+
public get name() {
229+
return this.animalName;
230+
}
231+
get legs() {
232+
return this.legCount;
233+
}
19234
}
20235
```
21236

22-
The following patterns are not warnings:
237+
The following patterns are considered correct with the example override
23238

24239
```ts
25240
class Animal {
26-
private name: string; // explicit accessibility modifier
27-
public getName(): string {} // explicit accessibility modifier
241+
public constructor(protected animalName) {}
242+
public get name() {
243+
return this.animalName;
244+
}
245+
get legs() {
246+
return this.legCount;
247+
}
28248
}
29249
```
30250

31251
## When Not To Use It
32252

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.
35254

36255
## Further Reading
37256

0 commit comments

Comments
 (0)