@aiready/consistency 0.3.5 → 0.5.0

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