/
explicit-member-accessibility.ts
229 lines (212 loc) · 6.08 KB
/
explicit-member-accessibility.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { TSESTree } from '@typescript-eslint/typescript-estree';
import * as util from '../util';
enum Check {
Yes,
No,
NoPublic,
}
interface NoPublic {
noPublic?: boolean;
}
type Override = boolean | NoPublic;
/**
* Type guard to ease checking on which kind of override is being supplied
*
* @param {Override} [option]
* @returns {option is NoPublic}
*/
function isNoPublic(option?: Override): option is NoPublic {
return typeof option !== 'boolean';
}
interface Config {
noPublic?: boolean;
overrides?: {
accessors?: Override;
constructors?: Override;
methods?: Override;
properties?: Override;
};
}
type Options = [Config];
type MessageIds = 'unwantedPublicAccessibility' | 'missingAccessibility';
const override = {
type: ['object', 'boolean'],
properties: {
noPublic: {
type: 'boolean',
},
},
};
export default util.createRule<Options, MessageIds>({
name: 'explicit-member-accessibility',
meta: {
type: 'problem',
docs: {
description:
'Require explicit accessibility modifiers on class properties and methods',
tslintRuleName: 'member-access',
category: 'Best Practices',
recommended: 'error',
},
messages: {
missingAccessibility:
'Missing accessibility modifier on {{type}} {{name}}.',
unwantedPublicAccessibility:
'Public accessibility modifier on {{type}} {{name}}.',
},
schema: [
{
type: 'object',
properties: {
noPublic: {
type: 'boolean',
},
overrides: {
type: 'object',
properties: {
accessors: override,
constructors: override,
methods: override,
parameterProperties: override,
},
},
},
additionalProperties: false,
},
],
},
defaultOptions: [
// technically there is a default, but the overrides mean
// that if we apply them here, it will break the no override case.
{},
],
create(context) {
const option: Config = util.applyDefault([{}], context.options)[0];
/**
* @param defaultCheck
* @param overrideToCheck
*/
/**
* Reads the value set on the Override and returns a Check value
* Check value is used to control what, if any accessiblity modifiers are required or banned
* @param {Check} defaultCheck
* @param {Override} [overrideToCheck]
* @returns {Check}
*/
function parseOverride(
defaultCheck: Check,
overrideToCheck?: Override,
): Check {
let result: Check = defaultCheck;
if (typeof overrideToCheck !== 'undefined') {
if (isNoPublic(overrideToCheck) && overrideToCheck.noPublic) {
result = Check.NoPublic;
} else if (!overrideToCheck) {
result = Check.No;
}
}
return result;
}
let baseCheck = Check.Yes;
if (option.noPublic) {
baseCheck = Check.NoPublic;
}
let ctorCheck: Check = baseCheck;
let accessorCheck: Check = baseCheck;
let methodCheck: Check = baseCheck;
let propCheck: Check = baseCheck;
if (option.overrides) {
ctorCheck = parseOverride(baseCheck, option.overrides.constructors);
accessorCheck = parseOverride(baseCheck, option.overrides.accessors);
methodCheck = parseOverride(baseCheck, option.overrides.methods);
propCheck = parseOverride(baseCheck, option.overrides.properties);
}
/**
*Generates the report for rule violations
*
* @param {MessageIds} messageId
* @param {string} nodeType
* @param {(TSESTree.MethodDefinition | TSESTree.ClassProperty)} node
*/
function reportIssue(
messageId: MessageIds,
nodeType: string,
node: TSESTree.MethodDefinition | TSESTree.ClassProperty,
) {
context.report({
node: node,
messageId: messageId,
data: {
type: nodeType,
name: util.getNameFromPropertyName(node.key),
},
});
}
/**
* Checks if a method declaration has an accessibility modifier.
* @param methodDefinition The node representing a MethodDefinition.
*/
function checkMethodAccessibilityModifier(
methodDefinition: TSESTree.MethodDefinition,
): void {
let nodeType = 'method definition';
let check: Check = baseCheck;
switch (methodDefinition.kind) {
case 'method':
check = methodCheck;
break;
case 'constructor':
check = ctorCheck;
break;
case 'get':
case 'set':
check = accessorCheck;
nodeType = `${methodDefinition.kind} property accessor`;
break;
default:
check = baseCheck;
break;
}
if (check == Check.No) {
return;
}
if (util.isTypeScriptFile(context.getFilename())) {
if (
check === Check.NoPublic &&
methodDefinition.accessibility === 'public'
) {
reportIssue(
'unwantedPublicAccessibility',
nodeType,
methodDefinition,
);
} else if (check === Check.Yes && !methodDefinition.accessibility) {
reportIssue('missingAccessibility', nodeType, methodDefinition);
}
}
}
/**
* Checks if property has an accessibility modifier.
* @param classProperty The node representing a ClassProperty.
*/
function checkPropertyAccessibilityModifier(
classProperty: TSESTree.ClassProperty,
): void {
const nodeType = 'class property';
if (util.isTypeScriptFile(context.getFilename())) {
if (
propCheck === Check.NoPublic &&
classProperty.accessibility === 'public'
) {
reportIssue('unwantedPublicAccessibility', nodeType, classProperty);
} else if (propCheck === Check.Yes && !classProperty.accessibility) {
reportIssue('missingAccessibility', nodeType, classProperty);
}
}
}
return {
ClassProperty: checkPropertyAccessibilityModifier,
MethodDefinition: checkMethodAccessibilityModifier,
};
},
});