@aiready/consistency 0.3.3 → 0.3.4
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/dist/chunk-Q5XMWG33.mjs +661 -0
- package/dist/chunk-TLVLM3M5.mjs +771 -0
- package/dist/cli.js +277 -14
- package/dist/cli.mjs +1 -1
- package/dist/index.js +277 -14
- package/dist/index.mjs +1 -1
- package/package.json +13 -12
- package/src/__tests__/analyzer.test.ts +14 -1
- package/src/analyzers/naming.ts +137 -23
- package/.turbo/turbo-build.log +0 -24
- package/.turbo/turbo-test.log +0 -76
|
@@ -0,0 +1,771 @@
|
|
|
1
|
+
// src/analyzers/naming.ts
|
|
2
|
+
import { readFileContent } from "@aiready/core";
|
|
3
|
+
var COMMON_SHORT_WORDS = /* @__PURE__ */ new Set([
|
|
4
|
+
// Full English words (1-3 letters)
|
|
5
|
+
"day",
|
|
6
|
+
"key",
|
|
7
|
+
"net",
|
|
8
|
+
"to",
|
|
9
|
+
"go",
|
|
10
|
+
"for",
|
|
11
|
+
"not",
|
|
12
|
+
"new",
|
|
13
|
+
"old",
|
|
14
|
+
"top",
|
|
15
|
+
"end",
|
|
16
|
+
"run",
|
|
17
|
+
"try",
|
|
18
|
+
"use",
|
|
19
|
+
"get",
|
|
20
|
+
"set",
|
|
21
|
+
"add",
|
|
22
|
+
"put",
|
|
23
|
+
"map",
|
|
24
|
+
"log",
|
|
25
|
+
"row",
|
|
26
|
+
"col",
|
|
27
|
+
"tab",
|
|
28
|
+
"box",
|
|
29
|
+
"div",
|
|
30
|
+
"nav",
|
|
31
|
+
"tag",
|
|
32
|
+
"any",
|
|
33
|
+
"all",
|
|
34
|
+
"one",
|
|
35
|
+
"two",
|
|
36
|
+
"out",
|
|
37
|
+
"off",
|
|
38
|
+
"on",
|
|
39
|
+
"yes",
|
|
40
|
+
"no",
|
|
41
|
+
"now",
|
|
42
|
+
"max",
|
|
43
|
+
"min",
|
|
44
|
+
"sum",
|
|
45
|
+
"avg",
|
|
46
|
+
"ref",
|
|
47
|
+
"src",
|
|
48
|
+
"dst",
|
|
49
|
+
"raw",
|
|
50
|
+
"def",
|
|
51
|
+
"sub",
|
|
52
|
+
"pub",
|
|
53
|
+
"pre",
|
|
54
|
+
"mid",
|
|
55
|
+
"alt",
|
|
56
|
+
"opt",
|
|
57
|
+
"tmp",
|
|
58
|
+
"ext",
|
|
59
|
+
"sep",
|
|
60
|
+
// Additional full words commonly flagged
|
|
61
|
+
"tax",
|
|
62
|
+
"cat",
|
|
63
|
+
"dog",
|
|
64
|
+
"car",
|
|
65
|
+
"bus",
|
|
66
|
+
"web",
|
|
67
|
+
"app",
|
|
68
|
+
"war",
|
|
69
|
+
"law",
|
|
70
|
+
"pay",
|
|
71
|
+
"buy",
|
|
72
|
+
"win",
|
|
73
|
+
"cut",
|
|
74
|
+
"hit",
|
|
75
|
+
"hot",
|
|
76
|
+
"pop",
|
|
77
|
+
"job",
|
|
78
|
+
"age",
|
|
79
|
+
"act",
|
|
80
|
+
"let",
|
|
81
|
+
"lot",
|
|
82
|
+
"bad",
|
|
83
|
+
"big",
|
|
84
|
+
"far",
|
|
85
|
+
"few",
|
|
86
|
+
"own",
|
|
87
|
+
"per",
|
|
88
|
+
"red",
|
|
89
|
+
"low",
|
|
90
|
+
"see",
|
|
91
|
+
"six",
|
|
92
|
+
"ten",
|
|
93
|
+
"way",
|
|
94
|
+
"who",
|
|
95
|
+
"why",
|
|
96
|
+
"yet",
|
|
97
|
+
"via",
|
|
98
|
+
"due",
|
|
99
|
+
"fee",
|
|
100
|
+
"fun",
|
|
101
|
+
"gas",
|
|
102
|
+
"gay",
|
|
103
|
+
"god",
|
|
104
|
+
"gun",
|
|
105
|
+
"guy",
|
|
106
|
+
"ice",
|
|
107
|
+
"ill",
|
|
108
|
+
"kid",
|
|
109
|
+
"mad",
|
|
110
|
+
"man",
|
|
111
|
+
"mix",
|
|
112
|
+
"mom",
|
|
113
|
+
"mrs",
|
|
114
|
+
"nor",
|
|
115
|
+
"odd",
|
|
116
|
+
"oil",
|
|
117
|
+
"pan",
|
|
118
|
+
"pet",
|
|
119
|
+
"pit",
|
|
120
|
+
"pot",
|
|
121
|
+
"pow",
|
|
122
|
+
"pro",
|
|
123
|
+
"raw",
|
|
124
|
+
"rep",
|
|
125
|
+
"rid",
|
|
126
|
+
"sad",
|
|
127
|
+
"sea",
|
|
128
|
+
"sit",
|
|
129
|
+
"sky",
|
|
130
|
+
"son",
|
|
131
|
+
"tea",
|
|
132
|
+
"tie",
|
|
133
|
+
"tip",
|
|
134
|
+
"van",
|
|
135
|
+
"war",
|
|
136
|
+
"win",
|
|
137
|
+
"won"
|
|
138
|
+
]);
|
|
139
|
+
var ACCEPTABLE_ABBREVIATIONS = /* @__PURE__ */ new Set([
|
|
140
|
+
// Standard identifiers
|
|
141
|
+
"id",
|
|
142
|
+
"uid",
|
|
143
|
+
"gid",
|
|
144
|
+
"pid",
|
|
145
|
+
// Loop counters and iterators
|
|
146
|
+
"i",
|
|
147
|
+
"j",
|
|
148
|
+
"k",
|
|
149
|
+
"n",
|
|
150
|
+
"m",
|
|
151
|
+
// Web/Network
|
|
152
|
+
"url",
|
|
153
|
+
"uri",
|
|
154
|
+
"api",
|
|
155
|
+
"cdn",
|
|
156
|
+
"dns",
|
|
157
|
+
"ip",
|
|
158
|
+
"tcp",
|
|
159
|
+
"udp",
|
|
160
|
+
"http",
|
|
161
|
+
"ssl",
|
|
162
|
+
"tls",
|
|
163
|
+
"utm",
|
|
164
|
+
"seo",
|
|
165
|
+
"rss",
|
|
166
|
+
"xhr",
|
|
167
|
+
"ajax",
|
|
168
|
+
"cors",
|
|
169
|
+
"ws",
|
|
170
|
+
"wss",
|
|
171
|
+
// Data formats
|
|
172
|
+
"json",
|
|
173
|
+
"xml",
|
|
174
|
+
"yaml",
|
|
175
|
+
"csv",
|
|
176
|
+
"html",
|
|
177
|
+
"css",
|
|
178
|
+
"svg",
|
|
179
|
+
"pdf",
|
|
180
|
+
// File types & extensions
|
|
181
|
+
"img",
|
|
182
|
+
"txt",
|
|
183
|
+
"doc",
|
|
184
|
+
"docx",
|
|
185
|
+
"xlsx",
|
|
186
|
+
"ppt",
|
|
187
|
+
"md",
|
|
188
|
+
"rst",
|
|
189
|
+
"jpg",
|
|
190
|
+
"png",
|
|
191
|
+
"gif",
|
|
192
|
+
// Databases
|
|
193
|
+
"db",
|
|
194
|
+
"sql",
|
|
195
|
+
"orm",
|
|
196
|
+
"dao",
|
|
197
|
+
"dto",
|
|
198
|
+
"ddb",
|
|
199
|
+
"rds",
|
|
200
|
+
"nosql",
|
|
201
|
+
// File system
|
|
202
|
+
"fs",
|
|
203
|
+
"dir",
|
|
204
|
+
"tmp",
|
|
205
|
+
"src",
|
|
206
|
+
"dst",
|
|
207
|
+
"bin",
|
|
208
|
+
"lib",
|
|
209
|
+
"pkg",
|
|
210
|
+
// Operating system
|
|
211
|
+
"os",
|
|
212
|
+
"env",
|
|
213
|
+
"arg",
|
|
214
|
+
"cli",
|
|
215
|
+
"cmd",
|
|
216
|
+
"exe",
|
|
217
|
+
"cwd",
|
|
218
|
+
"pwd",
|
|
219
|
+
// UI/UX
|
|
220
|
+
"ui",
|
|
221
|
+
"ux",
|
|
222
|
+
"gui",
|
|
223
|
+
"dom",
|
|
224
|
+
"ref",
|
|
225
|
+
// Request/Response
|
|
226
|
+
"req",
|
|
227
|
+
"res",
|
|
228
|
+
"ctx",
|
|
229
|
+
"err",
|
|
230
|
+
"msg",
|
|
231
|
+
"auth",
|
|
232
|
+
// Mathematics/Computing
|
|
233
|
+
"max",
|
|
234
|
+
"min",
|
|
235
|
+
"avg",
|
|
236
|
+
"sum",
|
|
237
|
+
"abs",
|
|
238
|
+
"cos",
|
|
239
|
+
"sin",
|
|
240
|
+
"tan",
|
|
241
|
+
"log",
|
|
242
|
+
"exp",
|
|
243
|
+
"pow",
|
|
244
|
+
"sqrt",
|
|
245
|
+
"std",
|
|
246
|
+
"var",
|
|
247
|
+
"int",
|
|
248
|
+
"num",
|
|
249
|
+
"idx",
|
|
250
|
+
// Time
|
|
251
|
+
"now",
|
|
252
|
+
"utc",
|
|
253
|
+
"tz",
|
|
254
|
+
"ms",
|
|
255
|
+
"sec",
|
|
256
|
+
"hr",
|
|
257
|
+
"min",
|
|
258
|
+
"yr",
|
|
259
|
+
"mo",
|
|
260
|
+
// Common patterns
|
|
261
|
+
"app",
|
|
262
|
+
"cfg",
|
|
263
|
+
"config",
|
|
264
|
+
"init",
|
|
265
|
+
"len",
|
|
266
|
+
"val",
|
|
267
|
+
"str",
|
|
268
|
+
"obj",
|
|
269
|
+
"arr",
|
|
270
|
+
"gen",
|
|
271
|
+
"def",
|
|
272
|
+
"raw",
|
|
273
|
+
"new",
|
|
274
|
+
"old",
|
|
275
|
+
"pre",
|
|
276
|
+
"post",
|
|
277
|
+
"sub",
|
|
278
|
+
"pub",
|
|
279
|
+
// Programming/Framework specific
|
|
280
|
+
"ts",
|
|
281
|
+
"js",
|
|
282
|
+
"jsx",
|
|
283
|
+
"tsx",
|
|
284
|
+
"py",
|
|
285
|
+
"rb",
|
|
286
|
+
"vue",
|
|
287
|
+
"re",
|
|
288
|
+
"fn",
|
|
289
|
+
"fns",
|
|
290
|
+
"mod",
|
|
291
|
+
"opts",
|
|
292
|
+
"dev",
|
|
293
|
+
// Cloud/Infrastructure
|
|
294
|
+
"s3",
|
|
295
|
+
"ec2",
|
|
296
|
+
"sqs",
|
|
297
|
+
"sns",
|
|
298
|
+
"vpc",
|
|
299
|
+
"ami",
|
|
300
|
+
"iam",
|
|
301
|
+
"acl",
|
|
302
|
+
"elb",
|
|
303
|
+
"alb",
|
|
304
|
+
"nlb",
|
|
305
|
+
"aws",
|
|
306
|
+
// Metrics/Performance
|
|
307
|
+
"fcp",
|
|
308
|
+
"lcp",
|
|
309
|
+
"cls",
|
|
310
|
+
"ttfb",
|
|
311
|
+
"tti",
|
|
312
|
+
"fid",
|
|
313
|
+
"fps",
|
|
314
|
+
"qps",
|
|
315
|
+
"rps",
|
|
316
|
+
"tps",
|
|
317
|
+
// Testing & i18n
|
|
318
|
+
"po",
|
|
319
|
+
"e2e",
|
|
320
|
+
"a11y",
|
|
321
|
+
"i18n",
|
|
322
|
+
"l10n",
|
|
323
|
+
// Domain-specific abbreviations (context-aware)
|
|
324
|
+
"sk",
|
|
325
|
+
"fy",
|
|
326
|
+
"faq",
|
|
327
|
+
"og",
|
|
328
|
+
"seo",
|
|
329
|
+
"cta",
|
|
330
|
+
"roi",
|
|
331
|
+
"kpi",
|
|
332
|
+
// Boolean helpers (these are intentional short names)
|
|
333
|
+
"is",
|
|
334
|
+
"has",
|
|
335
|
+
"can",
|
|
336
|
+
"did",
|
|
337
|
+
"was",
|
|
338
|
+
"are",
|
|
339
|
+
// Date/Time context (when in date contexts)
|
|
340
|
+
"d",
|
|
341
|
+
"t",
|
|
342
|
+
"dt"
|
|
343
|
+
]);
|
|
344
|
+
async function analyzeNaming(files) {
|
|
345
|
+
const issues = [];
|
|
346
|
+
for (const file of files) {
|
|
347
|
+
const content = await readFileContent(file);
|
|
348
|
+
const fileIssues = analyzeFileNaming(file, content);
|
|
349
|
+
issues.push(...fileIssues);
|
|
350
|
+
}
|
|
351
|
+
return issues;
|
|
352
|
+
}
|
|
353
|
+
function analyzeFileNaming(file, content) {
|
|
354
|
+
const issues = [];
|
|
355
|
+
const isTestFile = file.match(/\.(test|spec)\.(ts|tsx|js|jsx)$/);
|
|
356
|
+
const lines = content.split("\n");
|
|
357
|
+
lines.forEach((line, index) => {
|
|
358
|
+
const lineNumber = index + 1;
|
|
359
|
+
const singleLetterMatches = line.matchAll(/\b(?:const|let|var)\s+([a-hm-z])\s*=/gi);
|
|
360
|
+
for (const match of singleLetterMatches) {
|
|
361
|
+
const letter = match[1].toLowerCase();
|
|
362
|
+
const isInLoopContext = line.includes("for") || /\.(map|filter|forEach|reduce|find|some|every)\s*\(/.test(line) || line.includes("=>") || // Arrow function
|
|
363
|
+
/\w+\s*=>\s*/.test(line);
|
|
364
|
+
const isI18nContext = line.includes("useTranslation") || line.includes("i18n.t") || /\bt\s*\(['"]/.test(line);
|
|
365
|
+
const isArrowFunctionParam = /\(\s*[a-z]\s*(?:,\s*[a-z]\s*)*\)\s*=>/.test(line) || // (s) => or (a, b) =>
|
|
366
|
+
/[a-z]\s*=>/.test(line);
|
|
367
|
+
if (!isInLoopContext && !isI18nContext && !isArrowFunctionParam && !["x", "y", "z", "i", "j", "k", "l", "n", "m"].includes(letter)) {
|
|
368
|
+
if (isTestFile && ["a", "b", "c", "d", "e", "f", "s"].includes(letter)) {
|
|
369
|
+
continue;
|
|
370
|
+
}
|
|
371
|
+
issues.push({
|
|
372
|
+
file,
|
|
373
|
+
line: lineNumber,
|
|
374
|
+
type: "poor-naming",
|
|
375
|
+
identifier: match[1],
|
|
376
|
+
severity: "minor",
|
|
377
|
+
suggestion: `Use descriptive variable name instead of single letter '${match[1]}'`
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
const abbreviationMatches = line.matchAll(/\b(?:const|let|var)\s+([a-z]{1,3})(?=[A-Z]|_|\s*=)/g);
|
|
382
|
+
for (const match of abbreviationMatches) {
|
|
383
|
+
const abbrev = match[1].toLowerCase();
|
|
384
|
+
if (COMMON_SHORT_WORDS.has(abbrev)) {
|
|
385
|
+
continue;
|
|
386
|
+
}
|
|
387
|
+
if (ACCEPTABLE_ABBREVIATIONS.has(abbrev)) {
|
|
388
|
+
continue;
|
|
389
|
+
}
|
|
390
|
+
const isArrowFunctionParam = /\(\s*[a-z]\s*(?:,\s*[a-z]\s*)*\)\s*=>/.test(line) || // (s) => or (a, b) =>
|
|
391
|
+
new RegExp(`\\b${abbrev}\\s*=>`).test(line);
|
|
392
|
+
if (isArrowFunctionParam) {
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
if (abbrev.length <= 2) {
|
|
396
|
+
const isDateTimeContext = /date|time|day|hour|minute|second|timestamp/i.test(line);
|
|
397
|
+
if (isDateTimeContext && ["d", "t", "dt"].includes(abbrev)) {
|
|
398
|
+
continue;
|
|
399
|
+
}
|
|
400
|
+
const isUserContext = /user|auth|account/i.test(line);
|
|
401
|
+
if (isUserContext && abbrev === "u") {
|
|
402
|
+
continue;
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
issues.push({
|
|
406
|
+
file,
|
|
407
|
+
line: lineNumber,
|
|
408
|
+
type: "abbreviation",
|
|
409
|
+
identifier: match[1],
|
|
410
|
+
severity: "info",
|
|
411
|
+
suggestion: `Consider using full word instead of abbreviation '${match[1]}'`
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
if (file.match(/\.(ts|tsx|js|jsx)$/)) {
|
|
415
|
+
const camelCaseVars = line.match(/\b(?:const|let|var)\s+([a-z][a-zA-Z0-9]*)\s*=/);
|
|
416
|
+
const snakeCaseVars = line.match(/\b(?:const|let|var)\s+([a-z][a-z0-9]*_[a-z0-9_]*)\s*=/);
|
|
417
|
+
if (snakeCaseVars) {
|
|
418
|
+
issues.push({
|
|
419
|
+
file,
|
|
420
|
+
line: lineNumber,
|
|
421
|
+
type: "convention-mix",
|
|
422
|
+
identifier: snakeCaseVars[1],
|
|
423
|
+
severity: "minor",
|
|
424
|
+
suggestion: `Use camelCase '${snakeCaseToCamelCase(snakeCaseVars[1])}' instead of snake_case in TypeScript/JavaScript`
|
|
425
|
+
});
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
const booleanMatches = line.matchAll(/\b(?:const|let|var)\s+([a-z][a-zA-Z0-9]*)\s*:\s*boolean/gi);
|
|
429
|
+
for (const match of booleanMatches) {
|
|
430
|
+
const name = match[1];
|
|
431
|
+
if (!name.match(/^(is|has|should|can|will|did)/i)) {
|
|
432
|
+
issues.push({
|
|
433
|
+
file,
|
|
434
|
+
line: lineNumber,
|
|
435
|
+
type: "unclear",
|
|
436
|
+
identifier: name,
|
|
437
|
+
severity: "info",
|
|
438
|
+
suggestion: `Boolean variable '${name}' should start with is/has/should/can for clarity`
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
const functionMatches = line.matchAll(/function\s+([a-z][a-zA-Z0-9]*)/g);
|
|
443
|
+
for (const match of functionMatches) {
|
|
444
|
+
const name = match[1];
|
|
445
|
+
const isKeyword = ["for", "if", "else", "while", "do", "switch", "case", "break", "continue", "return", "throw", "try", "catch", "finally", "with", "yield", "await"].includes(name);
|
|
446
|
+
if (isKeyword) {
|
|
447
|
+
continue;
|
|
448
|
+
}
|
|
449
|
+
const isEntryPoint = ["main", "init", "setup", "bootstrap"].includes(name);
|
|
450
|
+
if (isEntryPoint) {
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const isFactoryPattern = name.match(/(Factory|Builder|Creator|Generator)$/);
|
|
454
|
+
const isEventHandler = name.match(/^on[A-Z]/);
|
|
455
|
+
const isDescriptiveLong = name.length > 15;
|
|
456
|
+
const isDescriptivePattern = name.match(/^(default|total|count|sum|avg|max|min|initial|current|previous|next)\w+/) || name.match(/\w+(Count|Total|Sum|Average|List|Map|Set|Config|Settings|Options|Props)$/);
|
|
457
|
+
const capitalCount = (name.match(/[A-Z]/g) || []).length;
|
|
458
|
+
const isCompoundWord = capitalCount >= 3;
|
|
459
|
+
const hasActionVerb = name.match(/^(get|set|is|has|can|should|create|update|delete|fetch|load|save|process|handle|validate|check|find|search|filter|map|reduce|make|do|run|start|stop|build|parse|format|render|calculate|compute|generate|transform|convert|normalize|sanitize|encode|decode|compress|extract|merge|split|join|sort|compare|test|verify|ensure|apply|execute|invoke|call|emit|dispatch|trigger|listen|subscribe|unsubscribe|add|remove|clear|reset|toggle|enable|disable|open|close|connect|disconnect|send|receive|read|write|import|export|register|unregister|mount|unmount)/);
|
|
460
|
+
if (!hasActionVerb && !isFactoryPattern && !isEventHandler && !isDescriptiveLong && !isDescriptivePattern && !isCompoundWord) {
|
|
461
|
+
issues.push({
|
|
462
|
+
file,
|
|
463
|
+
line: lineNumber,
|
|
464
|
+
type: "unclear",
|
|
465
|
+
identifier: name,
|
|
466
|
+
severity: "info",
|
|
467
|
+
suggestion: `Function '${name}' should start with an action verb (get, set, create, etc.)`
|
|
468
|
+
});
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
});
|
|
472
|
+
return issues;
|
|
473
|
+
}
|
|
474
|
+
function snakeCaseToCamelCase(str) {
|
|
475
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
476
|
+
}
|
|
477
|
+
function detectNamingConventions(files, allIssues) {
|
|
478
|
+
const camelCaseCount = allIssues.filter((i) => i.type === "convention-mix").length;
|
|
479
|
+
const totalChecks = files.length * 10;
|
|
480
|
+
if (camelCaseCount / totalChecks > 0.3) {
|
|
481
|
+
return { dominantConvention: "mixed", conventionScore: 0.5 };
|
|
482
|
+
}
|
|
483
|
+
return { dominantConvention: "camelCase", conventionScore: 0.9 };
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// src/analyzers/patterns.ts
|
|
487
|
+
import { readFileContent as readFileContent2 } from "@aiready/core";
|
|
488
|
+
async function analyzePatterns(files) {
|
|
489
|
+
const issues = [];
|
|
490
|
+
const errorHandlingIssues = await analyzeErrorHandling(files);
|
|
491
|
+
issues.push(...errorHandlingIssues);
|
|
492
|
+
const asyncIssues = await analyzeAsyncPatterns(files);
|
|
493
|
+
issues.push(...asyncIssues);
|
|
494
|
+
const importIssues = await analyzeImportStyles(files);
|
|
495
|
+
issues.push(...importIssues);
|
|
496
|
+
return issues;
|
|
497
|
+
}
|
|
498
|
+
async function analyzeErrorHandling(files) {
|
|
499
|
+
const patterns = {
|
|
500
|
+
tryCatch: [],
|
|
501
|
+
throwsError: [],
|
|
502
|
+
returnsNull: [],
|
|
503
|
+
returnsError: []
|
|
504
|
+
};
|
|
505
|
+
for (const file of files) {
|
|
506
|
+
const content = await readFileContent2(file);
|
|
507
|
+
if (content.includes("try {") || content.includes("} catch")) {
|
|
508
|
+
patterns.tryCatch.push(file);
|
|
509
|
+
}
|
|
510
|
+
if (content.match(/throw new \w*Error/)) {
|
|
511
|
+
patterns.throwsError.push(file);
|
|
512
|
+
}
|
|
513
|
+
if (content.match(/return null/)) {
|
|
514
|
+
patterns.returnsNull.push(file);
|
|
515
|
+
}
|
|
516
|
+
if (content.match(/return \{ error:/)) {
|
|
517
|
+
patterns.returnsError.push(file);
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
const issues = [];
|
|
521
|
+
const strategiesUsed = Object.values(patterns).filter((p) => p.length > 0).length;
|
|
522
|
+
if (strategiesUsed > 2) {
|
|
523
|
+
issues.push({
|
|
524
|
+
files: [.../* @__PURE__ */ new Set([
|
|
525
|
+
...patterns.tryCatch,
|
|
526
|
+
...patterns.throwsError,
|
|
527
|
+
...patterns.returnsNull,
|
|
528
|
+
...patterns.returnsError
|
|
529
|
+
])],
|
|
530
|
+
type: "error-handling",
|
|
531
|
+
description: "Inconsistent error handling strategies across codebase",
|
|
532
|
+
examples: [
|
|
533
|
+
patterns.tryCatch.length > 0 ? `Try-catch used in ${patterns.tryCatch.length} files` : "",
|
|
534
|
+
patterns.throwsError.length > 0 ? `Throws errors in ${patterns.throwsError.length} files` : "",
|
|
535
|
+
patterns.returnsNull.length > 0 ? `Returns null in ${patterns.returnsNull.length} files` : "",
|
|
536
|
+
patterns.returnsError.length > 0 ? `Returns error objects in ${patterns.returnsError.length} files` : ""
|
|
537
|
+
].filter((e) => e),
|
|
538
|
+
severity: "major"
|
|
539
|
+
});
|
|
540
|
+
}
|
|
541
|
+
return issues;
|
|
542
|
+
}
|
|
543
|
+
async function analyzeAsyncPatterns(files) {
|
|
544
|
+
const patterns = {
|
|
545
|
+
asyncAwait: [],
|
|
546
|
+
promises: [],
|
|
547
|
+
callbacks: []
|
|
548
|
+
};
|
|
549
|
+
for (const file of files) {
|
|
550
|
+
const content = await readFileContent2(file);
|
|
551
|
+
if (content.match(/async\s+(function|\(|[a-zA-Z])/)) {
|
|
552
|
+
patterns.asyncAwait.push(file);
|
|
553
|
+
}
|
|
554
|
+
if (content.match(/\.then\(/) || content.match(/\.catch\(/)) {
|
|
555
|
+
patterns.promises.push(file);
|
|
556
|
+
}
|
|
557
|
+
if (content.match(/callback\s*\(/) || content.match(/\(\s*err\s*,/)) {
|
|
558
|
+
patterns.callbacks.push(file);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
const issues = [];
|
|
562
|
+
if (patterns.callbacks.length > 0 && patterns.asyncAwait.length > 0) {
|
|
563
|
+
issues.push({
|
|
564
|
+
files: [...patterns.callbacks, ...patterns.asyncAwait],
|
|
565
|
+
type: "async-style",
|
|
566
|
+
description: "Mixed async patterns: callbacks and async/await",
|
|
567
|
+
examples: [
|
|
568
|
+
`Callbacks found in: ${patterns.callbacks.slice(0, 3).join(", ")}`,
|
|
569
|
+
`Async/await used in: ${patterns.asyncAwait.slice(0, 3).join(", ")}`
|
|
570
|
+
],
|
|
571
|
+
severity: "minor"
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
if (patterns.promises.length > patterns.asyncAwait.length * 0.3 && patterns.asyncAwait.length > 0) {
|
|
575
|
+
issues.push({
|
|
576
|
+
files: patterns.promises,
|
|
577
|
+
type: "async-style",
|
|
578
|
+
description: "Consider using async/await instead of promise chains for consistency",
|
|
579
|
+
examples: patterns.promises.slice(0, 5),
|
|
580
|
+
severity: "info"
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
return issues;
|
|
584
|
+
}
|
|
585
|
+
async function analyzeImportStyles(files) {
|
|
586
|
+
const patterns = {
|
|
587
|
+
esModules: [],
|
|
588
|
+
commonJS: [],
|
|
589
|
+
mixed: []
|
|
590
|
+
};
|
|
591
|
+
for (const file of files) {
|
|
592
|
+
const content = await readFileContent2(file);
|
|
593
|
+
const hasESM = content.match(/^import\s+/m);
|
|
594
|
+
const hasCJS = content.match(/require\s*\(/);
|
|
595
|
+
if (hasESM && hasCJS) {
|
|
596
|
+
patterns.mixed.push(file);
|
|
597
|
+
} else if (hasESM) {
|
|
598
|
+
patterns.esModules.push(file);
|
|
599
|
+
} else if (hasCJS) {
|
|
600
|
+
patterns.commonJS.push(file);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const issues = [];
|
|
604
|
+
if (patterns.mixed.length > 0) {
|
|
605
|
+
issues.push({
|
|
606
|
+
files: patterns.mixed,
|
|
607
|
+
type: "import-style",
|
|
608
|
+
description: "Mixed ES modules and CommonJS imports in same files",
|
|
609
|
+
examples: patterns.mixed.slice(0, 5),
|
|
610
|
+
severity: "major"
|
|
611
|
+
});
|
|
612
|
+
}
|
|
613
|
+
if (patterns.esModules.length > 0 && patterns.commonJS.length > 0) {
|
|
614
|
+
const ratio = patterns.commonJS.length / (patterns.esModules.length + patterns.commonJS.length);
|
|
615
|
+
if (ratio > 0.2 && ratio < 0.8) {
|
|
616
|
+
issues.push({
|
|
617
|
+
files: [...patterns.esModules, ...patterns.commonJS],
|
|
618
|
+
type: "import-style",
|
|
619
|
+
description: "Inconsistent import styles across project",
|
|
620
|
+
examples: [
|
|
621
|
+
`ES modules: ${patterns.esModules.length} files`,
|
|
622
|
+
`CommonJS: ${patterns.commonJS.length} files`
|
|
623
|
+
],
|
|
624
|
+
severity: "minor"
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return issues;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
// src/analyzer.ts
|
|
632
|
+
import { scanFiles } from "@aiready/core";
|
|
633
|
+
async function analyzeConsistency(options) {
|
|
634
|
+
const {
|
|
635
|
+
checkNaming = true,
|
|
636
|
+
checkPatterns = true,
|
|
637
|
+
checkArchitecture = false,
|
|
638
|
+
// Not implemented yet
|
|
639
|
+
minSeverity = "info",
|
|
640
|
+
...scanOptions
|
|
641
|
+
} = options;
|
|
642
|
+
const filePaths = await scanFiles(scanOptions);
|
|
643
|
+
const namingIssues = checkNaming ? await analyzeNaming(filePaths) : [];
|
|
644
|
+
const patternIssues = checkPatterns ? await analyzePatterns(filePaths) : [];
|
|
645
|
+
const results = [];
|
|
646
|
+
const fileIssuesMap = /* @__PURE__ */ new Map();
|
|
647
|
+
for (const issue of namingIssues) {
|
|
648
|
+
if (!shouldIncludeSeverity(issue.severity, minSeverity)) {
|
|
649
|
+
continue;
|
|
650
|
+
}
|
|
651
|
+
const consistencyIssue = {
|
|
652
|
+
type: issue.type === "convention-mix" ? "naming-inconsistency" : "naming-quality",
|
|
653
|
+
category: "naming",
|
|
654
|
+
severity: issue.severity,
|
|
655
|
+
message: `${issue.type}: ${issue.identifier}`,
|
|
656
|
+
location: {
|
|
657
|
+
file: issue.file,
|
|
658
|
+
line: issue.line,
|
|
659
|
+
column: issue.column
|
|
660
|
+
},
|
|
661
|
+
suggestion: issue.suggestion
|
|
662
|
+
};
|
|
663
|
+
if (!fileIssuesMap.has(issue.file)) {
|
|
664
|
+
fileIssuesMap.set(issue.file, []);
|
|
665
|
+
}
|
|
666
|
+
fileIssuesMap.get(issue.file).push(consistencyIssue);
|
|
667
|
+
}
|
|
668
|
+
for (const issue of patternIssues) {
|
|
669
|
+
if (!shouldIncludeSeverity(issue.severity, minSeverity)) {
|
|
670
|
+
continue;
|
|
671
|
+
}
|
|
672
|
+
const consistencyIssue = {
|
|
673
|
+
type: "pattern-inconsistency",
|
|
674
|
+
category: "patterns",
|
|
675
|
+
severity: issue.severity,
|
|
676
|
+
message: issue.description,
|
|
677
|
+
location: {
|
|
678
|
+
file: issue.files[0] || "multiple files",
|
|
679
|
+
line: 1
|
|
680
|
+
},
|
|
681
|
+
examples: issue.examples,
|
|
682
|
+
suggestion: `Standardize ${issue.type} patterns across ${issue.files.length} files`
|
|
683
|
+
};
|
|
684
|
+
const firstFile = issue.files[0];
|
|
685
|
+
if (firstFile && !fileIssuesMap.has(firstFile)) {
|
|
686
|
+
fileIssuesMap.set(firstFile, []);
|
|
687
|
+
}
|
|
688
|
+
if (firstFile) {
|
|
689
|
+
fileIssuesMap.get(firstFile).push(consistencyIssue);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
for (const [fileName, issues] of fileIssuesMap) {
|
|
693
|
+
results.push({
|
|
694
|
+
fileName,
|
|
695
|
+
issues,
|
|
696
|
+
metrics: {
|
|
697
|
+
consistencyScore: calculateConsistencyScore(issues)
|
|
698
|
+
}
|
|
699
|
+
});
|
|
700
|
+
}
|
|
701
|
+
const recommendations = generateRecommendations(namingIssues, patternIssues);
|
|
702
|
+
const conventionAnalysis = detectNamingConventions(filePaths, namingIssues);
|
|
703
|
+
return {
|
|
704
|
+
summary: {
|
|
705
|
+
totalIssues: namingIssues.length + patternIssues.length,
|
|
706
|
+
namingIssues: namingIssues.length,
|
|
707
|
+
patternIssues: patternIssues.length,
|
|
708
|
+
architectureIssues: 0,
|
|
709
|
+
filesAnalyzed: filePaths.length
|
|
710
|
+
},
|
|
711
|
+
results,
|
|
712
|
+
recommendations
|
|
713
|
+
};
|
|
714
|
+
}
|
|
715
|
+
function shouldIncludeSeverity(severity, minSeverity) {
|
|
716
|
+
const severityLevels = { info: 0, minor: 1, major: 2, critical: 3 };
|
|
717
|
+
return severityLevels[severity] >= severityLevels[minSeverity];
|
|
718
|
+
}
|
|
719
|
+
function calculateConsistencyScore(issues) {
|
|
720
|
+
const weights = { critical: 10, major: 5, minor: 2, info: 1 };
|
|
721
|
+
const totalWeight = issues.reduce((sum, issue) => sum + weights[issue.severity], 0);
|
|
722
|
+
return Math.max(0, 1 - totalWeight / 100);
|
|
723
|
+
}
|
|
724
|
+
function generateRecommendations(namingIssues, patternIssues) {
|
|
725
|
+
const recommendations = [];
|
|
726
|
+
if (namingIssues.length > 0) {
|
|
727
|
+
const conventionMixCount = namingIssues.filter((i) => i.type === "convention-mix").length;
|
|
728
|
+
if (conventionMixCount > 0) {
|
|
729
|
+
recommendations.push(
|
|
730
|
+
`Standardize naming conventions: Found ${conventionMixCount} snake_case variables in TypeScript/JavaScript (use camelCase)`
|
|
731
|
+
);
|
|
732
|
+
}
|
|
733
|
+
const poorNamingCount = namingIssues.filter((i) => i.type === "poor-naming").length;
|
|
734
|
+
if (poorNamingCount > 0) {
|
|
735
|
+
recommendations.push(
|
|
736
|
+
`Improve variable naming: Found ${poorNamingCount} single-letter or unclear variable names`
|
|
737
|
+
);
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
if (patternIssues.length > 0) {
|
|
741
|
+
const errorHandlingIssues = patternIssues.filter((i) => i.type === "error-handling");
|
|
742
|
+
if (errorHandlingIssues.length > 0) {
|
|
743
|
+
recommendations.push(
|
|
744
|
+
"Standardize error handling strategy across the codebase (prefer try-catch with typed errors)"
|
|
745
|
+
);
|
|
746
|
+
}
|
|
747
|
+
const asyncIssues = patternIssues.filter((i) => i.type === "async-style");
|
|
748
|
+
if (asyncIssues.length > 0) {
|
|
749
|
+
recommendations.push(
|
|
750
|
+
"Use async/await consistently instead of mixing with promise chains or callbacks"
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
const importIssues = patternIssues.filter((i) => i.type === "import-style");
|
|
754
|
+
if (importIssues.length > 0) {
|
|
755
|
+
recommendations.push(
|
|
756
|
+
"Use ES modules consistently across the project (avoid mixing with CommonJS)"
|
|
757
|
+
);
|
|
758
|
+
}
|
|
759
|
+
}
|
|
760
|
+
if (recommendations.length === 0) {
|
|
761
|
+
recommendations.push("No major consistency issues found! Your codebase follows good practices.");
|
|
762
|
+
}
|
|
763
|
+
return recommendations;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
export {
|
|
767
|
+
analyzeNaming,
|
|
768
|
+
detectNamingConventions,
|
|
769
|
+
analyzePatterns,
|
|
770
|
+
analyzeConsistency
|
|
771
|
+
};
|