@aiready/consistency 0.3.0 → 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/README.md CHANGED
@@ -25,17 +25,24 @@ aiready-consistency ./src
25
25
  Inconsistent code patterns confuse AI models and reduce their effectiveness. This tool analyzes:
26
26
 
27
27
  ### 🏷️ Naming Quality & Conventions
28
- - Single-letter variables (except loop counters)
29
- - Unclear abbreviations
30
- - Mixed naming conventions (camelCase vs snake_case)
31
- - Boolean naming (should use is/has/can prefixes)
32
- - Function naming (should start with action verbs)
28
+ - **Single-letter variables** - Detects unclear variable names (skips common iterators: i, j, k, l, x, y, z in appropriate contexts)
29
+ - **Abbreviations** - Identifies unclear abbreviations while allowing 60+ standard ones (env, req, res, ctx, max, min, etc.)
30
+ - **Mixed naming conventions** - Detects snake_case in TypeScript/JavaScript projects (should use camelCase)
31
+ - **Boolean naming** - Ensures booleans use clear prefixes (is/has/can/should)
32
+ - **Function naming** - Checks for action verbs while allowing factory patterns and descriptive names
33
+
34
+ **Smart Detection:** The tool understands context and won't flag:
35
+ - Common abbreviations (env, api, url, max, min, now, etc.)
36
+ - Boolean prefixes (is, has, can used as variables)
37
+ - Loop iterators in appropriate contexts
38
+ - Factory/builder patterns
39
+ - Long descriptive function names
33
40
 
34
41
  ### 🔄 Pattern Consistency
35
- - Error handling strategies (try-catch vs returns)
36
- - Async patterns (async/await vs promises vs callbacks)
37
- - Import styles (ES modules vs CommonJS)
38
- - API design patterns
42
+ - **Error handling strategies** - Detects mixed approaches (try-catch vs returns vs throws)
43
+ - **Async patterns** - Identifies mixing of async/await, promises, and callbacks
44
+ - **Import styles** - Flags mixing of ES modules and CommonJS
45
+ - **API design patterns** - Ensures consistent patterns across endpoints
39
46
 
40
47
  ### 🏗️ Architectural Consistency *(coming soon)*
41
48
  - File organization patterns
@@ -149,6 +156,20 @@ Create `aiready.json` in your project root:
149
156
  | `checkPatterns` | boolean | `true` | Check code pattern consistency |
150
157
  | `minSeverity` | string | `'info'` | Filter: `'info'`, `'minor'`, `'major'`, `'critical'` |
151
158
 
159
+ ### Acceptable Abbreviations
160
+
161
+ The tool recognizes 60+ standard abbreviations and won't flag them:
162
+
163
+ **Web/Network:** url, uri, api, cdn, dns, ip, http, utm, seo, xhr
164
+ **Data:** json, xml, yaml, csv, html, css, svg, pdf, dto, dao
165
+ **System:** env, os, fs, cli, tmp, src, dst, bin, lib, pkg
166
+ **Request/Response:** req, res, ctx, err, msg
167
+ **Math:** max, min, avg, sum, abs, cos, sin, log, sqrt
168
+ **Time:** now, utc, ms, sec
169
+ **Common:** id, uid, db, sql, orm, ui, ux, dom, ref, val, str, obj, arr, cfg, init
170
+
171
+ See [naming.ts](src/analyzers/naming.ts) for the complete list.
172
+
152
173
  ## 🔧 Programmatic API
153
174
 
