Skip to content

Commit ccb98d8

Browse files
ankeetmainibradzacher
authored andcommitted
fix(eslint-plugin): [expl-member-a11y] fix parameter properties (#912)
1 parent 0f63e3f commit ccb98d8

File tree

3 files changed

+250
-21
lines changed

3 files changed

+250
-21
lines changed

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

+40
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,46 @@ class Animal {
214214
}
215215
```
216216

217+
e.g. `[ { accessibility: 'off', overrides: { parameterProperties: 'explicit' } } ]`
218+
219+
The following code is considered incorrect with the example override
220+
221+
```ts
222+
class Animal {
223+
constructor(readonly animalName: string) {}
224+
}
225+
```
226+
227+
The following code patterns are considered correct with the example override
228+
229+
```ts
230+
class Animal {
231+
constructor(public readonly animalName: string) {}
232+
}
233+
234+
class Animal {
235+
constructor(public animalName: string) {}
236+
}
237+
```
238+
239+
e.g. `[ { accessibility: 'off', overrides: { parameterProperties: 'no-public' } } ]`
240+
241+
The following code is considered incorrect with the example override
242+
243+
```ts
244+
class Animal {
245+
constructor(public readonly animalName: string) {}
246+
}
247+
```
248+
249+
The following code is considered correct with the example override
250+
251+
```ts
252+
class Animal {
253+
constructor(public animalName: string) {}
254+
}
255+
```
256+
217257
#### Disable any checks on given member type
218258

219259
e.g. `[{ overrides: { accessors : 'off' } } ]`

Diff for: packages/eslint-plugin/src/rules/explicit-member-accessibility.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,24 @@ export default util.createRule<Options, MessageIds>({
196196
: // has to be an Identifier or TSC will throw an error
197197
(node.parameter.left as TSESTree.Identifier).name;
198198

199-
if (paramPropCheck === 'no-public' && node.accessibility === 'public') {
200-
reportIssue('unwantedPublicAccessibility', nodeType, node, nodeName);
199+
switch (paramPropCheck) {
200+
case 'explicit': {
201+
if (!node.accessibility) {
202+
reportIssue('missingAccessibility', nodeType, node, nodeName);
203+
}
204+
break;
205+
}
206+
case 'no-public': {
207+
if (node.accessibility === 'public' && node.readonly) {
208+
reportIssue(
209+
'unwantedPublicAccessibility',
210+
nodeType,
211+
node,
212+
nodeName,
213+
);
214+
}
215+
break;
216+
}
201217
}
202218
}
203219

Diff for: packages/eslint-plugin/tests/rules/explicit-member-accessibility.test.ts

+192-19
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,104 @@ ruleTester.run('explicit-member-accessibility', rule, {
1010
{
1111
filename: 'test.ts',
1212
code: `
13+
class Test {
14+
public constructor(private foo: string) {}
15+
}
16+
`,
17+
options: [
18+
{
19+
accessibility: 'explicit',
20+
overrides: { parameterProperties: 'explicit' },
21+
},
22+
],
23+
},
24+
{
25+
filename: 'test.ts',
26+
code: `
27+
class Test {
28+
public constructor(private readonly foo: string) {}
29+
}
30+
`,
31+
options: [
32+
{
33+
accessibility: 'explicit',
34+
overrides: { parameterProperties: 'explicit' },
35+
},
36+
],
37+
},
38+
{
39+
filename: 'test.ts',
40+
code: `
41+
class Test {
42+
public constructor(private foo: string) {}
43+
}
44+
`,
45+
options: [
46+
{
47+
accessibility: 'explicit',
48+
overrides: { parameterProperties: 'off' },
49+
},
50+
],
51+
},
52+
{
53+
filename: 'test.ts',
54+
code: `
55+
class Test {
56+
public constructor(protected foo: string) {}
57+
}
58+
`,
59+
options: [
60+
{
61+
accessibility: 'explicit',
62+
overrides: { parameterProperties: 'off' },
63+
},
64+
],
65+
},
66+
{
67+
filename: 'test.ts',
68+
code: `
69+
class Test {
70+
public constructor(public foo: string) {}
71+
}
72+
`,
73+
options: [
74+
{
75+
accessibility: 'explicit',
76+
overrides: { parameterProperties: 'off' },
77+
},
78+
],
79+
},
80+
{
81+
filename: 'test.ts',
82+
code: `
83+
class Test {
84+
public constructor(readonly foo: string) {}
85+
}
86+
`,
87+
options: [
88+
{
89+
accessibility: 'explicit',
90+
overrides: { parameterProperties: 'off' },
91+
},
92+
],
93+
},
94+
{
95+
filename: 'test.ts',
96+
code: `
97+
class Test {
98+
public constructor(private readonly foo: string) {}
99+
}
100+
`,
101+
options: [
102+
{
103+
accessibility: 'explicit',
104+
overrides: { parameterProperties: 'off' },
105+
},
106+
],
107+
},
108+
{
109+
filename: 'test.ts',
110+
code: `
13111
class Test {
14112
protected name: string
15113
private x: number
@@ -147,11 +245,90 @@ class Test {
147245
},
148246
],
149247
},
248+
{
249+
filename: 'test.ts',
250+
code: `
251+
class Test {
252+
constructor(public foo: number){}
253+
}
254+
`,
255+
options: [
256+
{
257+
accessibility: 'no-public',
258+
},
259+
],
260+
},
150261
],
151262
invalid: [
152263
{
153264
filename: 'test.ts',
154265
code: `
266+
export class XXXX {
267+
public constructor(readonly value: string) {}
268+
}
269+
`,
270+
options: [
271+
{
272+
accessibility: 'off',
273+
overrides: {
274+
parameterProperties: 'explicit',
275+
},
276+
},
277+
],
278+
errors: [
279+
{
280+
messageId: 'missingAccessibility',
281+
column: 22,
282+
line: 3,
283+
},
284+
],
285+
},
286+
{
287+
filename: 'test.ts',
288+
code: `
289+
export class WithParameterProperty {
290+
public constructor(readonly value: string) {}
291+
}
292+
`,
293+
options: [{ accessibility: 'explicit' }],
294+
errors: [{ messageId: 'missingAccessibility' }],
295+
},
296+
{
297+
filename: 'test.ts',
298+
code: `
299+
export class XXXX {
300+
public constructor(readonly samosa: string) {}
301+
}
302+
`,
303+
options: [
304+
{
305+
accessibility: 'off',
306+
overrides: {
307+
constructors: 'explicit',
308+
parameterProperties: 'explicit',
309+
},
310+
},
311+
],
312+
errors: [{ messageId: 'missingAccessibility' }],
313+
},
314+
{
315+
filename: 'test.ts',
316+
code: `
317+
class Test {
318+
public constructor(readonly foo: string) {}
319+
}
320+
`,
321+
options: [
322+
{
323+
accessibility: 'explicit',
324+
overrides: { parameterProperties: 'explicit' },
325+
},
326+
],
327+
errors: [{ messageId: 'missingAccessibility' }],
328+
},
329+
{
330+
filename: 'test.ts',
331+
code: `
155332
class Test {
156333
x: number
157334
public getX () {
@@ -365,18 +542,21 @@ class Test {
365542
code: `
366543
class Test {
367544
constructor(public x: number){}
545+
public foo(): string {
546+
return 'foo';
547+
}
368548
}
369549
`,
370550
errors: [
371551
{
372-
messageId: 'unwantedPublicAccessibility',
552+
messageId: 'missingAccessibility',
373553
line: 3,
374-
column: 15,
554+
column: 3,
375555
},
376556
],
377557
options: [
378558
{
379-
accessibility: 'no-public',
559+
overrides: { parameterProperties: 'no-public' },
380560
},
381561
],
382562
},
@@ -385,9 +565,6 @@ class Test {
385565
code: `
386566
class Test {
387567
constructor(public x: number){}
388-
public foo(): string {
389-
return 'foo';
390-
}
391568
}
392569
`,
393570
errors: [
@@ -396,30 +573,26 @@ class Test {
396573
line: 3,
397574
column: 3,
398575
},
399-
{
400-
messageId: 'unwantedPublicAccessibility',
401-
line: 3,
402-
column: 15,
403-
},
404-
],
405-
options: [
406-
{
407-
overrides: { parameterProperties: 'no-public' },
408-
},
409576
],
410577
},
411578
{
412579
filename: 'test.ts',
413580
code: `
414581
class Test {
415-
constructor(public x: number){}
582+
constructor(public readonly x: number){}
416583
}
417584
`,
585+
options: [
586+
{
587+
accessibility: 'off',
588+
overrides: { parameterProperties: 'no-public' },
589+
},
590+
],
418591
errors: [
419592
{
420-
messageId: 'missingAccessibility',
593+
messageId: 'unwantedPublicAccessibility',
421594
line: 3,
422-
column: 3,
595+
column: 15,
423596
},
424597
],
425598
},

0 commit comments

Comments
 (0)