mm_eslint 1.4.4 → 1.4.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,318 @@
1
+ const { Name } = require('./name.js');
2
+
3
+ /**
4
+ * 函数名检测器类
5
+ * 负责检测函数名是否符合命名规范
6
+ */
7
+ class FunctionName extends Name {
8
+ constructor(context, config) {
9
+ super(context, config);
10
+ }
11
+ }
12
+
13
+ /**
14
+ * 函数声明节点检测函数
15
+ * @param {Object} node - 函数声明节点
16
+ * @returns {Object|undefined} - 检测结果对象或undefined
17
+ */
18
+ FunctionName.prototype.FunctionDeclaration = function (node) {
19
+ let name = node.id.name;
20
+ let original_type = 'function';
21
+ let error = this.check(node.id, name, original_type);
22
+ this.report(node.id, error, 'function');
23
+ return error;
24
+ }
25
+
26
+ /**
27
+ * 函数表达式节点检测函数
28
+ * @param {Object} node - 函数表达式节点
29
+ * @returns {Object|undefined} - 检测结果对象或undefined
30
+ */
31
+ FunctionName.prototype.FunctionExpression = function (node) {
32
+ // 检测函数表达式中的函数名
33
+ if (node.id && node.id.type === 'Identifier') {
34
+ let name = node.id.name;
35
+ let original_type = 'function';
36
+ let error = this.check(node, name, original_type);
37
+ this.report(node, error, 'function');
38
+ return error;
39
+ }
40
+ }
41
+
42
+ /**
43
+ * 变量声明节点检测函数
44
+ * @param {Object} node - 变量声明节点
45
+ * @returns {Object|undefined} - 检测结果对象或undefined
46
+ */
47
+ FunctionName.prototype.VariableDeclaration = function (node) {
48
+ // 判断右边是否有函数表达式
49
+ if (node.declarations[0].init && (node.declarations[0].init.type === 'FunctionExpression' || node.declarations[0].init.type === 'ArrowFunctionExpression')) {
50
+ let name = node.declarations[0].id.name;
51
+ let original_type = 'function';
52
+ let error = this.check(node.declarations[0].id, name, original_type);
53
+ this.report(node.declarations[0].id, error, 'function');
54
+ return error;
55
+ }
56
+
57
+ // 判断右边是否直接赋值函数名
58
+ if (node.declarations[0].init && node.declarations[0].init.type === 'Identifier') {
59
+ // 根据右边值的类型决定左边变量的类型
60
+ let original_type = this._getOriginalType(node.declarations[0], 'VariableDeclaration');
61
+
62
+ // 如果右边是函数,左边变量应该按照函数引用的命名规范检测
63
+ if (original_type === 'function') {
64
+ let name = node.declarations[0].id.name;
65
+ let error = this.check(node.declarations[0].id, name, 'function');
66
+ this.report(node.declarations[0].id, error, 'function');
67
+ return error;
68
+ }
69
+ }
70
+ }
71
+
72
+ /**
73
+ * 属性节点检测函数(函数属性)
74
+ * @param {Object} node - 属性节点
75
+ * @returns {Object|undefined} - 检测结果对象或undefined
76
+ */
77
+ FunctionName.prototype.Property = function (node) {
78
+ // 判断右边是否为函数表达式
79
+ if (node.value.type === 'FunctionExpression' || node.value.type === 'ArrowFunctionExpression') {
80
+ let name = node.key.name;
81
+ let original_type = 'property-function';
82
+ let error = this.check(node, name, original_type);
83
+ this.report(node, error, original_type);
84
+ return error;
85
+ }
86
+
87
+ // 判断右边是否直接定义为函数名
88
+ if (node.value.type === 'Identifier') {
89
+ // 根据右边值的类型决定左边属性的类型
90
+ let original_type = this._getOriginalType(node, 'Property');
91
+
92
+ // 如果右边是函数,左边属性应该按照函数引用的命名规范检测
93
+ if (original_type === 'function') {
94
+ let name = node.key.name;
95
+ let original_type = 'property-function';
96
+ let error = this.check(node, name, original_type);
97
+ this.report(node, error, original_type);
98
+ return error;
99
+ }
100
+ }
101
+ }
102
+
103
+ /**
104
+ * 类属性定义节点检测函数
105
+ * @param {Object} node - 类属性定义节点
106
+ * @returns {Object|undefined} - 检测结果对象或undefined
107
+ */
108
+ FunctionName.prototype.PropertyDefinition = function (node) {
109
+ // 判断右边是否有 new 表达式
110
+ if (node.value && (node.value.type === 'FunctionExpression' || node.value.type === 'ArrowFunctionExpression')) {
111
+ let name = node.key.name;
112
+ let original_type = 'property-function';
113
+ if (node.static) {
114
+ original_type = 'static-function';
115
+ }
116
+ else if (node.key.type === 'PrivateIdentifier') {
117
+ original_type = 'private-function';
118
+ }
119
+ else if (name.startsWith('_')) {
120
+ original_type = 'internal-function';
121
+ }
122
+ let error = this.check(node, name, original_type);
123
+ this.report(node, error, original_type);
124
+ return error;
125
+ }
126
+
127
+ // 判断右边是否直接定义为类实例名
128
+ if (node.value && node.value.type === 'Identifier') {
129
+ // 根据右边值的类型决定左边属性的类型
130
+ let original_type = this._getOriginalType(node, 'PropertyDefinition');
131
+
132
+ // 如果右边是类实例,左边属性应该按照类实例的命名规范检测(小驼峰)
133
+ if (original_type === 'function') {
134
+ let name = node.key.name;
135
+ let original_type = 'property-function';
136
+ if (node.static) {
137
+ original_type = 'static-function';
138
+ }
139
+ else if (node.key.type === 'PrivateIdentifier') {
140
+ original_type = 'private-function';
141
+ }
142
+ else if (name.startsWith('_')) {
143
+ original_type = 'internal-function';
144
+ }
145
+ let error = this.check(node, name, original_type);
146
+ this.report(node, error, original_type);
147
+ return error;
148
+ }
149
+ }
150
+ }
151
+
152
+ /**
153
+ * 方法定义节点检测函数
154
+ * @param {Object} node - 方法定义节点
155
+ * @returns {Object|undefined} - 检测结果对象或undefined
156
+ */
157
+ FunctionName.prototype.MethodDefinition = function (node) {
158
+ // 排除getter访问器(应该按照属性处理)
159
+ if (node.kind === 'get' || node.kind === 'set') {
160
+ return undefined;
161
+ }
162
+
163
+ // 检测类中的方法定义(普通方法)
164
+ if (node.key && node.key.type === 'Identifier') {
165
+ let name = node.key.name;
166
+ let original_type = 'property-function';
167
+ if (node.static) {
168
+ original_type = 'static-function';
169
+ }
170
+ else if (node.key.type === 'PrivateIdentifier') {
171
+ original_type = 'private-function';
172
+ }
173
+ else if (name.startsWith('_')) {
174
+ original_type = 'internal-function';
175
+ }
176
+ let error = this.check(node, name, original_type);
177
+ this.report(node, error, original_type);
178
+ return error;
179
+ }
180
+ }
181
+
182
+ /**
183
+ * 赋值表达式节点检测函数(函数赋值)
184
+ * @param {Object} node - 赋值表达式节点
185
+ * @returns {Object|undefined} - 检测结果对象或undefined
186
+ */
187
+ FunctionName.prototype.AssignmentExpression = function (node) {
188
+ // 检测 exports 或 module.exports 赋值:exports.functionName = functionName;
189
+ if (node.operator === '=' && node.left && node.left.type === 'MemberExpression') {
190
+ // 检查左边是否为 exports 或 module.exports
191
+ let left_object = node.left.object;
192
+ let is_exports = false;
193
+
194
+ if (left_object.type === 'Identifier' && left_object.name === 'exports') {
195
+ is_exports = true;
196
+ } else if (left_object.type === 'MemberExpression' &&
197
+ left_object.object.type === 'Identifier' &&
198
+ left_object.object.name === 'module' &&
199
+ left_object.property.type === 'Identifier' &&
200
+ left_object.property.name === 'exports') {
201
+ is_exports = true;
202
+ }
203
+
204
+ if (is_exports && node.right && node.right.type === 'Identifier') {
205
+ // 先判断右边是否为函数
206
+ let original_type = this._getOriginalType(node, 'AssignmentExpression');
207
+
208
+ // 如果右边是函数,才检测左边的属性名
209
+ if (original_type === 'function') {
210
+ let left_property = node.left.property;
211
+ if (left_property && left_property.type === 'Identifier') {
212
+ let name = left_property.name;
213
+ let original_type = 'property-function';
214
+ let error = this.check(left_property, name, original_type);
215
+ this.report(left_property, error, original_type);
216
+ return error;
217
+ }
218
+ }
219
+ }
220
+ }
221
+
222
+ return undefined;
223
+ }
224
+
225
+ /**
226
+ * 调用表达式节点检测函数(函数调用)
227
+ * @param {Object} node - 调用表达式节点
228
+ * @returns {Object|undefined} - 检测结果对象或undefined
229
+ */
230
+ FunctionName.prototype.CallExpression = function (node) {
231
+ // 检测函数调用中的函数名
232
+ if (node.callee && node.callee.type === 'Identifier') {
233
+ let name = node.callee.name;
234
+ let original_type = 'use-function';
235
+ let error = this.check(node, name, original_type);
236
+ this.report(node, error, original_type);
237
+ return error;
238
+ }
239
+ }
240
+
241
+ /**
242
+ * 默认导出节点检测函数(函数导出)
243
+ * @param {Object} node - 默认导出节点
244
+ * @returns {Object|undefined} - 检测结果对象或undefined
245
+ */
246
+ FunctionName.prototype.ExportDefaultDeclaration = function (node) {
247
+ // 检测导出的函数表达式
248
+ if (node.declaration && (node.declaration.type === 'FunctionExpression' || node.declaration.type === 'ArrowFunctionExpression')) {
249
+ let name = node.declaration.id ? node.declaration.id.name : 'AnonymousFunction';
250
+ let original_type = 'export-function';
251
+ let error = this.check(node.declaration, name, original_type);
252
+ this.report(node.declaration, error, original_type);
253
+ return error;
254
+ }
255
+
256
+ // 检测导出的函数名
257
+ if (node.declaration && node.declaration.type === 'Identifier') {
258
+ let type = this._getOriginalType(node.declaration, 'ExportDefaultDeclaration');
259
+ if (type === 'function') {
260
+ let name = node.declaration.name;
261
+ let original_type = 'export-function';
262
+ let error = this.check(node.declaration, name, original_type);
263
+ this.report(node.declaration, error, original_type);
264
+ return error;
265
+ }
266
+ }
267
+
268
+ // 如果没有匹配的导出类型,返回 undefined
269
+ return undefined;
270
+ }
271
+
272
+ /**
273
+ * 命名导出节点检测函数(函数导出)
274
+ * @param {Object} node - 命名导出节点
275
+ * @returns {Object|undefined} - 检测结果对象或undefined
276
+ */
277
+ FunctionName.prototype.ExportNamedDeclaration = function (node) {
278
+ // 检测导出的函数声明(如 export function functionName() {})
279
+ if (node.declaration && node.declaration.type === 'FunctionDeclaration') {
280
+ let name = node.declaration.id ? node.declaration.id.name : 'AnonymousFunction';
281
+ let error = this.check(node, name, 'export-function');
282
+ this.report(node, error, 'export-function');
283
+ return error;
284
+ }
285
+
286
+ // 检测命名导出的函数引用
287
+ if (node.specifiers && node.specifiers.length > 0) {
288
+ for (let i = 0; i < node.specifiers.length; i++) {
289
+ let specifier = node.specifiers[i];
290
+
291
+ // 检测导出的标识符(避免重复检测)
292
+ // 优先检测 exported 属性,如果存在则使用它,否则使用 local 属性
293
+ let identifier_to_check = null;
294
+ let identifier_name = '';
295
+
296
+ if (specifier.exported && specifier.exported.type === 'Identifier') {
297
+ identifier_to_check = specifier.exported;
298
+ identifier_name = specifier.exported.name;
299
+ } else if (specifier.local && specifier.local.type === 'Identifier') {
300
+ identifier_to_check = specifier.local;
301
+ identifier_name = specifier.local.name;
302
+ }
303
+
304
+ // 只有当找到有效标识符时才进行检测
305
+ if (identifier_to_check) {
306
+ let original_type = this._getOriginalType(specifier, 'ExportSpecifier');
307
+
308
+ // 只有当导出的是函数时才检测
309
+ if (original_type === 'function') {
310
+ let error = this.check(identifier_to_check, identifier_name, 'export-function');
311
+ this.report(identifier_to_check, error, 'export-function');
312
+ }
313
+ }
314
+ }
315
+ }
316
+ }
317
+
318
+ module.exports = { FunctionName }
@@ -0,0 +1,18 @@
1
+ const { ClassName } = require('./class_name.js');
2
+ const { ClassInstanceName } = require('./class_instance_name.js');
3
+ const { FunctionName } = require('./function_name.js');
4
+ const { VariableName } = require('./variable_name.js');
5
+ const { ObjectName } = require('./object_name.js');
6
+ const { ConstName } = require('./const_name.js');
7
+ const { ParamName } = require('./param_name.js');
8
+
9
+
10
+ module.exports = {
11
+ ClassName,
12
+ ClassInstanceName,
13
+ FunctionName,
14
+ VariableName,
15
+ ObjectName,
16
+ ConstName,
17
+ ParamName,
18
+ }