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.
- package/README.md +56 -19
- package/README_EN.md +57 -20
- package/index.js +200 -1045
- package/{config.js → lib/config.js} +95 -30
- package/{corrector.js → lib/corrector.js} +54 -2
- package/lib/detector/class_instance_name.js +246 -0
- package/lib/detector/class_name.js +261 -0
- package/lib/detector/const_name.js +519 -0
- package/lib/detector/function_name.js +318 -0
- package/lib/detector/index.js +18 -0
- package/lib/detector/name.js +626 -0
- package/lib/detector/object_name.js +245 -0
- package/lib/detector/param_name.js +247 -0
- package/lib/detector/variable_name.js +286 -0
- package/lib/fix/class_fix.js +83 -0
- package/lib/fix/export_fix.js +169 -0
- package/lib/fix/function_fix.js +85 -0
- package/lib/fix/index.js +21 -0
- package/lib/fix/method_fix.js +82 -0
- package/lib/fix/param_fix.js +63 -0
- package/lib/fix/property_fix.js +93 -0
- package/lib/fix/variable_fix.js +134 -0
- package/lib/fix.js +160 -0
- package/lib/tip.js +85 -0
- package/{util.js → lib/util.js} +8 -0
- package/{validator.js → lib/validator.js} +12 -16
- package/package.json +13 -11
- package/detector.js +0 -1239
- package/eslint.config.js +0 -22
- package/tip.js +0 -71
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 方法相关修复函数
|
|
3
|
+
* 负责处理方法定义、类方法等AST节点的修复逻辑
|
|
4
|
+
*/
|
|
5
|
+
class MethodFix {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数
|
|
8
|
+
* @param {object} config 配置对象
|
|
9
|
+
*/
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 创建方法修复函数
|
|
16
|
+
* @param {object} context ESLint上下文
|
|
17
|
+
* @param {object} node AST节点
|
|
18
|
+
* @param {object} error 错误信息
|
|
19
|
+
* @returns {function} 修复函数
|
|
20
|
+
*/
|
|
21
|
+
createMethodFixFunction(context, node, error) {
|
|
22
|
+
var old_name = error.name;
|
|
23
|
+
var new_name = error.fix_suggestion;
|
|
24
|
+
|
|
25
|
+
if (!old_name || !new_name) {
|
|
26
|
+
return function(fixer) {
|
|
27
|
+
return [];
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return function(fixer) {
|
|
32
|
+
// 方法定义特殊处理
|
|
33
|
+
if (node.type === 'MethodDefinition' && node.key && (node.key.type === 'Identifier' || node.key.type === 'PrivateIdentifier') && node.key.name === old_name) {
|
|
34
|
+
// 私有标识符需要保留 # 前缀
|
|
35
|
+
if (node.key.type === 'PrivateIdentifier') {
|
|
36
|
+
return fixer.replaceText(node.key, '#' + new_name);
|
|
37
|
+
}
|
|
38
|
+
return fixer.replaceText(node.key, new_name);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// 类中的方法处理
|
|
42
|
+
if (node.type === 'ClassBody' && node.parent && node.parent.type === 'ClassDeclaration') {
|
|
43
|
+
for (var i = 0; i < node.body.length; i++) {
|
|
44
|
+
var method = node.body[i];
|
|
45
|
+
if (method.type === 'MethodDefinition' && method.key && method.key.type === 'Identifier' && method.key.name === old_name) {
|
|
46
|
+
return fixer.replaceText(method.key, new_name);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 通用查找作为备用方案
|
|
52
|
+
var identifier_node = this._findIdentifierNode(node, old_name);
|
|
53
|
+
if (identifier_node) {
|
|
54
|
+
return fixer.replaceText(identifier_node, new_name);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return [];
|
|
58
|
+
}.bind(this);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* 查找标识符节点
|
|
63
|
+
* @param {object} node AST节点
|
|
64
|
+
* @param {string} name 名称
|
|
65
|
+
* @returns {object|null} 标识符节点
|
|
66
|
+
*/
|
|
67
|
+
_findIdentifierNode(node, name) {
|
|
68
|
+
// 如果是标识符节点且名称匹配
|
|
69
|
+
if (node.type === 'Identifier' && node.name === name) {
|
|
70
|
+
return node;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// 如果是方法定义,检查方法名标识符
|
|
74
|
+
if (node.type === 'MethodDefinition' && node.key && node.key.type === 'Identifier' && node.key.name === name) {
|
|
75
|
+
return node.key;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
module.exports = { MethodFix };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 参数相关修复函数
|
|
3
|
+
* 负责处理函数参数等AST节点的修复逻辑
|
|
4
|
+
*/
|
|
5
|
+
class ParamFix {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数
|
|
8
|
+
* @param {object} config 配置对象
|
|
9
|
+
*/
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 创建参数修复函数
|
|
16
|
+
* @param {object} context ESLint上下文
|
|
17
|
+
* @param {object} node AST节点
|
|
18
|
+
* @param {object} error 错误信息
|
|
19
|
+
* @returns {function} 修复函数
|
|
20
|
+
*/
|
|
21
|
+
createParamFixFunction(context, node, error) {
|
|
22
|
+
var old_name = error.name;
|
|
23
|
+
var new_name = error.fix_suggestion;
|
|
24
|
+
|
|
25
|
+
if (!old_name || !new_name) {
|
|
26
|
+
return function(fixer) {
|
|
27
|
+
return [];
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return function(fixer) {
|
|
32
|
+
// 函数参数特殊处理:直接查找参数标识符
|
|
33
|
+
if (node.type === 'Identifier' && node.name === old_name) {
|
|
34
|
+
return fixer.replaceText(node, new_name);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 通用查找作为备用方案
|
|
38
|
+
var identifier_node = this._findIdentifierNode(node, old_name);
|
|
39
|
+
if (identifier_node) {
|
|
40
|
+
return fixer.replaceText(identifier_node, new_name);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
return [];
|
|
44
|
+
}.bind(this);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 查找标识符节点
|
|
49
|
+
* @param {object} node AST节点
|
|
50
|
+
* @param {string} name 名称
|
|
51
|
+
* @returns {object|null} 标识符节点
|
|
52
|
+
*/
|
|
53
|
+
_findIdentifierNode(node, name) {
|
|
54
|
+
// 如果是标识符节点且名称匹配
|
|
55
|
+
if (node.type === 'Identifier' && node.name === name) {
|
|
56
|
+
return node;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return null;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
module.exports = { ParamFix };
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 属性相关修复函数
|
|
3
|
+
* 负责处理对象属性、类属性定义等AST节点的修复逻辑
|
|
4
|
+
*/
|
|
5
|
+
class PropertyFix {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数
|
|
8
|
+
* @param {object} config 配置对象
|
|
9
|
+
*/
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 创建属性修复函数
|
|
16
|
+
* @param {object} context ESLint上下文
|
|
17
|
+
* @param {object} node AST节点
|
|
18
|
+
* @param {object} error 错误信息
|
|
19
|
+
* @returns {function} 修复函数
|
|
20
|
+
*/
|
|
21
|
+
createPropertyFixFunction(context, node, error) {
|
|
22
|
+
var old_name = error.name;
|
|
23
|
+
var new_name = error.fix_suggestion;
|
|
24
|
+
|
|
25
|
+
if (!old_name || !new_name) {
|
|
26
|
+
return function(fixer) {
|
|
27
|
+
return [];
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return function(fixer) {
|
|
32
|
+
// 对象属性特殊处理
|
|
33
|
+
if (node.type === 'Property' && node.key && node.key.type === 'Identifier' && node.key.name === old_name) {
|
|
34
|
+
return fixer.replaceText(node.key, new_name);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// 类属性定义处理
|
|
38
|
+
if (node.type === 'PropertyDefinition' && node.key && (node.key.type === 'Identifier' || node.key.type === 'PrivateIdentifier') && node.key.name === old_name) {
|
|
39
|
+
// 私有标识符需要保留 # 前缀
|
|
40
|
+
if (node.key.type === 'PrivateIdentifier') {
|
|
41
|
+
return fixer.replaceText(node.key, '#' + new_name);
|
|
42
|
+
}
|
|
43
|
+
return fixer.replaceText(node.key, new_name);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// 对象字面量属性处理
|
|
47
|
+
if (node.type === 'ObjectProperty' && node.key && node.key.type === 'Identifier' && node.key.name === old_name) {
|
|
48
|
+
return fixer.replaceText(node.key, new_name);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// 赋值表达式中的成员表达式属性处理(如 Usersb.prototype._GetUserpe)
|
|
52
|
+
if (node.type === 'AssignmentExpression' && node.left && node.left.type === 'MemberExpression' &&
|
|
53
|
+
node.left.property && node.left.property.type === 'Identifier' && node.left.property.name === old_name) {
|
|
54
|
+
return fixer.replaceText(node.left.property, new_name);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// 通用查找作为备用方案
|
|
58
|
+
var identifier_node = this._findIdentifierNode(node, old_name);
|
|
59
|
+
if (identifier_node) {
|
|
60
|
+
return fixer.replaceText(identifier_node, new_name);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return [];
|
|
64
|
+
}.bind(this);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 查找标识符节点
|
|
69
|
+
* @param {object} node AST节点
|
|
70
|
+
* @param {string} name 名称
|
|
71
|
+
* @returns {object|null} 标识符节点
|
|
72
|
+
*/
|
|
73
|
+
_findIdentifierNode(node, name) {
|
|
74
|
+
// 如果是标识符节点且名称匹配
|
|
75
|
+
if (node.type === 'Identifier' && node.name === name) {
|
|
76
|
+
return node;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// 如果是属性,检查属性名标识符
|
|
80
|
+
if (node.type === 'Property' && node.key && node.key.type === 'Identifier' && node.key.name === name) {
|
|
81
|
+
return node.key;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// 如果是类属性定义,检查属性名标识符
|
|
85
|
+
if (node.type === 'PropertyDefinition' && node.key && node.key.type === 'Identifier' && node.key.name === name) {
|
|
86
|
+
return node.key;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
module.exports = { PropertyFix };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 变量相关修复函数
|
|
3
|
+
* 负责处理变量声明、赋值表达式等AST节点的修复逻辑
|
|
4
|
+
*/
|
|
5
|
+
class VariableFix {
|
|
6
|
+
/**
|
|
7
|
+
* 构造函数
|
|
8
|
+
* @param {object} config 配置对象
|
|
9
|
+
*/
|
|
10
|
+
constructor(config) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 创建变量声明修复函数
|
|
16
|
+
* @param {object} context ESLint上下文
|
|
17
|
+
* @param {object} node AST节点
|
|
18
|
+
* @param {object} error 错误信息
|
|
19
|
+
* @returns {function} 修复函数
|
|
20
|
+
*/
|
|
21
|
+
createVariableFixFunction(context, node, error) {
|
|
22
|
+
var old_name = error.name;
|
|
23
|
+
var new_name = error.fix_suggestion;
|
|
24
|
+
|
|
25
|
+
if (!old_name || !new_name) {
|
|
26
|
+
return function(fixer) {
|
|
27
|
+
return [];
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return function(fixer) {
|
|
32
|
+
var fixes = [];
|
|
33
|
+
|
|
34
|
+
// 变量声明特殊处理:直接查找变量标识符
|
|
35
|
+
if (node.type === 'VariableDeclarator' && node.id && node.id.type === 'Identifier' && node.id.name === old_name) {
|
|
36
|
+
fixes.push(fixer.replaceText(node.id, new_name));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// 赋值表达式中的变量处理
|
|
40
|
+
if (node.type === 'AssignmentExpression' && node.left && node.left.type === 'Identifier' && node.left.name === old_name) {
|
|
41
|
+
fixes.push(fixer.replaceText(node.left, new_name));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 通用查找作为备用方案
|
|
45
|
+
var identifier_node = this._findIdentifierNode(node, old_name);
|
|
46
|
+
if (identifier_node) {
|
|
47
|
+
fixes.push(fixer.replaceText(identifier_node, new_name));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return fixes;
|
|
51
|
+
}.bind(this);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* 查找标识符节点
|
|
56
|
+
* @param {object} node AST节点
|
|
57
|
+
* @param {string} name 名称
|
|
58
|
+
* @returns {object|null} 标识符节点
|
|
59
|
+
*/
|
|
60
|
+
_findIdentifierNode(node, name) {
|
|
61
|
+
// 如果是标识符节点且名称匹配
|
|
62
|
+
if (node.type === 'Identifier' && node.name === name) {
|
|
63
|
+
return node;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 如果是变量声明,检查声明标识符
|
|
67
|
+
if (node.type === 'VariableDeclaration' && node.declarations && node.declarations.length > 0) {
|
|
68
|
+
for (var i = 0; i < node.declarations.length; i++) {
|
|
69
|
+
var decl = node.declarations[i];
|
|
70
|
+
if (decl.id && decl.id.type === 'Identifier' && decl.id.name === name) {
|
|
71
|
+
return decl.id;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 如果是变量声明器,检查声明标识符
|
|
77
|
+
if (node.type === 'VariableDeclarator' && node.id && node.id.type === 'Identifier' && node.id.name === name) {
|
|
78
|
+
return node.id;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 如果是表达式中的标识符引用(如 Name + Age)
|
|
82
|
+
if (node.type === 'BinaryExpression' || node.type === 'CallExpression' || node.type === 'MemberExpression' || node.type === 'VariableDeclaration') {
|
|
83
|
+
// 递归查找子节点中的标识符
|
|
84
|
+
for (var key in node) {
|
|
85
|
+
if (node[key] && typeof node[key] === 'object') {
|
|
86
|
+
var result = this._findIdentifierNode(node[key], name);
|
|
87
|
+
if (result) {
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* 查找所有标识符节点
|
|
99
|
+
* @param {object} node AST节点
|
|
100
|
+
* @param {string} name 名称
|
|
101
|
+
* @returns {array} 标识符节点数组
|
|
102
|
+
*/
|
|
103
|
+
_findAllIdentifiers(node, name) {
|
|
104
|
+
var identifiers = [];
|
|
105
|
+
var visited = new Set();
|
|
106
|
+
|
|
107
|
+
// 递归遍历AST树
|
|
108
|
+
function traverse(current_node) {
|
|
109
|
+
if (!current_node || typeof current_node !== 'object' || visited.has(current_node)) {
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// 标记为已访问,避免无限递归
|
|
114
|
+
visited.add(current_node);
|
|
115
|
+
|
|
116
|
+
// 如果是标识符节点且名称匹配
|
|
117
|
+
if (current_node.type === 'Identifier' && current_node.name === name) {
|
|
118
|
+
identifiers.push(current_node);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// 递归遍历所有子节点
|
|
122
|
+
for (var key in current_node) {
|
|
123
|
+
if (current_node[key] && typeof current_node[key] === 'object') {
|
|
124
|
+
traverse(current_node[key]);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
traverse(node);
|
|
130
|
+
return identifiers;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
module.exports = { VariableFix };
|
package/lib/fix.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 自动修复功能类
|
|
3
|
+
* 负责处理命名规范的自动修复逻辑
|
|
4
|
+
* 使用模块化修复函数,根据AST节点类型分发修复任务
|
|
5
|
+
*/
|
|
6
|
+
const {
|
|
7
|
+
ClassFix,
|
|
8
|
+
VariableFix,
|
|
9
|
+
FunctionFix,
|
|
10
|
+
PropertyFix,
|
|
11
|
+
MethodFix,
|
|
12
|
+
ParamFix,
|
|
13
|
+
ExportFix
|
|
14
|
+
} = require('./fix/index.js');
|
|
15
|
+
|
|
16
|
+
class Fix {
|
|
17
|
+
/**
|
|
18
|
+
* 构造函数
|
|
19
|
+
* @param {object} config 配置对象
|
|
20
|
+
*/
|
|
21
|
+
constructor(config) {
|
|
22
|
+
this.config = config;
|
|
23
|
+
|
|
24
|
+
// 初始化各个修复类实例
|
|
25
|
+
this.class_fix = new ClassFix(config);
|
|
26
|
+
this.variable_fix = new VariableFix(config);
|
|
27
|
+
this.function_fix = new FunctionFix(config);
|
|
28
|
+
this.property_fix = new PropertyFix(config);
|
|
29
|
+
this.method_fix = new MethodFix(config);
|
|
30
|
+
this.param_fix = new ParamFix(config);
|
|
31
|
+
this.export_fix = new ExportFix(config);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 根据节点类型创建修复函数
|
|
36
|
+
* @param {object} context ESLint上下文
|
|
37
|
+
* @param {object} node AST节点
|
|
38
|
+
* @param {object} error 错误信息
|
|
39
|
+
* @returns {function} 修复函数
|
|
40
|
+
*/
|
|
41
|
+
createFixFunction(context, node, error) {
|
|
42
|
+
var node_type = node.type;
|
|
43
|
+
var original_type = error.original_type;
|
|
44
|
+
|
|
45
|
+
// 根据节点类型或错误类型选择对应的修复函数
|
|
46
|
+
switch (node_type) {
|
|
47
|
+
case 'ClassDeclaration':
|
|
48
|
+
case 'ClassExpression':
|
|
49
|
+
return this.class_fix.createClassFixFunction(context, node, error);
|
|
50
|
+
|
|
51
|
+
case 'VariableDeclaration':
|
|
52
|
+
case 'VariableDeclarator':
|
|
53
|
+
case 'AssignmentExpression':
|
|
54
|
+
return this.variable_fix.createVariableFixFunction(context, node, error);
|
|
55
|
+
|
|
56
|
+
case 'FunctionDeclaration':
|
|
57
|
+
case 'FunctionExpression':
|
|
58
|
+
case 'ArrowFunctionExpression':
|
|
59
|
+
case 'CallExpression':
|
|
60
|
+
return this.function_fix.createFunctionFixFunction(context, node, error);
|
|
61
|
+
|
|
62
|
+
case 'Property':
|
|
63
|
+
case 'PropertyDefinition':
|
|
64
|
+
case 'ObjectProperty':
|
|
65
|
+
return this.property_fix.createPropertyFixFunction(context, node, error);
|
|
66
|
+
|
|
67
|
+
case 'MethodDefinition':
|
|
68
|
+
case 'ClassBody':
|
|
69
|
+
return this.method_fix.createMethodFixFunction(context, node, error);
|
|
70
|
+
|
|
71
|
+
case 'Identifier':
|
|
72
|
+
// 如果是参数标识符
|
|
73
|
+
if (original_type && original_type.includes('param')) {
|
|
74
|
+
return this.param_fix.createParamFixFunction(context, node, error);
|
|
75
|
+
}
|
|
76
|
+
// 默认使用变量修复
|
|
77
|
+
return this.variable_fix.createVariableFixFunction(context, node, error);
|
|
78
|
+
|
|
79
|
+
default:
|
|
80
|
+
// 根据错误类型选择修复函数
|
|
81
|
+
return this._createFixByErrorType(context, node, error, original_type);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 根据错误类型创建修复函数
|
|
87
|
+
* @param {object} context ESLint上下文
|
|
88
|
+
* @param {object} node AST节点
|
|
89
|
+
* @param {object} error 错误信息
|
|
90
|
+
* @param {string} original_type 原始类型
|
|
91
|
+
* @returns {function} 修复函数
|
|
92
|
+
*/
|
|
93
|
+
_createFixByErrorType(context, node, error, original_type) {
|
|
94
|
+
if (!original_type) {
|
|
95
|
+
return function(fixer) {
|
|
96
|
+
return [];
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
switch (original_type) {
|
|
101
|
+
case 'class':
|
|
102
|
+
case 'super-class':
|
|
103
|
+
return this.class_fix.createClassFixFunction(context, node, error);
|
|
104
|
+
|
|
105
|
+
case 'variable':
|
|
106
|
+
case 'object':
|
|
107
|
+
case 'constant':
|
|
108
|
+
return this.variable_fix.createVariableFixFunction(context, node, error);
|
|
109
|
+
|
|
110
|
+
case 'function':
|
|
111
|
+
return this.function_fix.createFunctionFixFunction(context, node, error);
|
|
112
|
+
|
|
113
|
+
case 'property':
|
|
114
|
+
case 'object-property':
|
|
115
|
+
case 'class-property':
|
|
116
|
+
return this.property_fix.createPropertyFixFunction(context, node, error);
|
|
117
|
+
|
|
118
|
+
case 'method':
|
|
119
|
+
case 'class-method':
|
|
120
|
+
return this.method_fix.createMethodFixFunction(context, node, error);
|
|
121
|
+
|
|
122
|
+
case 'param':
|
|
123
|
+
case 'function-param':
|
|
124
|
+
case 'arrow-function-param':
|
|
125
|
+
return this.param_fix.createParamFixFunction(context, node, error);
|
|
126
|
+
|
|
127
|
+
default:
|
|
128
|
+
return this.variable_fix.createVariableFixFunction(context, node, error);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* 修复导出语句中的引用
|
|
134
|
+
* @param {object} context ESLint上下文
|
|
135
|
+
* @param {string} old_name 旧名称
|
|
136
|
+
* @param {string} new_name 新名称
|
|
137
|
+
* @param {object} node AST节点
|
|
138
|
+
* @param {object} fixer 修复器
|
|
139
|
+
* @returns {array} 修复数组
|
|
140
|
+
*/
|
|
141
|
+
fixExportReferences(context, old_name, new_name, node, fixer) {
|
|
142
|
+
return this.export_fix.fixExportReferences(context, old_name, new_name, node, fixer);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 创建修复函数(向后兼容)
|
|
147
|
+
* @param {Object} error 错误对象
|
|
148
|
+
* @param {string} suggestion 建议修复内容
|
|
149
|
+
* @returns 修复对象
|
|
150
|
+
*/
|
|
151
|
+
createFunction(error, suggestion) {
|
|
152
|
+
// 保持向后兼容,但实际修复逻辑已转移到模块化修复函数中
|
|
153
|
+
return {
|
|
154
|
+
range: error.range,
|
|
155
|
+
text: suggestion
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
module.exports = { Fix };
|
package/lib/tip.js
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
var { Config } = require('./config');
|
|
2
|
+
var { Corrector } = require('./corrector');
|
|
3
|
+
var corrector = new Corrector();
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 提示管理类
|
|
7
|
+
* 统一管理所有错误提示和建议使用词的生成
|
|
8
|
+
*/
|
|
9
|
+
class Tip {
|
|
10
|
+
/**
|
|
11
|
+
* 构造函数
|
|
12
|
+
* @param {object} config 配置对象
|
|
13
|
+
*/
|
|
14
|
+
constructor(config) {
|
|
15
|
+
this.config = config || new Config();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* 生成完整的错误提示
|
|
21
|
+
* @param {object} error 错误信息
|
|
22
|
+
* @param {string} fix_suggestion 修正建议
|
|
23
|
+
* @returns {string} 完整的错误提示
|
|
24
|
+
*/
|
|
25
|
+
Tip.prototype.getFullMessage = function(error, fix_suggestion) {
|
|
26
|
+
var base_message = this._getBaseMessage(error);
|
|
27
|
+
var suggestion = fix_suggestion || corrector.getSuggestion(error);
|
|
28
|
+
if (suggestion) {
|
|
29
|
+
return base_message + ',建议使用:' + suggestion;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return base_message;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* 获取声明方式问题的错误消息
|
|
37
|
+
* @param {string} issue_type 问题类型
|
|
38
|
+
* @param {string} name 名称
|
|
39
|
+
* @returns {string} 错误消息
|
|
40
|
+
*/
|
|
41
|
+
Tip.prototype.getDeclarationIssueMessage = function(issue_type, name) {
|
|
42
|
+
switch (issue_type) {
|
|
43
|
+
case 'const-should-be-let':
|
|
44
|
+
return `变量'${name}'应该用let声明而不是const`;
|
|
45
|
+
default:
|
|
46
|
+
return `变量'${name}'声明方式不符合规范`;
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* 获取基础错误消息
|
|
52
|
+
* @param {object} error 错误信息对象
|
|
53
|
+
* @returns {string} 基础错误消息
|
|
54
|
+
*/
|
|
55
|
+
Tip.prototype._getBaseMessage = function(error) {
|
|
56
|
+
var type_name_map = this.config.getTypeNameMap();
|
|
57
|
+
var name = error.name;
|
|
58
|
+
var original_type = error.original_type || 'variable';
|
|
59
|
+
var type_name = type_name_map[original_type] || '名称';
|
|
60
|
+
|
|
61
|
+
switch (error.type) {
|
|
62
|
+
case 'style':
|
|
63
|
+
var styles = error.rule.styles.join('、');
|
|
64
|
+
return `${type_name}'${name}'命名不符合${styles}风格`;
|
|
65
|
+
|
|
66
|
+
case 'length':
|
|
67
|
+
if (error.min_length) {
|
|
68
|
+
return `${type_name}'${name}'小于最小长度${error.min_length}字符`;
|
|
69
|
+
} else {
|
|
70
|
+
return `${type_name}'${name}'超过最大长度${error.max_length}字符`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
case 'word_length':
|
|
74
|
+
var word = error.word || name;
|
|
75
|
+
return `${type_name}'${name}'拼接单词"${word}"超过最大长度${error.max_word_length}字符`;
|
|
76
|
+
|
|
77
|
+
case 'forbidden_word':
|
|
78
|
+
return `${type_name}'${name}'拼接了禁用词"${error.forbidden_word}"`;
|
|
79
|
+
|
|
80
|
+
default:
|
|
81
|
+
return `${type_name}'${name}'不符合命名规范`;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
module.exports = { Tip };
|
package/{util.js → lib/util.js}
RENAMED
|
@@ -107,6 +107,14 @@ module.exports = {
|
|
|
107
107
|
* @returns {object} 包含前缀和实际名称的对象
|
|
108
108
|
*/
|
|
109
109
|
getRealName(name) {
|
|
110
|
+
// 如果name为undefined或null,返回默认值
|
|
111
|
+
if (name === undefined || name === null) {
|
|
112
|
+
return {
|
|
113
|
+
prefix: '',
|
|
114
|
+
real_name: ''
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
110
118
|
var real_name = name;
|
|
111
119
|
var prefix = '';
|
|
112
120
|
if (real_name.indexOf('_') === 0) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
const { Config } = require('./config');
|
|
2
|
-
const { splitWords, getRealName } = require('./util');
|
|
1
|
+
const { Config } = require('./config.js');
|
|
2
|
+
const { splitWords, getRealName } = require('./util.js');
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* 命名规范验证器类
|
|
@@ -152,7 +152,7 @@ Validator.prototype._validateLength = function (name, min = 1, max = 32) {
|
|
|
152
152
|
* @returns {string} 验证类型
|
|
153
153
|
*/
|
|
154
154
|
Validator.prototype._getValidationType = function (original_type) {
|
|
155
|
-
return
|
|
155
|
+
return this.config.getType(original_type);
|
|
156
156
|
};
|
|
157
157
|
|
|
158
158
|
/**
|
|
@@ -218,9 +218,9 @@ Validator.prototype._validateForbiddenWords = function (words, forbidden_words)
|
|
|
218
218
|
if (!forbidden_words || forbidden_words.length === 0) {
|
|
219
219
|
return null;
|
|
220
220
|
}
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
let word = this.getForbiddenWord(
|
|
221
|
+
|
|
222
|
+
for (var i = 0; i < words.length; i++) {
|
|
223
|
+
let word = this.getForbiddenWord(forbidden_words, words[i]);
|
|
224
224
|
if (word) {
|
|
225
225
|
return {
|
|
226
226
|
type: 'forbidden_word',
|
|
@@ -229,24 +229,20 @@ Validator.prototype._validateForbiddenWords = function (words, forbidden_words)
|
|
|
229
229
|
};
|
|
230
230
|
}
|
|
231
231
|
}
|
|
232
|
-
|
|
233
232
|
return null;
|
|
234
233
|
};
|
|
235
234
|
|
|
236
235
|
/**
|
|
237
236
|
* 检查单词数组是否包含禁止拼接词
|
|
238
|
-
* @param {array}
|
|
239
|
-
* @param {string}
|
|
237
|
+
* @param {array} forbidden_words 禁止拼接词组
|
|
238
|
+
* @param {string} word 单词
|
|
240
239
|
* @returns {boolean} 是否包含禁止拼接词
|
|
241
240
|
*/
|
|
242
|
-
Validator.prototype.getForbiddenWord = function (
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
for (var i = 0; i < words.length; i++) {
|
|
247
|
-
var word = words[i];
|
|
241
|
+
Validator.prototype.getForbiddenWord = function (forbidden_words, word) {
|
|
242
|
+
let word_lower = word.toLowerCase();
|
|
243
|
+
for (var i = 0; i < forbidden_words.length; i++) {
|
|
248
244
|
// 忽略大小写进行比较
|
|
249
|
-
if (
|
|
245
|
+
if (forbidden_words[i].toLowerCase() === word_lower) {
|
|
250
246
|
return word;
|
|
251
247
|
}
|
|
252
248
|
}
|