mm_validator 1.0.0 → 1.0.2
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/index.js +32 -6
- package/package.json +33 -34
- package/tool/abac/index.js +78 -5
- package/tool/basic/index.js +71 -5
- package/tool/code/index.js +460 -5
- package/tool/enum/index.js +20 -5
- package/tool/json/index.js +179 -5
- package/tool/jwt/index.js +107 -5
- package/tool/length/index.js +59 -5
- package/tool/oauth/index.js +168 -5
- package/tool/params/index.js +97 -5
- package/tool/pattern/index.js +56 -5
- package/tool/range/index.js +110 -5
- package/tool/rbac/index.js +90 -5
- package/tool/required/index.js +35 -5
- package/tool/sanitize/index.js +122 -5
- package/tool/string/index.js +73 -5
- package/tool/type/index.js +19 -5
package/tool/code/index.js
CHANGED
|
@@ -1,7 +1,462 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* 代码格式验证器
|
|
3
|
+
* 验证代码语法和格式规范,返回问题列表
|
|
4
|
+
* @param {object} ctx 上下文对象
|
|
5
|
+
* @param {object} params 步骤参数
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
async function main(params, ctx, options) {
|
|
8
|
+
var code = params.code || '';
|
|
9
|
+
var language = params.language || 'javascript';
|
|
10
|
+
var rules =
|
|
11
|
+
params.rules ||
|
|
12
|
+
[
|
|
13
|
+
'naming',
|
|
14
|
+
'semicolon',
|
|
15
|
+
'jsdoc',
|
|
16
|
+
'boolean_name',
|
|
17
|
+
'long_function',
|
|
18
|
+
'event_handler',
|
|
19
|
+
'try_catch',
|
|
20
|
+
'tab_indent',
|
|
21
|
+
'double_quote',
|
|
22
|
+
'var_decl',
|
|
23
|
+
'line_length'
|
|
24
|
+
];
|
|
25
|
+
|
|
26
|
+
if (!code) {
|
|
27
|
+
ctx.output = {
|
|
28
|
+
error: { code: 'VAL_CODE_PARAM', message: '缺少代码内容(code)' }
|
|
29
|
+
};
|
|
30
|
+
return ctx.output;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
var errors = [];
|
|
34
|
+
var lines = code.split(/\r?\n/);
|
|
35
|
+
|
|
36
|
+
// 语法检查
|
|
37
|
+
if (language === 'javascript' || language === 'js') {
|
|
38
|
+
try {
|
|
39
|
+
new Function(code);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
errors.push({
|
|
42
|
+
rule: 'syntax',
|
|
43
|
+
severity: 'error',
|
|
44
|
+
line: 0,
|
|
45
|
+
message: '语法错误: ' + e.message
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// 检查 Tab 缩进
|
|
51
|
+
if (rules.indexOf('tab_indent') !== -1) {
|
|
52
|
+
for (var i = 0; i < lines.length; i++) {
|
|
53
|
+
if (lines[i].indexOf('\t') !== -1) {
|
|
54
|
+
errors.push({
|
|
55
|
+
rule: 'tab_indent',
|
|
56
|
+
severity: 'warning',
|
|
57
|
+
line: i + 1,
|
|
58
|
+
message: '使用了 Tab 缩进,应使用 2 个空格'
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// 检查双引号字符串
|
|
65
|
+
if (rules.indexOf('double_quote') !== -1) {
|
|
66
|
+
var dq_re = /"([^"\\\n]|\\.)*"/g;
|
|
67
|
+
var dq_match;
|
|
68
|
+
while ((dq_match = dq_re.exec(code)) !== null) {
|
|
69
|
+
errors.push({
|
|
70
|
+
rule: 'double_quote',
|
|
71
|
+
severity: 'warning',
|
|
72
|
+
line: _findLine(lines, dq_match.index),
|
|
73
|
+
message: '使用了双引号字符串,应使用单引号'
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// 检查 var 声明
|
|
79
|
+
if (rules.indexOf('var_decl') !== -1) {
|
|
80
|
+
var var_re = /\bvar\s+[a-zA-Z_$]/g;
|
|
81
|
+
var var_match;
|
|
82
|
+
while ((var_match = var_re.exec(code)) !== null) {
|
|
83
|
+
errors.push({
|
|
84
|
+
rule: 'var_decl',
|
|
85
|
+
severity: 'warning',
|
|
86
|
+
line: _findLine(lines, var_match.index),
|
|
87
|
+
message: '使用了 var 声明,应使用 let 或 const'
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// 检查命名规范
|
|
93
|
+
if (rules.indexOf('naming') !== -1) {
|
|
94
|
+
_checkNaming(code, lines, errors);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// 检查分号
|
|
98
|
+
if (rules.indexOf('semicolon') !== -1) {
|
|
99
|
+
_checkSemicolon(lines, errors);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// 检查 JSDoc 注释
|
|
103
|
+
if (rules.indexOf('jsdoc') !== -1) {
|
|
104
|
+
_checkJsdoc(lines, errors);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// 检查布尔变量命名
|
|
108
|
+
if (rules.indexOf('boolean_name') !== -1) {
|
|
109
|
+
_checkBooleanName(code, lines, errors);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// 检查函数长度
|
|
113
|
+
if (rules.indexOf('long_function') !== -1) {
|
|
114
|
+
_checkLongFunction(lines, errors);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// 检查事件处理函数命名
|
|
118
|
+
if (rules.indexOf('event_handler') !== -1) {
|
|
119
|
+
_checkEventHandler(code, lines, errors);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// 检查错误处理
|
|
123
|
+
if (rules.indexOf('try_catch') !== -1) {
|
|
124
|
+
_checkTryCatch(lines, errors);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// 检查行长度
|
|
128
|
+
if (rules.indexOf('line_length') !== -1) {
|
|
129
|
+
for (var j = 0; j < lines.length; j++) {
|
|
130
|
+
if (lines[j].length > 120) {
|
|
131
|
+
errors.push({
|
|
132
|
+
rule: 'line_length',
|
|
133
|
+
severity: 'warning',
|
|
134
|
+
line: j + 1,
|
|
135
|
+
message: '行长度超过 120 个字符'
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
var valid = errors.length === 0;
|
|
142
|
+
ctx.output = {
|
|
143
|
+
valid: valid,
|
|
144
|
+
tip: valid ? '代码验证通过' : '发现 ' + errors.length + ' 个问题',
|
|
145
|
+
detail: _stringifyErrors(errors),
|
|
146
|
+
errors: errors,
|
|
147
|
+
error_count: errors.length,
|
|
148
|
+
line_count: lines.length
|
|
149
|
+
};
|
|
150
|
+
return ctx.output;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* 将问题列表字符串化,供 LLM 精准修复时反馈
|
|
155
|
+
* @param {Array} errors 问题列表
|
|
156
|
+
* @returns {string} 字符串化的问题详情,无问题时返回空字符串
|
|
157
|
+
*/
|
|
158
|
+
function _stringifyErrors(errors) {
|
|
159
|
+
if (!errors || errors.length === 0) return '';
|
|
160
|
+
var list = [];
|
|
161
|
+
for (var i = 0; i < errors.length; i++) {
|
|
162
|
+
var e = errors[i];
|
|
163
|
+
var loc = e.line > 0 ? '第' + e.line + '行 ' : '';
|
|
164
|
+
list.push(loc + '[' + e.rule + '] ' + e.message);
|
|
165
|
+
}
|
|
166
|
+
return list.join('\n');
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* 检查命名规范
|
|
171
|
+
* @param {string} code 代码内容
|
|
172
|
+
* @param {Array} lines 行数组
|
|
173
|
+
* @param {Array} errors 问题列表
|
|
174
|
+
*/
|
|
175
|
+
function _checkNaming(code, lines, errors) {
|
|
176
|
+
// 函数名:小驼峰(检测蛇形或大写开头)
|
|
177
|
+
var fn_re = /(?:async\s+)?function\s+([a-zA-Z_$][\w$]*)\s*\(/g;
|
|
178
|
+
var fn_match;
|
|
179
|
+
while ((fn_match = fn_re.exec(code)) !== null) {
|
|
180
|
+
var fn_name = fn_match[1];
|
|
181
|
+
if (fn_name.indexOf('_') !== -1 || /^[A-Z]/.test(fn_name)) {
|
|
182
|
+
errors.push({
|
|
183
|
+
rule: 'naming',
|
|
184
|
+
severity: 'warning',
|
|
185
|
+
line: _findLine(lines, fn_match.index),
|
|
186
|
+
message: '函数名 "' + fn_name + '" 应使用小驼峰命名'
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// 变量名:小写蛇形(检测含大写字母)
|
|
192
|
+
var lv_re = /\b(?:let|var)\s+([a-zA-Z_$][\w$]*)\s*=/g;
|
|
193
|
+
var lv_match;
|
|
194
|
+
while ((lv_match = lv_re.exec(code)) !== null) {
|
|
195
|
+
var lv_name = lv_match[1];
|
|
196
|
+
if (/[A-Z]/.test(lv_name)) {
|
|
197
|
+
errors.push({
|
|
198
|
+
rule: 'naming',
|
|
199
|
+
severity: 'warning',
|
|
200
|
+
line: _findLine(lines, lv_match.index),
|
|
201
|
+
message: '变量名 "' + lv_name + '" 应使用小写蛇形命名'
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 类名:大驼峰(检测小写开头或蛇形)
|
|
207
|
+
var cls_re = /class\s+([a-zA-Z_$][\w$]*)/g;
|
|
208
|
+
var cls_match;
|
|
209
|
+
while ((cls_match = cls_re.exec(code)) !== null) {
|
|
210
|
+
var cls_name = cls_match[1];
|
|
211
|
+
if (/^[a-z]/.test(cls_name) || cls_name.indexOf('_') !== -1) {
|
|
212
|
+
errors.push({
|
|
213
|
+
rule: 'naming',
|
|
214
|
+
severity: 'warning',
|
|
215
|
+
line: _findLine(lines, cls_match.index),
|
|
216
|
+
message: '类名 "' + cls_name + '" 应使用大驼峰命名'
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// 常量名:大写蛇形(检测 const 小写开头且赋值为基本字面量)
|
|
222
|
+
var const_re = /const\s+([a-z][\w$]*)\s*=\s*(?:"[^"]*"|'[^']*'|\d+\.?\d*|true|false|null)\b/g;
|
|
223
|
+
var const_match;
|
|
224
|
+
while ((const_match = const_re.exec(code)) !== null) {
|
|
225
|
+
var const_name = const_match[1];
|
|
226
|
+
errors.push({
|
|
227
|
+
rule: 'naming',
|
|
228
|
+
severity: 'warning',
|
|
229
|
+
line: _findLine(lines, const_match.index),
|
|
230
|
+
message: '常量名 "' + const_name + '" 应使用大写蛇形命名'
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* 检查分号
|
|
237
|
+
* @param {Array} lines 行数组
|
|
238
|
+
* @param {Array} errors 问题列表
|
|
239
|
+
*/
|
|
240
|
+
function _checkSemicolon(lines, errors) {
|
|
241
|
+
var cont_re = /[\(\)\,\[\]\.\:\+\-\*\/\=\<\>\!\?\&\|\}]$/;
|
|
242
|
+
var stmt_re = /^(let|const|var)\s+[a-zA-Z_$][\w$]*\s*=|^[a-zA-Z_$][\w$]*(?:\.[a-zA-Z_$][\w$]*)*\s*[=(]/;
|
|
243
|
+
|
|
244
|
+
for (var i = 0; i < lines.length; i++) {
|
|
245
|
+
var line = lines[i].trim();
|
|
246
|
+
if (!line) continue;
|
|
247
|
+
if (line.startsWith('//') || line.startsWith('/*') || line.startsWith('*')) continue;
|
|
248
|
+
if (line.endsWith(';') || line.endsWith('{') || line.endsWith('}')) continue;
|
|
249
|
+
if (cont_re.test(line)) continue;
|
|
250
|
+
|
|
251
|
+
if (stmt_re.test(line) || /^return\s+\S/.test(line) || /^throw\s+\S/.test(line)) {
|
|
252
|
+
errors.push({
|
|
253
|
+
rule: 'semicolon',
|
|
254
|
+
severity: 'warning',
|
|
255
|
+
line: i + 1,
|
|
256
|
+
message: '语句末尾缺少分号'
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* 检查 JSDoc 注释
|
|
264
|
+
* @param {Array} lines 行数组
|
|
265
|
+
* @param {Array} errors 问题列表
|
|
266
|
+
*/
|
|
267
|
+
function _checkJsdoc(lines, errors) {
|
|
268
|
+
var fn_re = /(?:async\s+)?function\s+[a-zA-Z_$][\w$]*\s*\(/;
|
|
269
|
+
for (var i = 0; i < lines.length; i++) {
|
|
270
|
+
var line = lines[i].trim();
|
|
271
|
+
if (!fn_re.test(line)) continue;
|
|
272
|
+
|
|
273
|
+
var has_jsdoc = false;
|
|
274
|
+
for (var j = i - 1; j >= 0; j--) {
|
|
275
|
+
var prev = lines[j].trim();
|
|
276
|
+
if (!prev) continue;
|
|
277
|
+
if (prev.startsWith('/**')) {
|
|
278
|
+
has_jsdoc = true;
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
if (prev.startsWith('*')) continue;
|
|
282
|
+
break;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
if (!has_jsdoc) {
|
|
286
|
+
errors.push({
|
|
287
|
+
rule: 'jsdoc',
|
|
288
|
+
severity: 'warning',
|
|
289
|
+
line: i + 1,
|
|
290
|
+
message: '函数缺少 JSDoc 注释'
|
|
291
|
+
});
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* 检查布尔变量命名
|
|
298
|
+
* @param {string} code 代码内容
|
|
299
|
+
* @param {Array} lines 行数组
|
|
300
|
+
* @param {Array} errors 问题列表
|
|
301
|
+
*/
|
|
302
|
+
function _checkBooleanName(code, lines, errors) {
|
|
303
|
+
var bool_re = /\b(?:let|const|var)\s+([a-zA-Z_$][\w$]*)\s*=\s*(?:true|false)\b/g;
|
|
304
|
+
var bool_match;
|
|
305
|
+
while ((bool_match = bool_re.exec(code)) !== null) {
|
|
306
|
+
var bool_name = bool_match[1];
|
|
307
|
+
if (!/^(is|has|can)([A-Z_]|$)/.test(bool_name)) {
|
|
308
|
+
errors.push({
|
|
309
|
+
rule: 'boolean_name',
|
|
310
|
+
severity: 'warning',
|
|
311
|
+
line: _findLine(lines, bool_match.index),
|
|
312
|
+
message: '布尔变量名 "' + bool_name + '" 应以 is/has/can 开头'
|
|
313
|
+
});
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* 检查函数长度
|
|
320
|
+
* @param {Array} lines 行数组
|
|
321
|
+
* @param {Array} errors 问题列表
|
|
322
|
+
*/
|
|
323
|
+
function _checkLongFunction(lines, errors) {
|
|
324
|
+
var fn_re = /(?:async\s+)?function\s+[a-zA-Z_$][\w$]*\s*\([^)]*\)\s*\{/;
|
|
325
|
+
for (var i = 0; i < lines.length; i++) {
|
|
326
|
+
var line = lines[i].trim();
|
|
327
|
+
if (!fn_re.test(line)) continue;
|
|
328
|
+
|
|
329
|
+
var depth = _countBraces(line);
|
|
330
|
+
var end_idx = i;
|
|
331
|
+
if (depth > 0) {
|
|
332
|
+
for (var j = i + 1; j < lines.length; j++) {
|
|
333
|
+
depth += _countBraces(lines[j]);
|
|
334
|
+
end_idx = j;
|
|
335
|
+
if (depth <= 0) break;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
var fn_lines = end_idx - i + 1;
|
|
340
|
+
if (fn_lines > 50) {
|
|
341
|
+
errors.push({
|
|
342
|
+
rule: 'long_function',
|
|
343
|
+
severity: 'warning',
|
|
344
|
+
line: i + 1,
|
|
345
|
+
message: '函数体 ' + fn_lines + ' 行,超过 50 行限制'
|
|
346
|
+
});
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* 检查事件处理函数命名
|
|
353
|
+
* @param {string} code 代码内容
|
|
354
|
+
* @param {Array} lines 行数组
|
|
355
|
+
* @param {Array} errors 问题列表
|
|
356
|
+
*/
|
|
357
|
+
function _checkEventHandler(code, lines, errors) {
|
|
358
|
+
var add_re = /addEventListener\s*\(\s*['"][^'"]*['"]\s*,\s*([a-zA-Z_$][\w$]*)/g;
|
|
359
|
+
var add_match;
|
|
360
|
+
while ((add_match = add_re.exec(code)) !== null) {
|
|
361
|
+
_checkHandlerName(add_match[1], add_match.index, lines, errors);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
var on_re = /\.on[a-zA-Z]+\s*=\s*([a-zA-Z_$][\w$]*)/g;
|
|
365
|
+
var on_match;
|
|
366
|
+
while ((on_match = on_re.exec(code)) !== null) {
|
|
367
|
+
_checkHandlerName(on_match[1], on_match.index, lines, errors);
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* 检查单个事件处理函数名是否以 handle 开头
|
|
373
|
+
* @param {string} handler 处理函数名
|
|
374
|
+
* @param {number} offset 字符偏移
|
|
375
|
+
* @param {Array} lines 行数组
|
|
376
|
+
* @param {Array} errors 问题列表
|
|
377
|
+
*/
|
|
378
|
+
function _checkHandlerName(handler, offset, lines, errors) {
|
|
379
|
+
if (handler === 'function' || handler === 'async') return;
|
|
380
|
+
if (!/^handle/.test(handler)) {
|
|
381
|
+
errors.push({
|
|
382
|
+
rule: 'event_handler',
|
|
383
|
+
severity: 'warning',
|
|
384
|
+
line: _findLine(lines, offset),
|
|
385
|
+
message: '事件处理函数 "' + handler + '" 应以 handle 开头'
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/**
|
|
391
|
+
* 检查错误处理
|
|
392
|
+
* @param {Array} lines 行数组
|
|
393
|
+
* @param {Array} errors 问题列表
|
|
394
|
+
*/
|
|
395
|
+
function _checkTryCatch(lines, errors) {
|
|
396
|
+
var await_re = /await\s+(?:fetch|axios)/;
|
|
397
|
+
for (var i = 0; i < lines.length; i++) {
|
|
398
|
+
var line = lines[i].trim();
|
|
399
|
+
if (!await_re.test(line)) continue;
|
|
400
|
+
|
|
401
|
+
var in_try = false;
|
|
402
|
+
for (var j = i - 1; j >= 0; j--) {
|
|
403
|
+
var prev = lines[j].trim();
|
|
404
|
+
if (!prev || prev.startsWith('//')) continue;
|
|
405
|
+
if (prev.endsWith('{')) {
|
|
406
|
+
if (prev.startsWith('try')) in_try = true;
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
if (prev.endsWith('}')) break;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (!in_try) {
|
|
413
|
+
errors.push({
|
|
414
|
+
rule: 'try_catch',
|
|
415
|
+
severity: 'warning',
|
|
416
|
+
line: i + 1,
|
|
417
|
+
message: '异步 API 调用未使用 try...catch 包裹'
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* 计算一行中大括号的净增量,跳过字符串内容
|
|
425
|
+
* @param {string} line 行内容
|
|
426
|
+
* @returns {number} 大括号净增量
|
|
427
|
+
*/
|
|
428
|
+
function _countBraces(line) {
|
|
429
|
+
var depth = 0;
|
|
430
|
+
var in_str = null;
|
|
431
|
+
for (var i = 0; i < line.length; i++) {
|
|
432
|
+
var ch = line[i];
|
|
433
|
+
if (in_str) {
|
|
434
|
+
if (ch === in_str && line[i - 1] !== '\\') in_str = null;
|
|
435
|
+
continue;
|
|
436
|
+
}
|
|
437
|
+
if (ch === "'" || ch === '"' || ch === '`') {
|
|
438
|
+
in_str = ch;
|
|
439
|
+
continue;
|
|
440
|
+
}
|
|
441
|
+
if (ch === '{') depth++;
|
|
442
|
+
else if (ch === '}') depth--;
|
|
443
|
+
}
|
|
444
|
+
return depth;
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* 根据字符偏移计算所在行号
|
|
449
|
+
* @param {Array} lines 行数组
|
|
450
|
+
* @param {number} offset 字符偏移
|
|
451
|
+
* @returns {number} 行号(从1开始)
|
|
452
|
+
*/
|
|
453
|
+
function _findLine(lines, offset) {
|
|
454
|
+
var pos = 0;
|
|
455
|
+
for (var i = 0; i < lines.length; i++) {
|
|
456
|
+
pos += lines[i].length + 1;
|
|
457
|
+
if (pos > offset) return i + 1;
|
|
458
|
+
}
|
|
459
|
+
return 1;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
module.exports = { main };
|
package/tool/enum/index.js
CHANGED
|
@@ -1,7 +1,22 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
* 其它模块可直接 require('mm_validator').tools.enum 复用。
|
|
2
|
+
* 枚举验证器
|
|
3
|
+
* 验证值是否在允许的枚举列表中
|
|
6
4
|
*/
|
|
7
|
-
|
|
5
|
+
|
|
6
|
+
async function main(params, ctx, options) {
|
|
7
|
+
var errors = [];
|
|
8
|
+
var value = params.value;
|
|
9
|
+
var options = params.options || [];
|
|
10
|
+
|
|
11
|
+
if (!Array.isArray(options)) {
|
|
12
|
+
errors.push('options 必须是数组');
|
|
13
|
+
} else if (options.indexOf(value) === -1) {
|
|
14
|
+
errors.push(params.field + ' 的值不在允许范围内: ' + options.join(', '));
|
|
15
|
+
}
|
|
16
|
+
var valid = errors.length === 0;
|
|
17
|
+
var tip = valid ? '参数校验通过' : errors.length + '个参数校验失败: ' + errors.join('; ');
|
|
18
|
+
ctx.output = { valid: valid, tip: tip, errors: errors, allowed_values: options };
|
|
19
|
+
return ctx.output;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = { main };
|
package/tool/json/index.js
CHANGED
|
@@ -1,7 +1,181 @@
|
|
|
1
|
-
'use strict';
|
|
2
1
|
/**
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* JSON 格式验证器
|
|
3
|
+
* 验证 JSON 语法和 work.json 字段规范,返回问题列表
|
|
4
|
+
* @param {object} ctx 上下文对象
|
|
5
|
+
* @param {object} params 步骤参数
|
|
6
6
|
*/
|
|
7
|
-
|
|
7
|
+
async function main(params, ctx, options) {
|
|
8
|
+
var code = params.code || '';
|
|
9
|
+
var rules =
|
|
10
|
+
params.rules ||
|
|
11
|
+
[
|
|
12
|
+
'syntax',
|
|
13
|
+
'required_fields',
|
|
14
|
+
'name_format',
|
|
15
|
+
'version_format',
|
|
16
|
+
'description_length'
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
if (!code) {
|
|
20
|
+
ctx.output = {
|
|
21
|
+
error: { code: 'VAL_JSON_PARAM', message: '缺少JSON内容(code)' }
|
|
22
|
+
};
|
|
23
|
+
return ctx.output;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
var errors = [];
|
|
27
|
+
var lines = code.split(/\r?\n/);
|
|
28
|
+
var obj = null;
|
|
29
|
+
|
|
30
|
+
// 语法检查
|
|
31
|
+
if (rules.indexOf('syntax') !== -1) {
|
|
32
|
+
try {
|
|
33
|
+
obj = JSON.parse(code);
|
|
34
|
+
} catch (e) {
|
|
35
|
+
errors.push({
|
|
36
|
+
rule: 'syntax',
|
|
37
|
+
severity: 'error',
|
|
38
|
+
line: _errorLine(code, e.message),
|
|
39
|
+
message: 'JSON语法错误: ' + e.message
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// 字段级检查仅在语法通过且为对象时进行
|
|
45
|
+
if (obj && typeof obj === 'object' && !Array.isArray(obj)) {
|
|
46
|
+
if (rules.indexOf('required_fields') !== -1) {
|
|
47
|
+
_checkRequired(obj, errors);
|
|
48
|
+
}
|
|
49
|
+
if (rules.indexOf('name_format') !== -1) {
|
|
50
|
+
_checkNameFormat(obj, errors);
|
|
51
|
+
}
|
|
52
|
+
if (rules.indexOf('version_format') !== -1) {
|
|
53
|
+
_checkVersionFormat(obj, errors);
|
|
54
|
+
}
|
|
55
|
+
if (rules.indexOf('description_length') !== -1) {
|
|
56
|
+
_checkDescriptionLength(obj, errors);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
var valid = errors.length === 0;
|
|
61
|
+
ctx.output = {
|
|
62
|
+
valid: valid,
|
|
63
|
+
tip: valid ? 'JSON验证通过' : '发现 ' + errors.length + ' 个问题',
|
|
64
|
+
detail: _stringifyErrors(errors),
|
|
65
|
+
errors: errors,
|
|
66
|
+
error_count: errors.length,
|
|
67
|
+
line_count: lines.length
|
|
68
|
+
};
|
|
69
|
+
return ctx.output;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 将问题列表字符串化,供 LLM 精准修复时反馈
|
|
74
|
+
* @param {Array} errors 问题列表
|
|
75
|
+
* @returns {string} 字符串化的问题详情,无问题时返回空字符串
|
|
76
|
+
*/
|
|
77
|
+
function _stringifyErrors(errors) {
|
|
78
|
+
if (!errors || errors.length === 0) return '';
|
|
79
|
+
var list = [];
|
|
80
|
+
for (var i = 0; i < errors.length; i++) {
|
|
81
|
+
var e = errors[i];
|
|
82
|
+
var loc = e.line > 0 ? '第' + e.line + '行 ' : '';
|
|
83
|
+
list.push(loc + '[' + e.rule + '] ' + e.message);
|
|
84
|
+
}
|
|
85
|
+
return list.join('\n');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* 从 JSON.parse 错误信息中提取 position 并转换为行号
|
|
90
|
+
* @param {string} code JSON内容
|
|
91
|
+
* @param {string} message 错误信息
|
|
92
|
+
* @returns {number} 行号(从1开始),无法定位时返回0
|
|
93
|
+
*/
|
|
94
|
+
function _errorLine(code, message) {
|
|
95
|
+
var match = message.match(/position\s+(\d+)/i);
|
|
96
|
+
if (!match) return 0;
|
|
97
|
+
var pos = parseInt(match[1], 10);
|
|
98
|
+
if (isNaN(pos) || pos < 0) return 0;
|
|
99
|
+
return code.slice(0, pos).split('\n').length;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* 检查必填字段
|
|
104
|
+
* @param {object} obj JSON对象
|
|
105
|
+
* @param {Array} errors 问题列表
|
|
106
|
+
*/
|
|
107
|
+
function _checkRequired(obj, errors) {
|
|
108
|
+
var required_fields = ['name', 'title', 'description', 'version', 'group'];
|
|
109
|
+
for (var i = 0; i < required_fields.length; i++) {
|
|
110
|
+
var field = required_fields[i];
|
|
111
|
+
if (obj[field] === undefined || obj[field] === null || obj[field] === '') {
|
|
112
|
+
errors.push({
|
|
113
|
+
rule: 'required_fields',
|
|
114
|
+
severity: 'error',
|
|
115
|
+
line: 0,
|
|
116
|
+
message: '缺少必填字段: ' + field
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* 检查 name 和 group 字段格式
|
|
124
|
+
* @param {object} obj JSON对象
|
|
125
|
+
* @param {Array} errors 问题列表
|
|
126
|
+
*/
|
|
127
|
+
function _checkNameFormat(obj, errors) {
|
|
128
|
+
var name_re = /^[a-z_]+$/;
|
|
129
|
+
var fields = ['name', 'group'];
|
|
130
|
+
for (var i = 0; i < fields.length; i++) {
|
|
131
|
+
var field = fields[i];
|
|
132
|
+
var val = obj[field];
|
|
133
|
+
if (typeof val !== 'string' || val === '') continue;
|
|
134
|
+
if (!name_re.test(val)) {
|
|
135
|
+
errors.push({
|
|
136
|
+
rule: 'name_format',
|
|
137
|
+
severity: 'warning',
|
|
138
|
+
line: 0,
|
|
139
|
+
message: '字段 "' + field + '" 的值 "' + val + '" 应仅包含英文小写和下划线'
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* 检查 version 字段格式
|
|
147
|
+
* @param {object} obj JSON对象
|
|
148
|
+
* @param {Array} errors 问题列表
|
|
149
|
+
*/
|
|
150
|
+
function _checkVersionFormat(obj, errors) {
|
|
151
|
+
var ver = obj.version;
|
|
152
|
+
if (typeof ver !== 'string' || ver === '') return;
|
|
153
|
+
if (!/^\d+\.\d+\.\d+$/.test(ver)) {
|
|
154
|
+
errors.push({
|
|
155
|
+
rule: 'version_format',
|
|
156
|
+
severity: 'warning',
|
|
157
|
+
line: 0,
|
|
158
|
+
message: '字段 "version" 的值 "' + ver + '" 不符合 x.x.x 格式'
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* 检查 description 字段长度
|
|
165
|
+
* @param {object} obj JSON对象
|
|
166
|
+
* @param {Array} errors 问题列表
|
|
167
|
+
*/
|
|
168
|
+
function _checkDescriptionLength(obj, errors) {
|
|
169
|
+
var desc = obj.description;
|
|
170
|
+
if (typeof desc !== 'string' || desc === '') return;
|
|
171
|
+
if (desc.length > 50) {
|
|
172
|
+
errors.push({
|
|
173
|
+
rule: 'description_length',
|
|
174
|
+
severity: 'warning',
|
|
175
|
+
line: 0,
|
|
176
|
+
message: '字段 "description" 长度 ' + desc.length + ' 超过 50 字限制'
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
module.exports = { main };
|