/
member-naming.ts
140 lines (126 loc) · 3.36 KB
/
member-naming.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
import {
TSESTree,
AST_NODE_TYPES,
} from '@typescript-eslint/experimental-utils';
import * as util from '../util';
interface Config<T = string> {
private?: T;
protected?: T;
public?: T;
}
type Modifiers = keyof Config;
type Options = [Config];
type MessageIds = 'incorrectName';
export default util.createRule<Options, MessageIds>({
name: 'member-naming',
meta: {
type: 'suggestion',
docs: {
description:
'Enforces naming conventions for class members by visibility',
category: 'Stylistic Issues',
recommended: false,
},
messages: {
incorrectName:
'{{accessibility}} property {{name}} should match {{convention}}.',
},
schema: [
{
type: 'object',
properties: {
public: {
type: 'string',
minLength: 1,
format: 'regex',
},
protected: {
type: 'string',
minLength: 1,
format: 'regex',
},
private: {
type: 'string',
minLength: 1,
format: 'regex',
},
},
additionalProperties: false,
minProperties: 1,
},
],
},
defaultOptions: [{}],
create(context, [config]) {
const sourceCode = context.getSourceCode();
const conventions = (Object.keys(config) as Modifiers[]).reduce<
Config<RegExp>
>((acc, accessibility) => {
acc[accessibility] = new RegExp(config[accessibility]!);
return acc;
}, {});
function getParameterNode(
node: TSESTree.TSParameterProperty,
): TSESTree.Identifier | null {
if (node.parameter.type === AST_NODE_TYPES.AssignmentPattern) {
return node.parameter.left as TSESTree.Identifier;
}
if (node.parameter.type === AST_NODE_TYPES.Identifier) {
return node.parameter;
}
return null;
}
function validateParameterPropertyName(
node: TSESTree.TSParameterProperty,
): void {
const parameterNode = getParameterNode(node);
if (!parameterNode) {
return;
}
validateName(parameterNode, parameterNode.name, node.accessibility);
}
function validatePropertyName(
node: TSESTree.MethodDefinition | TSESTree.ClassProperty,
): void {
if (
node.type === AST_NODE_TYPES.MethodDefinition &&
node.kind === 'constructor'
) {
return;
}
validateName(
node.key,
util.getNameFromClassMember(node, sourceCode),
node.accessibility,
);
}
/**
* Check that the name matches the convention for its accessibility.
* @param {ASTNode} node the named node to evaluate.
* @param {string} name
* @param {Modifiers} accessibility
* @returns {void}
* @private
*/
function validateName(
node: TSESTree.Identifier | TSESTree.Expression,
name: string,
accessibility: Modifiers = 'public',
): void {
const convention = conventions[accessibility];
if (!convention || convention.test(name)) {
return;
}
context.report({
node,
messageId: 'incorrectName',
data: { accessibility, name, convention },
});
}
return {
TSParameterProperty: validateParameterPropertyName,
MethodDefinition: validatePropertyName,
ClassProperty: validatePropertyName,
};
},
});