mm_eslint 1.2.8 → 1.2.9

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.
Files changed (3) hide show
  1. package/detector.js +216 -0
  2. package/index.js +88 -20
  3. package/package.json +1 -1
package/detector.js CHANGED
@@ -391,6 +391,14 @@ Detector.prototype._checkConstantName = function (constant_name, node, val_node)
391
391
  return null;
392
392
  }
393
393
 
394
+ // 检查是否为解构导入(从模块导入的变量)
395
+ var is_destructured_import = this._isDestructuredImport(node);
396
+
397
+ // 如果是解构导入,则不进行常量检测
398
+ if (is_destructured_import) {
399
+ return null;
400
+ }
401
+
394
402
  // 检查是否为复杂类型(方法、函数、实例等)
395
403
  var is_complex_type = this._isComplexType(val_node);
396
404
 
@@ -692,6 +700,14 @@ Detector.prototype._checkPropertyName = function (
692
700
  parent_node,
693
701
  ) {
694
702
  try {
703
+ // 检查是否为解构导入中的属性
704
+ var is_destructured_import = this._isDestructuredImportProperty(node, parent_node);
705
+
706
+ // 如果是解构导入中的属性,则不进行属性名检测
707
+ if (is_destructured_import) {
708
+ return null;
709
+ }
710
+
695
711
  // 获取属性类型
696
712
  var prop_type = this._getPropType(val_node, prop_name, parent_node);
697
713
  var rule_type = '';
@@ -906,6 +922,128 @@ Detector.prototype._isComplexType = function (val_node) {
906
922
  }
907
923
  };
908
924
 
925
+ /**
926
+ * 检查是否为解构导入
927
+ * @param {object} node AST节点
928
+ * @returns {boolean} 是否为解构导入
929
+ */
930
+ Detector.prototype._isDestructuredImport = function (node) {
931
+ try {
932
+ if (!node || !node.parent) {
933
+ return false;
934
+ }
935
+
936
+ // 检查是否为解构导入模式
937
+ var is_destructured = false;
938
+
939
+ // 情况1:节点是Property.key(解构赋值中的属性键)
940
+ if (node.parent.type === 'Property' && node.parent.key === node) {
941
+ var property = node.parent;
942
+
943
+ // 检查Property的父节点是否为ObjectPattern
944
+ if (property.parent && property.parent.type === 'ObjectPattern') {
945
+ var object_pattern = property.parent;
946
+
947
+ // 检查ObjectPattern的父节点是否为VariableDeclarator
948
+ if (object_pattern.parent && object_pattern.parent.type === 'VariableDeclarator') {
949
+ var declarator = object_pattern.parent;
950
+
951
+ // 检查右侧是否为require调用或其他模块导入
952
+ if (declarator.init) {
953
+ var init_type = declarator.init.type;
954
+
955
+ // 常见的模块导入方式
956
+ if (init_type === 'CallExpression' &&
957
+ declarator.init.callee &&
958
+ declarator.init.callee.name === 'require') {
959
+ is_destructured = true;
960
+ }
961
+
962
+ // 如果是ImportDeclaration(ES6导入)
963
+ if (init_type === 'ImportExpression') {
964
+ is_destructured = true;
965
+ }
966
+ }
967
+ }
968
+ }
969
+ }
970
+
971
+ // 情况2:节点是VariableDeclarator(简单变量声明)
972
+ else if (node.parent.type === 'VariableDeclarator') {
973
+ var declarator = node.parent;
974
+
975
+ // 检查是否为解构模式(ObjectPattern或ArrayPattern)
976
+ if (declarator.id && declarator.id.type === 'ObjectPattern') {
977
+ // 检查右侧是否为require调用或其他模块导入
978
+ if (declarator.init) {
979
+ var init_type = declarator.init.type;
980
+
981
+ // 常见的模块导入方式
982
+ if (init_type === 'CallExpression' &&
983
+ declarator.init.callee &&
984
+ declarator.init.callee.name === 'require') {
985
+ is_destructured = true;
986
+ }
987
+
988
+ // 如果是ImportDeclaration(ES6导入)
989
+ if (init_type === 'ImportExpression') {
990
+ is_destructured = true;
991
+ }
992
+ }
993
+ }
994
+ }
995
+
996
+ return is_destructured;
997
+ } catch (error) {
998
+ console.error('检查是否为解构导入时出错:', error);
999
+ return false;
1000
+ }
1001
+ };
1002
+
1003
+ /**
1004
+ * 检查是否为解构导入中的属性
1005
+ * @param {object} node 属性节点
1006
+ * @param {object} parent_node 父节点
1007
+ * @returns {boolean} 是否为解构导入中的属性
1008
+ */
1009
+ Detector.prototype._isDestructuredImportProperty = function (node, parent_node) {
1010
+ try {
1011
+ if (!node || !parent_node) {
1012
+ return false;
1013
+ }
1014
+
1015
+ // 检查父节点是否为ObjectPattern(解构模式)
1016
+ if (parent_node.type === 'ObjectPattern') {
1017
+ // 检查父节点的父节点是否为VariableDeclarator
1018
+ if (parent_node.parent && parent_node.parent.type === 'VariableDeclarator') {
1019
+ var declarator = parent_node.parent;
1020
+
1021
+ // 检查右侧是否为require调用或其他模块导入
1022
+ if (declarator.init) {
1023
+ var init_type = declarator.init.type;
1024
+
1025
+ // 常见的模块导入方式
1026
+ if (init_type === 'CallExpression' &&
1027
+ declarator.init.callee &&
1028
+ declarator.init.callee.name === 'require') {
1029
+ return true;
1030
+ }
1031
+
1032
+ // 如果是ImportDeclaration(ES6导入)
1033
+ if (init_type === 'ImportExpression') {
1034
+ return true;
1035
+ }
1036
+ }
1037
+ }
1038
+ }
1039
+
1040
+ return false;
1041
+ } catch (error) {
1042
+ console.error('检查是否为解构导入中的属性时出错:', error);
1043
+ return false;
1044
+ }
1045
+ };
1046
+
909
1047
  /**
910
1048
  * 检查常量名是否符合规范
911
1049
  * @param {string} constant_name 常量名
@@ -995,4 +1133,82 @@ Detector.prototype._generateRecommendedName = function (original_name, long_word
995
1133
  }
996
1134
  };
997
1135
 
1136
+ /**
1137
+ * 检测解构赋值中的常量名
1138
+ * @param {string} constant_name 常量名
1139
+ * @param {object} node AST节点
1140
+ * @returns {object|null} 错误信息或null
1141
+ */
1142
+ Detector.prototype._checkConstantNameForDestructuring = function (constant_name, node) {
1143
+ try {
1144
+ var rule = this.config.getRule('constant-name');
1145
+ if (!rule) {
1146
+ return null;
1147
+ }
1148
+
1149
+ // 检查是否为忽略词
1150
+ if (this._isIgnoredWord(constant_name, 'constant-name')) {
1151
+ return null;
1152
+ }
1153
+
1154
+ // 检查是否为解构导入(从模块导入的变量)
1155
+ var is_destructured_import = this._isDestructuredImport(node);
1156
+
1157
+ // 如果是解构导入,则不进行常量检测
1158
+ if (is_destructured_import) {
1159
+ return null;
1160
+ }
1161
+
1162
+ // 检查命名风格
1163
+ var style_err = this._validate_naming_style(constant_name, rule.styles, 'constant-name');
1164
+ if (style_err) {
1165
+ return {
1166
+ node: node,
1167
+ message: style_err,
1168
+ };
1169
+ }
1170
+
1171
+ // 检查并推荐更合适的词汇
1172
+ var rec_err = this._checkAndRecommend(constant_name, 'constant-name');
1173
+ if (rec_err) {
1174
+ return {
1175
+ node: node,
1176
+ message: rec_err,
1177
+ };
1178
+ }
1179
+
1180
+ // 检查长度
1181
+ var len_err = this._checkNameLength(constant_name, rule.min, rule.max, 'constant-name');
1182
+ if (len_err) {
1183
+ return {
1184
+ node: node,
1185
+ message: len_err,
1186
+ };
1187
+ }
1188
+
1189
+ // 检查单词长度
1190
+ var word_err = this._checkWordLength(constant_name, rule.single_word_len, 'constant-name');
1191
+ if (word_err) {
1192
+ return {
1193
+ node: node,
1194
+ message: word_err,
1195
+ };
1196
+ }
1197
+
1198
+ // 检查禁止词汇
1199
+ var forbid_err = this._checkForbiddenWords(constant_name, 'constant-name');
1200
+ if (forbid_err) {
1201
+ return {
1202
+ node: node,
1203
+ message: forbid_err,
1204
+ };
1205
+ }
1206
+
1207
+ return null;
1208
+ } catch (error) {
1209
+ console.error('检测解构赋值常量名时出错:', error);
1210
+ return null;
1211
+ }
1212
+ };
1213
+
998
1214
  module.exports = { Detector };
package/index.js CHANGED
@@ -121,19 +121,52 @@ function createNamingRules() {
121
121
  },
122
122
  create: function (context) {
123
123
  return {
124
- VariableDeclarator: function (node) {
124
+ VariableDeclaration: function (node) {
125
125
  // 排除const声明,只检测let和var声明的变量
126
- if (node.parent && node.parent.kind !== 'const') {
127
- if (node.id && node.id.name) {
128
- var variable_name = node.id.name;
129
- var err = detector._checkVariableName(variable_name, node);
130
- if (err) {
131
- context.report({
132
- node: err.node,
133
- message: err.message,
126
+ if (node.kind !== 'const') {
127
+ node.declarations.forEach(function(decl) {
128
+ // 处理简单变量声明
129
+ if (decl.id && decl.id.type === 'Identifier') {
130
+ var variable_name = decl.id.name;
131
+ var err = detector._checkVariableName(variable_name, decl);
132
+ if (err) {
133
+ context.report({
134
+ node: decl.id,
135
+ message: err.message,
136
+ });
137
+ }
138
+ }
139
+ // 处理解构赋值 - ObjectPattern
140
+ else if (decl.id && decl.id.type === 'ObjectPattern') {
141
+ decl.id.properties.forEach(function(property) {
142
+ if (property.key && property.key.type === 'Identifier') {
143
+ var variable_name = property.key.name;
144
+ var err = detector._checkVariableName(variable_name, decl);
145
+ if (err) {
146
+ context.report({
147
+ node: property.key,
148
+ message: err.message,
149
+ });
150
+ }
151
+ }
134
152
  });
135
153
  }
136
- }
154
+ // 处理解构赋值 - ArrayPattern
155
+ else if (decl.id && decl.id.type === 'ArrayPattern') {
156
+ decl.id.elements.forEach(function(element, index) {
157
+ if (element && element.type === 'Identifier') {
158
+ var variable_name = element.name;
159
+ var err = detector._checkVariableName(variable_name, decl);
160
+ if (err) {
161
+ context.report({
162
+ node: element,
163
+ message: err.message,
164
+ });
165
+ }
166
+ }
167
+ });
168
+ }
169
+ });
137
170
  }
138
171
  },
139
172
  };
@@ -219,18 +252,53 @@ function createNamingRules() {
219
252
  },
220
253
  create: function (context) {
221
254
  return {
222
- VariableDeclarator: function (node) {
223
- if (node.parent && node.parent.kind === 'const') {
224
- if (node.id && node.id.name) {
225
- var constant_name = node.id.name;
226
- var err = detector._checkConstantName(constant_name, node, node.init);
227
- if (err) {
228
- context.report({
229
- node: err.node,
230
- message: err.message,
255
+ VariableDeclaration: function (node) {
256
+ if (node.kind === 'const') {
257
+ node.declarations.forEach(function(decl) {
258
+ // 处理简单变量声明
259
+ if (decl.id && decl.id.type === 'Identifier') {
260
+ var constant_name = decl.id.name;
261
+ var err = detector._checkConstantName(constant_name, decl, decl.init);
262
+ if (err) {
263
+ context.report({
264
+ node: decl.id,
265
+ message: err.message,
266
+ });
267
+ }
268
+ }
269
+ // 处理解构赋值 - ObjectPattern
270
+ else if (decl.id && decl.id.type === 'ObjectPattern') {
271
+ decl.id.properties.forEach(function(property) {
272
+ if (property.key && property.key.type === 'Identifier') {
273
+ var constant_name = property.key.name;
274
+ // 对于解构赋值,使用专门的检测方法
275
+ var err = detector._checkConstantNameForDestructuring(constant_name, property.key);
276
+ if (err) {
277
+ context.report({
278
+ node: property.key,
279
+ message: err.message,
280
+ });
281
+ }
282
+ }
231
283
  });
232
284
  }
233
- }
285
+ // 处理解构赋值 - ArrayPattern
286
+ else if (decl.id && decl.id.type === 'ArrayPattern') {
287
+ decl.id.elements.forEach(function(element, index) {
288
+ if (element && element.type === 'Identifier') {
289
+ var constant_name = element.name;
290
+ // 对于解构赋值,使用专门的检测方法
291
+ var err = detector._checkConstantNameForDestructuring(constant_name, element);
292
+ if (err) {
293
+ context.report({
294
+ node: element,
295
+ message: err.message,
296
+ });
297
+ }
298
+ }
299
+ });
300
+ }
301
+ });
234
302
  }
235
303
  },
236
304
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mm_eslint",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "description": "ESLint plugin for naming conventions - PascalCase, camelCase, snake_case, and UPPER_SNAKE_CASE naming rules",
5
5
  "main": "index.js",
6
6
  "keywords": [