154
175
  ```typescript
@@ -0,0 +1,508 @@
1
+ // src/analyzers/naming.ts
2
+ import { readFileContent } from "@aiready/core";
3
+ var ACCEPTABLE_ABBREVIATIONS = /* @__PURE__ */ new Set([
4
+ // Standard identifiers
5
+ "id",
6
+ "uid",
7
+ "gid",
8
+ "pid",
9
+ // Web/Network
10
+ "url",
11
+ "uri",
12
+ "api",
13
+ "cdn",
14
+ "dns",
15
+ "ip",
16
+ "tcp",
17
+ "udp",
18
+ "http",
19
+ "ssl",
20
+ "tls",
21
+ "utm",
22
+ "seo",
23
+ "rss",
24
+ "xhr",
25
+ "ajax",
26
+ // Data formats
27
+ "json",
28
+ "xml",
29
+ "yaml",
30
+ "csv",
31
+ "html",
32
+ "css",
33
+ "svg",
34
+ "pdf",
35
+ // Databases
36
+ "db",
37
+ "sql",
38
+ "orm",
39
+ "dao",
40
+ "dto",
41
+ // File system
42
+ "fs",
43
+ "dir",
44
+ "tmp",
45
+ "src",
46
+ "dst",
47
+ "bin",
48
+ "lib",
49
+ "pkg",
50
+ // Operating system
51
+ "os",
52
+ "env",
53
+ "arg",
54
+ "cli",
55
+ "cmd",
56
+ "exe",
57
+ // UI/UX
58
+ "ui",
59
+ "ux",
60
+ "gui",
61
+ "dom",
62
+ "ref",
63
+ // Request/Response
64
+ "req",
65
+ "res",
66
+ "ctx",
67
+ "err",
68
+ "msg",
69
+ // Mathematics/Computing
70
+ "max",
71
+ "min",
72
+ "avg",
73
+ "sum",
74
+ "abs",
75
+ "cos",
76
+ "sin",
77
+ "tan",
78
+ "log",
79
+ "exp",
80
+ "pow",
81
+ "sqrt",
82
+ "std",
83
+ "var",
84
+ "int",
85
+ "num",
86
+ // Time
87
+ "now",
88
+ "utc",
89
+ "tz",
90
+ "ms",
91
+ "sec",
92
+ // Common patterns
93
+ "app",
94
+ "cfg",
95
+ "config",
96
+ "init",
97
+ "len",
98
+ "val",
99
+ "str",
100
+ "obj",
101
+ "arr",
102
+ "gen",
103
+ "def",
104
+ "raw",
105
+ "new",
106
+ "old",
107
+ "pre",
108
+ "post",
109
+ "sub",
110
+ "pub",
111
+ // Boolean helpers (these are intentional short names)
112
+ "is",
113
+ "has",
114
+ "can",
115
+ "did",
116
+ "was",
117
+ "are"
118
+ ]);
119
+ async function analyzeNaming(files) {
120
+ const issues = [];
121
+ for (const file of files) {
122
+ const content = await readFileContent(file);
123
+ const fileIssues = analyzeFileNaming(file, content);
124
+ issues.push(...fileIssues);
125
+ }
126
+ return issues;
127
+ }
128
+ function analyzeFileNaming(file, content) {
129
+ const issues = [];
130
+ const lines = content.split("\n");
131
+ lines.forEach((line, index) => {
132
+ const lineNumber = index + 1;
133
+ const singleLetterMatches = line.matchAll(/\b(?:const|let|var)\s+([a-hm-z])\s*=/gi);
134
+ for (const match of singleLetterMatches) {
135
+ const letter = match[1].toLowerCase();
136
+ const isInLoopContext = line.includes("for") || line.includes(".map") || line.includes(".filter") || line.includes(".forEach") || line.includes(".reduce");
137
+ if (!isInLoopContext && !["x", "y", "z", "i", "j", "k", "l", "n", "m"].includes(letter)) {
138
+ issues.push({
139
+ file,
140
+ line: lineNumber,
141
+ type: "poor-naming",
142
+ identifier: match[1],
143
+ severity: "minor",
144
+ suggestion: `Use descriptive variable name instead of single letter '${match[1]}'`
145
+ });
146
+ }
147
+ }
148
+ const abbreviationMatches = line.matchAll(/\b(?:const|let|var)\s+([a-z]{1,3})(?=[A-Z]|_|\s*=)/g);
149
+ for (const match of abbreviationMatches) {
150
+ const abbrev = match[1].toLowerCase();
151
+ if (!ACCEPTABLE_ABBREVIATIONS.has(abbrev)) {
152
+ issues.push({
153
+ file,
154
+ line: lineNumber,
155
+ type: "abbreviation",
156
+ identifier: match[1],
157
+ severity: "info",
158
+ suggestion: `Consider using full word instead of abbreviation '${match[1]}'`
159
+ });
160
+ }
161
+ }
162
+ if (file.match(/\.(ts|tsx|js|jsx)$/)) {
163
+ const camelCaseVars = line.match(/\b(?:const|let|var)\s+([a-z][a-zA-Z0-9]*)\s*=/);
164
+ const snakeCaseVars = line.match(/\b(?:const|let|var)\s+([a-z][a-z0-9]*_[a-z0-9_]*)\s*=/);
165
+ if (snakeCaseVars) {
166
+ issues.push({
167
+ file,
168
+ line: lineNumber,
169
+ type: "convention-mix",
170
+ identifier: snakeCaseVars[1],
171
+ severity: "minor",
172
+ suggestion: `Use camelCase '${snakeCaseToCamelCase(snakeCaseVars[1])}' instead of snake_case in TypeScript/JavaScript`
173
+ });
174
+ }
175
+ }
176
+ const booleanMatches = line.matchAll(/\b(?:const|let|var)\s+([a-z][a-zA-Z0-9]*)\s*:\s*boolean/gi);
177
+ for (const match of booleanMatches) {
178
+ const name = match[1];
179
+ if (!name.match(/^(is|has|should|can|will|did)/i)) {
180
+ issues.push({
181
+ file,
182
+ line: lineNumber,
183
+ type: "unclear",
184
+ identifier: name,
185
+ severity: "info",
186
+ suggestion: `Boolean variable '${name}' should start with is/has/should/can for clarity`
187
+ });
188
+ }
189
+ }
190
+ const functionMatches = line.matchAll(/function\s+([a-z][a-zA-Z0-9]*)/g);
191
+ for (const match of functionMatches) {
192
+ const name = match[1];
193
+ const isFactoryPattern = name.match(/(Factory|Builder|Creator|Generator)$/);
194
+ const isEventHandler = name.match(/^on[A-Z]/);
195
+ const isDescriptiveLong = name.length > 20;
196
+ 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)/);
197
+ if (!hasActionVerb && !isFactoryPattern && !isEventHandler && !isDescriptiveLong) {
198
+ issues.push({
199
+ file,
200
+ line: lineNumber,
201
+ type: "unclear",
202
+ identifier: name,
203
+ severity: "info",
204
+ suggestion: `Function '${name}' should start with an action verb (get, set, create, etc.)`
205
+ });
206
+ }
207
+ }
208
+ });
209
+ return issues;
210
+ }
211
+ function snakeCaseToCamelCase(str) {
212
+ return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
213
+ }
214
+ function detectNamingConventions(files, allIssues) {
215
+ const camelCaseCount = allIssues.filter((i) => i.type === "convention-mix").length;
216
+ const totalChecks = files.length * 10;
217
+ if (camelCaseCount / totalChecks > 0.3) {
218
+ return { dominantConvention: "mixed", conventionScore: 0.5 };
219
+ }
220
+ return { dominantConvention: "camelCase", conventionScore: 0.9 };
221
+ }
222
+
223
+ // src/analyzers/patterns.ts
224
+ import { readFileContent as readFileContent2 } from "@aiready/core";
225
+ async function analyzePatterns(files) {
226
+ const issues = [];
227
+ const errorHandlingIssues = await analyzeErrorHandling(files);
228
+ issues.push(...errorHandlingIssues);
229
+ const asyncIssues = await analyzeAsyncPatterns(files);
230
+ issues.push(...asyncIssues);
231
+ const importIssues = await analyzeImportStyles(files);
232
+ issues.push(...importIssues);
233
+ return issues;
234
+ }
235
+ async function analyzeErrorHandling(files) {
236
+ const patterns = {
237
+ tryCatch: [],
238
+ throwsError: [],
239
+ returnsNull: [],
240
+ returnsError: []
241
+ };
242
+ for (const file of files) {
243
+ const content = await readFileContent2(file);
244
+ if (content.includes("try {") || content.includes("} catch")) {
245
+ patterns.tryCatch.push(file);
246
+ }
247
+ if (content.match(/throw new \w*Error/)) {
248
+ patterns.throwsError.push(file);
249
+ }
250
+ if (content.match(/return null/)) {
251
+ patterns.returnsNull.push(file);
252
+ }
253
+ if (content.match(/return \{ error:/)) {
254
+ patterns.returnsError.push(file);
255
+ }
256
+ }
257
+ const issues = [];
258
+ const strategiesUsed = Object.values(patterns).filter((p) => p.length > 0).length;
259
+ if (strategiesUsed > 2) {
260
+ issues.push({
261
+ files: [.../* @__PURE__ */ new Set([
262
+ ...patterns.tryCatch,
263
+ ...patterns.throwsError,
264
+ ...patterns.returnsNull,
265
+ ...patterns.returnsError
266
+ ])],
267
+ type: "error-handling",
268
+ description: "Inconsistent error handling strategies across codebase",
269
+ examples: [
270
+ patterns.tryCatch.length > 0 ? `Try-catch used in ${patterns.tryCatch.length} files` : "",
271
+ patterns.throwsError.length > 0 ? `Throws errors in ${patterns.throwsError.length} files` : "",
272
+ patterns.returnsNull.length > 0 ? `Returns null in ${patterns.returnsNull.length} files` : "",
273
+ patterns.returnsError.length > 0 ? `Returns error objects in ${patterns.returnsError.length} files` : ""
274
+ ].filter((e) => e),
275
+ severity: "major"
276
+ });
277
+ }
278
+ return issues;
279
+ }
280
+ async function analyzeAsyncPatterns(files) {
281
+ const patterns = {
282
+ asyncAwait: [],
283
+ promises: [],
284
+ callbacks: []
285
+ };
286
+ for (const file of files) {
287
+ const content = await readFileContent2(file);
288
+ if (content.match(/async\s+(function|\(|[a-zA-Z])/)) {
289
+ patterns.asyncAwait.push(file);
290
+ }
291
+ if (content.match(/\.then\(/) || content.match(/\.catch\(/)) {
292
+ patterns.promises.push(file);
293
+ }
294
+ if (content.match(/callback\s*\(/) || content.match(/\(\s*err\s*,/)) {
295
+ patterns.callbacks.push(file);
296
+ }
297
+ }
298
+ const issues = [];
299
+ if (patterns.callbacks.length > 0 && patterns.asyncAwait.length > 0) {
300
+ issues.push({
301
+ files: [...patterns.callbacks, ...patterns.asyncAwait],
302
+ type: "async-style",
303
+ description: "Mixed async patterns: callbacks and async/await",
304
+ examples: [
305
+ `Callbacks found in: ${patterns.callbacks.slice(0, 3).join(", ")}`,
306
+ `Async/await used in: ${patterns.asyncAwait.slice(0, 3).join(", ")}`
307
+ ],
308
+ severity: "minor"
309
+ });
310
+ }
311
+ if (patterns.promises.length > patterns.asyncAwait.length * 0.3 && patterns.asyncAwait.length > 0) {
312
+ issues.push({
313
+ files: patterns.promises,
314
+ type: "async-style",
315
+ description: "Consider using async/await instead of promise chains for consistency",
316
+ examples: patterns.promises.slice(0, 5),
317
+ severity: "info"
318
+ });
319
+ }
320
+ return issues;
321
+ }
322
+ async function analyzeImportStyles(files) {
323
+ const patterns = {
324
+ esModules: [],
325
+ commonJS: [],
326
+ mixed: []
327
+ };
328
+ for (const file of files) {
329
+ const content = await readFileContent2(file);
330
+ const hasESM = content.match(/^import\s+/m);
331
+ const hasCJS = content.match(/require\s*\(/);
332
+ if (hasESM && hasCJS) {
333
+ patterns.mixed.push(file);
334
+ } else if (hasESM) {
335
+ patterns.esModules.push(file);
336
+ } else if (hasCJS) {
337
+ patterns.commonJS.push(file);
338
+ }
339
+ }
340
+ const issues = [];
341
+ if (patterns.mixed.length > 0) {
342
+ issues.push({
343
+ files: patterns.mixed,
344
+ type: "import-style",
345
+ description: "Mixed ES modules and CommonJS imports in same files",
346
+ examples: patterns.mixed.slice(0, 5),
347
+ severity: "major"
348
+ });
349
+ }
350
+ if (patterns.esModules.length > 0 && patterns.commonJS.length > 0) {
351
+ const ratio = patterns.commonJS.length / (patterns.esModules.length + patterns.commonJS.length);
352
+ if (ratio > 0.2 && ratio < 0.8) {
353
+ issues.push({
354
+ files: [...patterns.esModules, ...patterns.commonJS],
355
+ type: "import-style",
356
+ description: "Inconsistent import styles across project",
357
+ examples: [
358
+ `ES modules: ${patterns.esModules.length} files`,
359
+ `CommonJS: ${patterns.commonJS.length} files`
360
+ ],
361
+ severity: "minor"
362
+ });
363
+ }
364
+ }
365
+ return issues;
366
+ }
367
+
368
+ // src/analyzer.ts
369
+ import { scanFiles } from "@aiready/core";
370
+ async function analyzeConsistency(options) {
371
+ const {
372
+ checkNaming = true,
373
+ checkPatterns = true,
374
+ checkArchitecture = false,
375
+ // Not implemented yet
376
+ minSeverity = "info",
377
+ ...scanOptions
378
+ } = options;
379
+ const filePaths = await scanFiles(scanOptions);
380
+ const namingIssues = checkNaming ? await analyzeNaming(filePaths) : [];
381
+ const patternIssues = checkPatterns ? await analyzePatterns(filePaths) : [];
382
+ const results = [];
383
+ const fileIssuesMap = /* @__PURE__ */ new Map();
384
+ for (const issue of namingIssues) {
385
+ if (!shouldIncludeSeverity(issue.severity, minSeverity)) {
386
+ continue;
387
+ }
388
+ const consistencyIssue = {
389
+ type: issue.type === "convention-mix" ? "naming-inconsistency" : "naming-quality",
390
+ category: "naming",
391
+ severity: issue.severity,
392
+ message: `${issue.type}: ${issue.identifier}`,
393
+ location: {
394
+ file: issue.file,
395
+ line: issue.line,
396
+ column: issue.column
397
+ },
398
+ suggestion: issue.suggestion
399
+ };
400
+ if (!fileIssuesMap.has(issue.file)) {
401
+ fileIssuesMap.set(issue.file, []);
402
+ }
403
+ fileIssuesMap.get(issue.file).push(consistencyIssue);
404
+ }
405
+ for (const issue of patternIssues) {
406
+ if (!shouldIncludeSeverity(issue.severity, minSeverity)) {
407
+ continue;
408
+ }
409
+ const consistencyIssue = {
410
+ type: "pattern-inconsistency",
411
+ category: "patterns",
412
+ severity: issue.severity,
413
+ message: issue.description,
414
+ location: {
415
+ file: issue.files[0] || "multiple files",
416
+ line: 1
417
+ },
418
+ examples: issue.examples,
419
+ suggestion: `Standardize ${issue.type} patterns across ${issue.files.length} files`
420
+ };
421
+ const firstFile = issue.files[0];
422
+ if (firstFile && !fileIssuesMap.has(firstFile)) {
423
+ fileIssuesMap.set(firstFile, []);
424
+ }
425
+ if (firstFile) {
426
+ fileIssuesMap.get(firstFile).push(consistencyIssue);
427
+ }
428
+ }
429
+ for (const [fileName, issues] of fileIssuesMap) {
430
+ results.push({
431
+ fileName,
432
+ issues,
433
+ metrics: {
434
+ consistencyScore: calculateConsistencyScore(issues)
435
+ }
436
+ });
437
+ }
438
+ const recommendations = generateRecommendations(namingIssues, patternIssues);
439
+ const conventionAnalysis = detectNamingConventions(filePaths, namingIssues);
440
+ return {
441
+ summary: {
442
+ totalIssues: namingIssues.length + patternIssues.length,
443
+ namingIssues: namingIssues.length,
444
+ patternIssues: patternIssues.length,
445
+ architectureIssues: 0,
446
+ filesAnalyzed: filePaths.length
447
+ },
448
+ results,
449
+ recommendations
450
+ };
451
+ }
452
+ function shouldIncludeSeverity(severity, minSeverity) {
453
+ const severityLevels = { info: 0, minor: 1, major: 2, critical: 3 };
454
+ return severityLevels[severity] >= severityLevels[minSeverity];
455
+ }
456
+ function calculateConsistencyScore(issues) {
457
+ const weights = { critical: 10, major: 5, minor: 2, info: 1 };
458
+ const totalWeight = issues.reduce((sum, issue) => sum + weights[issue.severity], 0);
459
+ return Math.max(0, 1 - totalWeight / 100);
460
+ }
461
+ function generateRecommendations(namingIssues, patternIssues) {
462
+ const recommendations = [];
463
+ if (namingIssues.length > 0) {
464
+ const conventionMixCount = namingIssues.filter((i) => i.type === "convention-mix").length;
465
+ if (conventionMixCount > 0) {
466
+ recommendations.push(
467
+ `Standardize naming conventions: Found ${conventionMixCount} snake_case variables in TypeScript/JavaScript (use camelCase)`
468
+ );
469
+ }
470
+ const poorNamingCount = namingIssues.filter((i) => i.type === "poor-naming").length;
471
+ if (poorNamingCount > 0) {
472
+ recommendations.push(
473
+ `Improve variable naming: Found ${poorNamingCount} single-letter or unclear variable names`
474
+ );
475
+ }
476
+ }
477
+ if (patternIssues.length > 0) {
478
+ const errorHandlingIssues = patternIssues.filter((i) => i.type === "error-handling");
479
+ if (errorHandlingIssues.length > 0) {
480
+ recommendations.push(
481
+ "Standardize error handling strategy across the codebase (prefer try-catch with typed errors)"
482
+ );
483
+ }
484
+ const asyncIssues = patternIssues.filter((i) => i.type === "async-style");
485
+ if (asyncIssues.length > 0) {
486
+ recommendations.push(
487
+ "Use async/await consistently instead of mixing with promise chains or callbacks"
488
+ );
489
+ }
490
+ const importIssues = patternIssues.filter((i) => i.type === "import-style");
491
+ if (importIssues.length > 0) {
492
+ recommendations.push(
493
+ "Use ES modules consistently across the project (avoid mixing with CommonJS)"
494
+ );
495
+ }
496
+ }
497
+ if (recommendations.length === 0) {
498
+ recommendations.push("No major consistency issues found! Your codebase follows good practices.");
499
+ }
500
+ return recommendations;
501
+ }
502
+
503
+ export {
504
+ analyzeNaming,
505
+ detectNamingConventions,
506
+ analyzePatterns,
507
+ analyzeConsistency
508
+ };