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,261 @@
1
+ const { Name } = require('./name.js');
2
+
3
+ /**
4
+ * 类名检测器类
5
+ * 负责检测类名是否符合命名规范
6
+ */
7
+ class ClassName 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
+ ClassName.prototype.ClassDeclaration = function (node) {
19
+ // 检测类名
20
+ let name = node.id.name;
21
+ let original_type = 'class';
22
+ let error = this.check(node.id, name, original_type);
23
+ this.report(node.id, error, original_type);
24
+
25
+ // 检测继承的父类名(如果有)
26
+ if (node.superClass && node.superClass.type === 'Identifier') {
27
+ let super_name = node.superClass.name;
28
+ let super_original_type = 'super-class';
29
+ let super_error = this.check(node.superClass, super_name, super_original_type);
30
+ this.report(node.superClass, super_error, super_original_type);
31
+ return error || super_error;
32
+ }
33
+
34
+ return error;
35
+ }
36
+
37
+ /**
38
+ * 变量声明节点检测
39
+ * @param {Object} node - 变量声明节点
40
+ * @returns {Object|undefined} - 检测结果对象或undefined
41
+ */
42
+ ClassName.prototype.VariableDeclaration = function (node) {
43
+ // 判断右边是否有类表达式
44
+ if (node.declarations[0].init && node.declarations[0].init.type === 'ClassExpression') {
45
+ let name = node.declarations[0].id.name;
46
+ let original_type = 'class';
47
+ let error = this.check(node.declarations[0].id, name, original_type);
48
+ this.report(node.declarations[0].id, error, original_type);
49
+ return error;
50
+ }
51
+
52
+ // 判断右边是否直接赋值类名
53
+ if (node.declarations[0].init && node.declarations[0].init.type === 'Identifier') {
54
+ // 根据右边值的类型决定左边变量的类型
55
+ let original_type = this._getOriginalType(node.declarations[0], 'VariableDeclaration');
56
+
57
+ // 如果右边是类,左边变量应该按照类引用的命名规范检测(大驼峰)
58
+ if (original_type === 'class') {
59
+ let name = node.declarations[0].id.name;
60
+ let original_type = 'class';
61
+ let error = this.check(node.declarations[0].id, name, original_type);
62
+ this.report(node.declarations[0].id, error, original_type);
63
+ return error;
64
+ }
65
+ }
66
+ }
67
+
68
+ /**
69
+ * 属性节点检测函数
70
+ * @param {Object} node - 属性节点
71
+ * @returns {Object|undefined} - 检测结果对象或undefined
72
+ */
73
+ ClassName.prototype.Property = function (node) {
74
+ // 判断右边是否为类表达式
75
+ if (node.value.type === 'ClassExpression') {
76
+ let name = node.key.name;
77
+ let original_type = 'property-class';
78
+ let error = this.check(node, name, original_type);
79
+ this.report(node, error, original_type);
80
+ return error;
81
+ }
82
+
83
+ // 判断右边是否直接定义为类名
84
+ if (node.value.type === 'Identifier') {
85
+ // 根据右边值的类型决定左边属性的类型
86
+ let original_type = this._getOriginalType(node, 'Property');
87
+
88
+ // 如果右边是类,左边属性应该按照类引用的命名规范检测(大驼峰)
89
+ if (original_type === 'class') {
90
+ let name = node.key.name;
91
+ let original_type = 'property-class';
92
+ let error = this.check(node, name, original_type);
93
+ this.report(node, error, original_type);
94
+ return error;
95
+ }
96
+ }
97
+ }
98
+
99
+ /**
100
+ * 类表达式节点检测函数
101
+ * @param {Object} node - 类表达式节点
102
+ * @returns {Object|undefined} - 检测结果对象或undefined
103
+ */
104
+ ClassName.prototype.ClassExpression = function (node) {
105
+ // 检测类表达式中的类名:class info {}
106
+ if (node.id && node.id.type === 'Identifier') {
107
+ let name = node.id.name;
108
+ let original_type = 'class';
109
+ let error = this.check(node, name, original_type);
110
+ this.report(node, error, 'class');
111
+ return error;
112
+ }
113
+
114
+ return undefined;
115
+ }
116
+
117
+ /**
118
+ * 新表达式节点检测函数
119
+ * @param {Object} node - 新表达式节点
120
+ * @returns {Object|undefined} - 检测结果对象或undefined
121
+ */
122
+ ClassName.prototype.NewExpression = function (node) {
123
+ // 检测独立的新表达式:new ClassName();
124
+ if (node.callee && node.callee.type === 'Identifier') {
125
+ let name = node.callee.name;
126
+ let original_type = 'use-class';
127
+ let error = this.check(node.callee, name, original_type);
128
+ this.report(node.callee, error, original_type);
129
+ return error;
130
+ }
131
+
132
+ return undefined;
133
+ }
134
+
135
+ /**
136
+ * 赋值表达式节点检测函数
137
+ * @param {Object} node - 赋值表达式节点
138
+ * @returns {Object|undefined} - 检测结果对象或undefined
139
+ */
140
+ ClassName.prototype.AssignmentExpression = function (node) {
141
+ // 检测 exports 或 module.exports 赋值:exports.className = className;
142
+ if (node.operator === '=' && node.left && node.left.type === 'MemberExpression') {
143
+ // 检查左边是否为 exports 或 module.exports
144
+ let left_object = node.left.object;
145
+ let is_exports = false;
146
+
147
+ if (left_object.type === 'Identifier' && left_object.name === 'exports') {
148
+ is_exports = true;
149
+ } else if (left_object.type === 'MemberExpression' &&
150
+ left_object.object.type === 'Identifier' &&
151
+ left_object.object.name === 'module' &&
152
+ left_object.property.type === 'Identifier' &&
153
+ left_object.property.name === 'exports') {
154
+ is_exports = true;
155
+ }
156
+
157
+ if (is_exports && node.right && node.right.type === 'Identifier') {
158
+ // 先判断右边是否为类
159
+ let original_type = this._getOriginalType(node, 'AssignmentExpression');
160
+
161
+ // 如果右边是类,才检测左边的属性名
162
+ if (original_type === 'class') {
163
+ let left_property = node.left.property;
164
+ if (left_property && left_property.type === 'Identifier') {
165
+ let name = left_property.name;
166
+ let original_type = 'property-class';
167
+ let error = this.check(left_property, name, original_type);
168
+ this.report(left_property, error, original_type);
169
+ return error;
170
+ }
171
+ }
172
+ }
173
+ }
174
+ }
175
+
176
+ /**
177
+ * 默认导出节点检测函数
178
+ * @param {Object} node - 默认导出节点
179
+ * @returns {Object|undefined} - 检测结果对象或undefined
180
+ */
181
+ ClassName.prototype.ExportDefaultDeclaration = function (node) {
182
+ // 检测导出的类实例中的类名
183
+ if (node.declaration && node.declaration.type === 'NewExpression') {
184
+ let name = node.declaration.callee ? node.declaration.callee.name : 'AnonymousInstance';
185
+ let original_type = 'export-class';
186
+ let error = this.check(node, name, original_type);
187
+ this.report(node, error, original_type);
188
+ return error;
189
+ }
190
+
191
+ // 检测导出的类表达式
192
+ if (node.declaration && node.declaration.type === 'ClassExpression') {
193
+ let name = node.declaration.id ? node.declaration.id.name : 'AnonymousClass';
194
+ let original_type = 'export-class';
195
+ let error = this.check(node, name, original_type);
196
+ this.report(node, error, original_type);
197
+ return error;
198
+ }
199
+
200
+ // 检测导出的类名
201
+ if (node.declaration && node.declaration.type === 'Identifier') {
202
+ let name = node.declaration.name;
203
+ let type = this._getOriginalType(node.declaration, 'ExportDefaultDeclaration');
204
+ if (type === 'class') {
205
+ let original_type = 'export-class';
206
+ let error = this.check(node.declaration, name, original_type);
207
+ this.report(node.declaration, error, original_type);
208
+ return error;
209
+ }
210
+ }
211
+ }
212
+
213
+ /**
214
+ * 命名导出节点检测函数
215
+ * @param {Object} node - 命名导出节点
216
+ * @returns {Object|undefined} - 检测结果对象或undefined
217
+ */
218
+ ClassName.prototype.ExportNamedDeclaration = function (node) {
219
+ // 检测导出的类声明(如 export class className {})
220
+ if (node.declaration && node.declaration.type === 'ClassDeclaration') {
221
+ let name = node.declaration.id ? node.declaration.id.name : 'AnonymousClass';
222
+ let error = this.check(node, name, 'export-class');
223
+ this.report(node, error, 'export-class');
224
+ return error;
225
+ }
226
+
227
+ // 检测命名导出的函数引用
228
+ if (node.specifiers && node.specifiers.length > 0) {
229
+ for (let i = 0; i < node.specifiers.length; i++) {
230
+ let specifier = node.specifiers[i];
231
+
232
+ // 检测导出的标识符(避免重复检测)
233
+ // 优先检测 exported 属性,如果存在则使用它,否则使用 local 属性
234
+ let identifier_to_check = null;
235
+ let identifier_name = '';
236
+
237
+ if (specifier.exported && specifier.exported.type === 'Identifier') {
238
+ identifier_to_check = specifier.exported;
239
+ identifier_name = specifier.exported.name;
240
+ } else if (specifier.local && specifier.local.type === 'Identifier') {
241
+ identifier_to_check = specifier.local;
242
+ identifier_name = specifier.local.name;
243
+ }
244
+
245
+ // 只有当找到有效标识符时才进行检测
246
+ if (identifier_to_check) {
247
+ let original_type = this._getOriginalType(specifier, 'ExportSpecifier');
248
+
249
+ // 只有当导出的是类时才检测
250
+ if (original_type === 'class') {
251
+ let error = this.check(identifier_to_check, identifier_name, 'export-class');
252
+ this.report(identifier_to_check, error, 'export-class');
253
+ }
254
+ }
255
+ }
256
+ }
257
+ }
258
+
259
+ module.exports = {
260
+ ClassName
261
+ }