@vibecheckai/cli 3.5.0 → 3.5.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.
Files changed (224) hide show
  1. package/bin/registry.js +214 -237
  2. package/bin/runners/cli-utils.js +33 -2
  3. package/bin/runners/context/analyzer.js +52 -1
  4. package/bin/runners/context/generators/cursor.js +2 -49
  5. package/bin/runners/context/git-context.js +3 -1
  6. package/bin/runners/context/team-conventions.js +33 -7
  7. package/bin/runners/lib/analysis-core.js +25 -5
  8. package/bin/runners/lib/analyzers.js +431 -481
  9. package/bin/runners/lib/default-config.js +127 -0
  10. package/bin/runners/lib/doctor/modules/security.js +3 -1
  11. package/bin/runners/lib/engine/ast-cache.js +210 -0
  12. package/bin/runners/lib/engine/auth-extractor.js +211 -0
  13. package/bin/runners/lib/engine/billing-extractor.js +112 -0
  14. package/bin/runners/lib/engine/enforcement-extractor.js +100 -0
  15. package/bin/runners/lib/engine/env-extractor.js +207 -0
  16. package/bin/runners/lib/engine/express-extractor.js +208 -0
  17. package/bin/runners/lib/engine/extractors.js +849 -0
  18. package/bin/runners/lib/engine/index.js +207 -0
  19. package/bin/runners/lib/engine/repo-index.js +514 -0
  20. package/bin/runners/lib/engine/types.js +124 -0
  21. package/bin/runners/lib/engines/accessibility-engine.js +18 -218
  22. package/bin/runners/lib/engines/api-consistency-engine.js +30 -335
  23. package/bin/runners/lib/engines/cross-file-analysis-engine.js +27 -292
  24. package/bin/runners/lib/engines/empty-catch-engine.js +17 -127
  25. package/bin/runners/lib/engines/mock-data-engine.js +10 -53
  26. package/bin/runners/lib/engines/performance-issues-engine.js +36 -176
  27. package/bin/runners/lib/engines/security-vulnerabilities-engine.js +54 -382
  28. package/bin/runners/lib/engines/type-aware-engine.js +39 -263
  29. package/bin/runners/lib/engines/vibecheck-engines/index.js +13 -122
  30. package/bin/runners/lib/engines/vibecheck-engines/lib/ast-cache.js +164 -0
  31. package/bin/runners/lib/engines/vibecheck-engines/lib/code-quality-engine.js +291 -0
  32. package/bin/runners/lib/engines/vibecheck-engines/lib/console-logs-engine.js +83 -0
  33. package/bin/runners/lib/engines/vibecheck-engines/lib/dead-code-engine.js +198 -0
  34. package/bin/runners/lib/engines/vibecheck-engines/lib/deprecated-api-engine.js +275 -0
  35. package/bin/runners/lib/engines/vibecheck-engines/lib/empty-catch-engine.js +167 -0
  36. package/bin/runners/lib/engines/vibecheck-engines/lib/file-filter.js +217 -0
  37. package/bin/runners/lib/engines/vibecheck-engines/lib/hardcoded-secrets-engine.js +73 -373
  38. package/bin/runners/lib/engines/vibecheck-engines/lib/mock-data-engine.js +140 -0
  39. package/bin/runners/lib/engines/vibecheck-engines/lib/parallel-processor.js +164 -0
  40. package/bin/runners/lib/engines/vibecheck-engines/lib/performance-issues-engine.js +234 -0
  41. package/bin/runners/lib/engines/vibecheck-engines/lib/type-aware-engine.js +217 -0
  42. package/bin/runners/lib/engines/vibecheck-engines/lib/unsafe-regex-engine.js +78 -0
  43. package/bin/runners/lib/entitlements-v2.js +73 -97
  44. package/bin/runners/lib/error-handler.js +44 -3
  45. package/bin/runners/lib/error-messages.js +289 -0
  46. package/bin/runners/lib/evidence-pack.js +7 -1
  47. package/bin/runners/lib/finding-id.js +69 -0
  48. package/bin/runners/lib/finding-sorter.js +89 -0
  49. package/bin/runners/lib/html-proof-report.js +700 -350
  50. package/bin/runners/lib/missions/plan.js +6 -46
  51. package/bin/runners/lib/missions/templates.js +0 -232
  52. package/bin/runners/lib/next-action.js +560 -0
  53. package/bin/runners/lib/prerequisites.js +149 -0
  54. package/bin/runners/lib/route-detection.js +137 -68
  55. package/bin/runners/lib/scan-output.js +91 -76
  56. package/bin/runners/lib/scan-runner.js +135 -0
  57. package/bin/runners/lib/schemas/ajv-validator.js +464 -0
  58. package/bin/runners/lib/schemas/error-envelope.schema.json +105 -0
  59. package/bin/runners/lib/schemas/finding-v3.schema.json +151 -0
  60. package/bin/runners/lib/schemas/report-artifact.schema.json +120 -0
  61. package/bin/runners/lib/schemas/run-request.schema.json +108 -0
  62. package/bin/runners/lib/schemas/validator.js +27 -0
  63. package/bin/runners/lib/schemas/verdict.schema.json +140 -0
  64. package/bin/runners/lib/ship-output-enterprise.js +23 -23
  65. package/bin/runners/lib/ship-output.js +75 -31
  66. package/bin/runners/lib/terminal-ui.js +6 -113
  67. package/bin/runners/lib/truth.js +351 -10
  68. package/bin/runners/lib/unified-cli-output.js +430 -603
  69. package/bin/runners/lib/unified-output.js +13 -9
  70. package/bin/runners/runAIAgent.js +10 -5
  71. package/bin/runners/runAgent.js +0 -3
  72. package/bin/runners/runAllowlist.js +389 -0
  73. package/bin/runners/runApprove.js +0 -33
  74. package/bin/runners/runAuth.js +73 -45
  75. package/bin/runners/runCheckpoint.js +51 -11
  76. package/bin/runners/runClassify.js +85 -21
  77. package/bin/runners/runContext.js +0 -3
  78. package/bin/runners/runDoctor.js +41 -28
  79. package/bin/runners/runEvidencePack.js +362 -0
  80. package/bin/runners/runFirewall.js +0 -3
  81. package/bin/runners/runFirewallHook.js +0 -3
  82. package/bin/runners/runFix.js +66 -76
  83. package/bin/runners/runGuard.js +18 -411
  84. package/bin/runners/runInit.js +113 -30
  85. package/bin/runners/runLabs.js +424 -0
  86. package/bin/runners/runMcp.js +19 -25
  87. package/bin/runners/runPolish.js +64 -240
  88. package/bin/runners/runPromptFirewall.js +12 -5
  89. package/bin/runners/runProve.js +57 -22
  90. package/bin/runners/runQuickstart.js +531 -0
  91. package/bin/runners/runReality.js +59 -68
  92. package/bin/runners/runReport.js +38 -33
  93. package/bin/runners/runRuntime.js +8 -5
  94. package/bin/runners/runScan.js +1413 -190
  95. package/bin/runners/runShip.js +113 -719
  96. package/bin/runners/runTruth.js +0 -3
  97. package/bin/runners/runValidate.js +13 -9
  98. package/bin/runners/runWatch.js +23 -14
  99. package/bin/scan.js +6 -1
  100. package/bin/vibecheck.js +204 -185
  101. package/mcp-server/deprecation-middleware.js +282 -0
  102. package/mcp-server/handlers/index.ts +15 -0
  103. package/mcp-server/handlers/tool-handler.ts +554 -0
  104. package/mcp-server/index-v1.js +698 -0
  105. package/mcp-server/index.js +210 -238
  106. package/mcp-server/lib/cache-wrapper.cjs +383 -0
  107. package/mcp-server/lib/error-envelope.js +138 -0
  108. package/mcp-server/lib/executor.ts +499 -0
  109. package/mcp-server/lib/index.ts +19 -0
  110. package/mcp-server/lib/rate-limiter.js +166 -0
  111. package/mcp-server/lib/sandbox.test.ts +519 -0
  112. package/mcp-server/lib/sandbox.ts +395 -0
  113. package/mcp-server/lib/types.ts +267 -0
  114. package/mcp-server/package.json +12 -3
  115. package/mcp-server/registry/tool-registry.js +794 -0
  116. package/mcp-server/registry/tools.json +605 -0
  117. package/mcp-server/registry.test.ts +334 -0
  118. package/mcp-server/tests/tier-gating.test.js +297 -0
  119. package/mcp-server/tier-auth.js +378 -45
  120. package/mcp-server/tools-v3.js +353 -442
  121. package/mcp-server/tsconfig.json +37 -0
  122. package/mcp-server/vibecheck-2.0-tools.js +14 -1
  123. package/package.json +1 -1
  124. package/bin/runners/lib/agent-firewall/learning/learning-engine.js +0 -849
  125. package/bin/runners/lib/audit-logger.js +0 -532
  126. package/bin/runners/lib/authority/authorities/architecture.js +0 -364
  127. package/bin/runners/lib/authority/authorities/compliance.js +0 -341
  128. package/bin/runners/lib/authority/authorities/human.js +0 -343
  129. package/bin/runners/lib/authority/authorities/quality.js +0 -420
  130. package/bin/runners/lib/authority/authorities/security.js +0 -228
  131. package/bin/runners/lib/authority/index.js +0 -293
  132. package/bin/runners/lib/bundle/bundle-intelligence.js +0 -846
  133. package/bin/runners/lib/cli-charts.js +0 -368
  134. package/bin/runners/lib/cli-config-display.js +0 -405
  135. package/bin/runners/lib/cli-demo.js +0 -275
  136. package/bin/runners/lib/cli-errors.js +0 -438
  137. package/bin/runners/lib/cli-help-formatter.js +0 -439
  138. package/bin/runners/lib/cli-interactive-menu.js +0 -509
  139. package/bin/runners/lib/cli-prompts.js +0 -441
  140. package/bin/runners/lib/cli-scan-cards.js +0 -362
  141. package/bin/runners/lib/compliance-reporter.js +0 -710
  142. package/bin/runners/lib/conductor/index.js +0 -671
  143. package/bin/runners/lib/easy/README.md +0 -123
  144. package/bin/runners/lib/easy/index.js +0 -140
  145. package/bin/runners/lib/easy/interactive-wizard.js +0 -788
  146. package/bin/runners/lib/easy/one-click-firewall.js +0 -564
  147. package/bin/runners/lib/easy/zero-config-reality.js +0 -714
  148. package/bin/runners/lib/engines/async-patterns-engine.js +0 -444
  149. package/bin/runners/lib/engines/bundle-size-engine.js +0 -433
  150. package/bin/runners/lib/engines/confidence-scoring.js +0 -276
  151. package/bin/runners/lib/engines/context-detection.js +0 -264
  152. package/bin/runners/lib/engines/database-patterns-engine.js +0 -429
  153. package/bin/runners/lib/engines/duplicate-code-engine.js +0 -354
  154. package/bin/runners/lib/engines/env-variables-engine.js +0 -458
  155. package/bin/runners/lib/engines/error-handling-engine.js +0 -437
  156. package/bin/runners/lib/engines/false-positive-prevention.js +0 -630
  157. package/bin/runners/lib/engines/framework-adapters/index.js +0 -607
  158. package/bin/runners/lib/engines/framework-detection.js +0 -508
  159. package/bin/runners/lib/engines/import-order-engine.js +0 -429
  160. package/bin/runners/lib/engines/naming-conventions-engine.js +0 -544
  161. package/bin/runners/lib/engines/noise-reduction-engine.js +0 -452
  162. package/bin/runners/lib/engines/orchestrator.js +0 -334
  163. package/bin/runners/lib/engines/react-patterns-engine.js +0 -457
  164. package/bin/runners/lib/engines/vibecheck-engines/lib/ai-hallucination-engine.js +0 -806
  165. package/bin/runners/lib/engines/vibecheck-engines/lib/smart-fix-engine.js +0 -577
  166. package/bin/runners/lib/engines/vibecheck-engines/lib/vibe-score-engine.js +0 -543
  167. package/bin/runners/lib/engines/vibecheck-engines.js +0 -514
  168. package/bin/runners/lib/enhanced-features/index.js +0 -305
  169. package/bin/runners/lib/enhanced-output.js +0 -631
  170. package/bin/runners/lib/enterprise.js +0 -300
  171. package/bin/runners/lib/firewall/command-validator.js +0 -351
  172. package/bin/runners/lib/firewall/config.js +0 -341
  173. package/bin/runners/lib/firewall/content-validator.js +0 -519
  174. package/bin/runners/lib/firewall/index.js +0 -101
  175. package/bin/runners/lib/firewall/path-validator.js +0 -256
  176. package/bin/runners/lib/intelligence/cross-repo-intelligence.js +0 -817
  177. package/bin/runners/lib/mcp-utils.js +0 -425
  178. package/bin/runners/lib/output/index.js +0 -1022
  179. package/bin/runners/lib/policy-engine.js +0 -652
  180. package/bin/runners/lib/polish/autofix/accessibility-fixes.js +0 -333
  181. package/bin/runners/lib/polish/autofix/async-handlers.js +0 -273
  182. package/bin/runners/lib/polish/autofix/dead-code.js +0 -280
  183. package/bin/runners/lib/polish/autofix/imports-optimizer.js +0 -344
  184. package/bin/runners/lib/polish/autofix/index.js +0 -200
  185. package/bin/runners/lib/polish/autofix/remove-consoles.js +0 -209
  186. package/bin/runners/lib/polish/autofix/strengthen-types.js +0 -245
  187. package/bin/runners/lib/polish/backend-checks.js +0 -148
  188. package/bin/runners/lib/polish/documentation-checks.js +0 -111
  189. package/bin/runners/lib/polish/frontend-checks.js +0 -168
  190. package/bin/runners/lib/polish/index.js +0 -71
  191. package/bin/runners/lib/polish/infrastructure-checks.js +0 -131
  192. package/bin/runners/lib/polish/library-detection.js +0 -175
  193. package/bin/runners/lib/polish/performance-checks.js +0 -100
  194. package/bin/runners/lib/polish/security-checks.js +0 -148
  195. package/bin/runners/lib/polish/utils.js +0 -203
  196. package/bin/runners/lib/prompt-builder.js +0 -540
  197. package/bin/runners/lib/proof-certificate.js +0 -634
  198. package/bin/runners/lib/reality/accessibility-audit.js +0 -946
  199. package/bin/runners/lib/reality/api-contract-validator.js +0 -1012
  200. package/bin/runners/lib/reality/chaos-engineering.js +0 -1084
  201. package/bin/runners/lib/reality/performance-tracker.js +0 -1077
  202. package/bin/runners/lib/reality/scenario-generator.js +0 -1404
  203. package/bin/runners/lib/reality/visual-regression.js +0 -852
  204. package/bin/runners/lib/reality-profiler.js +0 -717
  205. package/bin/runners/lib/replay/flight-recorder-viewer.js +0 -1160
  206. package/bin/runners/lib/review/ai-code-review.js +0 -832
  207. package/bin/runners/lib/rules/custom-rule-engine.js +0 -985
  208. package/bin/runners/lib/sbom-generator.js +0 -641
  209. package/bin/runners/lib/scan-output-enhanced.js +0 -512
  210. package/bin/runners/lib/security/owasp-scanner.js +0 -939
  211. package/bin/runners/lib/validators/contract-validator.js +0 -283
  212. package/bin/runners/lib/validators/dead-export-detector.js +0 -279
  213. package/bin/runners/lib/validators/dep-audit.js +0 -245
  214. package/bin/runners/lib/validators/env-validator.js +0 -319
  215. package/bin/runners/lib/validators/index.js +0 -120
  216. package/bin/runners/lib/validators/license-checker.js +0 -252
  217. package/bin/runners/lib/validators/route-validator.js +0 -290
  218. package/bin/runners/runAuthority.js +0 -528
  219. package/bin/runners/runConductor.js +0 -772
  220. package/bin/runners/runContainer.js +0 -366
  221. package/bin/runners/runEasy.js +0 -410
  222. package/bin/runners/runIaC.js +0 -372
  223. package/bin/runners/runVibe.js +0 -791
  224. package/mcp-server/tools.js +0 -495
@@ -0,0 +1,164 @@
1
+ /**
2
+ * Parallel Processor
3
+ * Utility to process a list of files concurrently with bounded parallelism.
4
+ *
5
+ * NOTE: The caller provides `processor(filePath)` which may return any value.
6
+ * We collect both results and errors without crashing the whole run.
7
+ */
8
+
9
+ const os = require("os");
10
+
11
+ /**
12
+ * Run a promise with an optional timeout.
13
+ */
14
+ async function withTimeout(promise, timeoutMs, label = "operation") {
15
+ if (!timeoutMs || timeoutMs <= 0) return promise;
16
+
17
+ let timer = null;
18
+ const timeout = new Promise((_, reject) => {
19
+ timer = setTimeout(() => {
20
+ const err = new Error(`${label} timed out after ${timeoutMs}ms`);
21
+ err.code = "ETIMEDOUT";
22
+ reject(err);
23
+ }, timeoutMs);
24
+ });
25
+
26
+ try {
27
+ return await Promise.race([promise, timeout]);
28
+ } finally {
29
+ if (timer) clearTimeout(timer);
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Process files in parallel with bounded concurrency.
35
+ *
36
+ * @param {string[]} files
37
+ * @param {(filePath: string) => Promise<any>} processor
38
+ * @param {object} options
39
+ * @returns {Promise<{results: Array<{file: string, result: any}>, errors: Array<{file: string, error: Error}>, stats: object}>}
40
+ */
41
+ async function processFilesInParallel(files, processor, options = {}) {
42
+ const concurrency =
43
+ Number.isFinite(options.concurrency) && options.concurrency > 0
44
+ ? Math.floor(options.concurrency)
45
+ : Math.max(1, Math.min(os.cpus().length, 8));
46
+
47
+ const timeoutMs = Number.isFinite(options.timeoutMs) ? options.timeoutMs : 0;
48
+ const stopOnError = Boolean(options.stopOnError);
49
+ const onProgress = typeof options.onProgress === "function" ? options.onProgress : null;
50
+ const signal = options.signal;
51
+
52
+ const results = [];
53
+ const errors = [];
54
+
55
+ let cursor = 0;
56
+ let completed = 0;
57
+ const startedAt = Date.now();
58
+
59
+ function reportProgress(file, kind) {
60
+ if (!onProgress) return;
61
+ try {
62
+ onProgress({
63
+ kind,
64
+ file,
65
+ completed,
66
+ total: files.length,
67
+ errors: errors.length,
68
+ elapsedMs: Date.now() - startedAt,
69
+ });
70
+ } catch {
71
+ // ignore progress handler failures
72
+ }
73
+ }
74
+
75
+ async function runFile(filePath) {
76
+ if (signal?.aborted) {
77
+ const err = new Error("Aborted");
78
+ err.code = "ABORTED";
79
+ throw err;
80
+ }
81
+
82
+ const label = `process(${filePath})`;
83
+ const result = await withTimeout(Promise.resolve().then(() => processor(filePath)), timeoutMs, label);
84
+ return result;
85
+ }
86
+
87
+ async function worker(workerId) {
88
+ // eslint-disable-next-line no-constant-condition
89
+ while (true) {
90
+ if (signal?.aborted) break;
91
+ if (stopOnError && errors.length) break;
92
+
93
+ const i = cursor++;
94
+ if (i >= files.length) break;
95
+
96
+ const filePath = files[i];
97
+ reportProgress(filePath, "start");
98
+
99
+ try {
100
+ const result = await runFile(filePath);
101
+ results.push({ file: filePath, result });
102
+ } catch (error) {
103
+ errors.push({ file: filePath, error });
104
+ reportProgress(filePath, "error");
105
+ } finally {
106
+ completed++;
107
+ reportProgress(filePath, "done");
108
+ }
109
+ }
110
+ }
111
+
112
+ const workers = Array.from({ length: Math.min(concurrency, files.length) }, (_, i) => worker(i));
113
+ await Promise.all(workers);
114
+
115
+ return {
116
+ results,
117
+ errors,
118
+ stats: {
119
+ totalFiles: files.length,
120
+ processed: completed,
121
+ results: results.length,
122
+ errors: errors.length,
123
+ concurrency,
124
+ timeoutMs,
125
+ elapsedMs: Date.now() - startedAt,
126
+ aborted: Boolean(signal?.aborted),
127
+ },
128
+ };
129
+ }
130
+
131
+ /**
132
+ * Process in sequential batches (useful when you want deterministic ordering).
133
+ *
134
+ * @param {string[]} files
135
+ * @param {(filePath: string) => Promise<any>} processor
136
+ * @param {number} batchSize
137
+ */
138
+ async function processFilesInBatches(files, processor, batchSize = 10) {
139
+ const results = [];
140
+ const errors = [];
141
+ const size = Math.max(1, Number(batchSize) || 10);
142
+
143
+ for (let i = 0; i < files.length; i += size) {
144
+ const batch = files.slice(i, i + size);
145
+ const batchResults = await Promise.allSettled(batch.map((f) => Promise.resolve().then(() => processor(f))));
146
+
147
+ for (let j = 0; j < batchResults.length; j++) {
148
+ const file = batch[j];
149
+ const r = batchResults[j];
150
+ if (r.status === "fulfilled") {
151
+ results.push({ file, result: r.value });
152
+ } else {
153
+ errors.push({ file, error: r.reason });
154
+ }
155
+ }
156
+ }
157
+
158
+ return { results, errors };
159
+ }
160
+
161
+ module.exports = {
162
+ processFilesInParallel,
163
+ processFilesInBatches,
164
+ };
@@ -0,0 +1,234 @@
1
+ /**
2
+ * Performance Issues Engine
3
+ * Detects potential performance problems:
4
+ * - Nested loops
5
+ * - Potential memory leaks (event listeners without cleanup)
6
+ * - Expensive operations in render-like contexts (heuristic)
7
+ */
8
+
9
+ const { getAST, parseCode } = require("./ast-cache");
10
+ const traverse = require("@babel/traverse").default;
11
+ const t = require("@babel/types");
12
+ const { shouldExcludeFile, isTestContext, hasIgnoreDirective } = require("./file-filter");
13
+
14
+ function snippetForLine(lines, line) {
15
+ return lines[line - 1] ? lines[line - 1].trim() : "";
16
+ }
17
+
18
+ function isLoopStatement(node) {
19
+ return (
20
+ t.isForStatement(node) ||
21
+ t.isForInStatement(node) ||
22
+ t.isForOfStatement(node) ||
23
+ t.isWhileStatement(node) ||
24
+ t.isDoWhileStatement(node)
25
+ );
26
+ }
27
+
28
+ function getEventName(callNode) {
29
+ const arg0 = callNode.arguments?.[0];
30
+ if (t.isStringLiteral(arg0)) return arg0.value;
31
+ if (t.isIdentifier(arg0)) return arg0.name;
32
+ return null;
33
+ }
34
+
35
+ function calleePropertyName(callNode) {
36
+ const callee = callNode.callee;
37
+ if (t.isMemberExpression(callee) && t.isIdentifier(callee.property) && !callee.computed) return callee.property.name;
38
+ if (t.isIdentifier(callee)) return callee.name;
39
+ return null;
40
+ }
41
+
42
+ function hasMatchingRemoveEventListener(fnPath, eventName) {
43
+ let found = false;
44
+
45
+ fnPath.traverse({
46
+ Function(inner) {
47
+ if (inner !== fnPath) inner.skip();
48
+ },
49
+ CallExpression(p) {
50
+ const name = calleePropertyName(p.node);
51
+ if (name !== "removeEventListener") return;
52
+ const ev = getEventName(p.node);
53
+ if (!eventName || !ev) {
54
+ // no event info: still counts as cleanup
55
+ found = true;
56
+ return;
57
+ }
58
+ if (ev === eventName) found = true;
59
+ },
60
+ });
61
+
62
+ return found;
63
+ }
64
+
65
+ function isLikelyRenderContext(path) {
66
+ // Heuristic: inside a function named render, or a React component (PascalCase) returning JSX
67
+ const fn = path.getFunctionParent();
68
+ if (!fn) return false;
69
+
70
+ const node = fn.node;
71
+ let name = null;
72
+
73
+ if (t.isFunctionDeclaration(node) && node.id?.name) name = node.id.name;
74
+ if ((t.isFunctionExpression(node) || t.isArrowFunctionExpression(node)) && t.isVariableDeclarator(fn.parent) && t.isIdentifier(fn.parent.id)) {
75
+ name = fn.parent.id.name;
76
+ }
77
+
78
+ if (name === "render") return true;
79
+ if (name && /^[A-Z]/.test(name)) return true; // component-ish
80
+
81
+ // If function returns JSX somewhere
82
+ let returnsJSX = false;
83
+ fn.traverse({
84
+ Function(inner) {
85
+ if (inner !== fn) inner.skip();
86
+ },
87
+ ReturnStatement(r) {
88
+ if (t.isJSXElement(r.node.argument) || t.isJSXFragment(r.node.argument)) returnsJSX = true;
89
+ },
90
+ });
91
+
92
+ return returnsJSX;
93
+ }
94
+
95
+ function analyzePerformanceIssues(code, filePath) {
96
+ const findings = [];
97
+
98
+ if (shouldExcludeFile(filePath)) return findings;
99
+ if (isTestContext(code, filePath)) return findings;
100
+ if (hasIgnoreDirective(code, "performance")) return findings;
101
+
102
+ const ast = getAST(code, filePath);
103
+ if (!ast) return findings;
104
+
105
+ const lines = code.split("\n");
106
+
107
+ traverse(ast, {
108
+ // Nested loops - only flag 3+ levels deep
109
+ enter(path) {
110
+ if (!isLoopStatement(path.node)) return;
111
+
112
+ // Count nesting depth
113
+ let depth = 0;
114
+ let current = path;
115
+ while (current) {
116
+ if (isLoopStatement(current.node)) {
117
+ depth++;
118
+ }
119
+ current = current.parentPath;
120
+ }
121
+
122
+ // Only flag if 3 or more levels deep
123
+ if (depth < 3) return;
124
+
125
+ const loc = path.node.loc?.start;
126
+ if (!loc) return;
127
+
128
+ findings.push({
129
+ type: "nested_loop",
130
+ severity: "WARN",
131
+ category: "Performance",
132
+ file: filePath,
133
+ line: loc.line,
134
+ column: loc.column,
135
+ title: `Deeply nested loop (${depth} levels)`,
136
+ message: "Deeply nested loops can be expensive. Consider optimizing or using better data structures.",
137
+ codeSnippet: snippetForLine(lines, loc.line),
138
+ confidence: "med",
139
+ });
140
+ path.skip(); // avoid multiple nested loop reports in same subtree
141
+ },
142
+
143
+ // Event listeners without cleanup - only flag in component-like contexts
144
+ CallExpression(path) {
145
+ const callee = path.node.callee;
146
+ if (!t.isMemberExpression(callee)) return;
147
+ if (callee.computed) return;
148
+ if (!t.isIdentifier(callee.property, { name: "addEventListener" })) return;
149
+
150
+ const loc = path.node.loc?.start;
151
+ if (!loc) return;
152
+
153
+ // Skip if not in a component-like context (React, Vue, etc.)
154
+ const fn = path.getFunctionParent();
155
+ if (!fn) return;
156
+
157
+ // Only flag in component-like functions or useEffect-like hooks
158
+ const fnName = fn.node.id?.name || (fn.parent?.id?.name);
159
+ const isComponent = fnName && /^[A-Z]/.test(fnName);
160
+ const isEffectHook = fnName && /useEffect|useLayoutEffect|componentDidMount|componentWillUnmount/i.test(fnName);
161
+
162
+ if (!isComponent && !isEffectHook) return;
163
+
164
+ const evName = getEventName(path.node);
165
+ const hasCleanup = hasMatchingRemoveEventListener(fn, evName);
166
+
167
+ if (hasCleanup) return;
168
+
169
+ findings.push({
170
+ type: "potential_memory_leak",
171
+ severity: "WARN",
172
+ category: "Performance",
173
+ file: filePath,
174
+ line: loc.line,
175
+ column: loc.column,
176
+ title: "Event listener without cleanup",
177
+ message: "addEventListener() found without a matching removeEventListener() in the same function scope.",
178
+ codeSnippet: snippetForLine(lines, loc.line),
179
+ confidence: "low",
180
+ });
181
+ },
182
+
183
+ // Expensive operations in render-like contexts
184
+ CallExpression(path) {
185
+ const callee = path.node.callee;
186
+ const loc = path.node.loc?.start;
187
+ if (!loc) return;
188
+
189
+ if (!isLikelyRenderContext(path)) return;
190
+
191
+ // JSON.parse / stringify in render
192
+ if (t.isMemberExpression(callee) && t.isIdentifier(callee.object, { name: "JSON" }) && t.isIdentifier(callee.property)) {
193
+ const m = callee.property.name;
194
+ if (m === "parse" || m === "stringify") {
195
+ findings.push({
196
+ type: "expensive_render_op",
197
+ severity: "INFO",
198
+ category: "Performance",
199
+ file: filePath,
200
+ line: loc.line,
201
+ column: loc.column,
202
+ title: `JSON.${m} in render context`,
203
+ message: "Consider moving JSON parsing/serialization out of render to avoid repeated work.",
204
+ codeSnippet: snippetForLine(lines, loc.line),
205
+ confidence: "low",
206
+ });
207
+ }
208
+ }
209
+
210
+ // Array.sort in render (mutates + expensive)
211
+ if (t.isMemberExpression(callee) && t.isIdentifier(callee.property, { name: "sort" })) {
212
+ findings.push({
213
+ type: "expensive_render_op",
214
+ severity: "INFO",
215
+ category: "Performance",
216
+ file: filePath,
217
+ line: loc.line,
218
+ column: loc.column,
219
+ title: "Array.sort in render context",
220
+ message: "Sorting during render can be expensive and mutates arrays. Consider sorting in memoized/selectors.",
221
+ codeSnippet: snippetForLine(lines, loc.line),
222
+ confidence: "low",
223
+ });
224
+ }
225
+ },
226
+ });
227
+
228
+ return findings;
229
+ }
230
+
231
+ module.exports = {
232
+ analyzePerformanceIssues,
233
+ parseCode,
234
+ };
@@ -0,0 +1,217 @@
1
+ /**
2
+ * Type-Aware Engine (TypeScript focused)
3
+ * Detects risky TypeScript patterns: any, non-null assertions, ts-ignore, unsafe casts, missing return types on exported APIs.
4
+ */
5
+
6
+ const { getAST, parseCode } = require("./ast-cache");
7
+ const traverse = require("@babel/traverse").default;
8
+ const t = require("@babel/types");
9
+ const { shouldExcludeFile, isTestContext, hasIgnoreDirective } = require("./file-filter");
10
+
11
+ function isTSFile(filePath) {
12
+ return filePath.endsWith(".ts") || filePath.endsWith(".tsx");
13
+ }
14
+
15
+ function snippetForLine(lines, line) {
16
+ return lines[line - 1] ? lines[line - 1].trim() : "";
17
+ }
18
+
19
+ function isExportedFunctionPath(path) {
20
+ // export function foo() {}
21
+ if (path.parentPath?.isExportNamedDeclaration?.() || path.parentPath?.isExportDefaultDeclaration?.()) return true;
22
+
23
+ // export const foo = () => {}
24
+ const exportDecl = path.findParent((p) => p.isExportNamedDeclaration?.());
25
+ if (exportDecl) return true;
26
+
27
+ return false;
28
+ }
29
+
30
+ function analyzeTypeAware(code, filePath) {
31
+ const findings = [];
32
+
33
+ if (!isTSFile(filePath)) return findings;
34
+ if (shouldExcludeFile(filePath)) return findings;
35
+ if (isTestContext(code, filePath)) return findings;
36
+ if (hasIgnoreDirective(code, "type-aware")) return findings;
37
+
38
+ const ast = getAST(code, filePath);
39
+ if (!ast) return findings;
40
+
41
+ const lines = code.split("\n");
42
+
43
+ // Scan header comments for ts-ignore
44
+ const comments = ast.comments || [];
45
+ for (const c of comments) {
46
+ const v = (c.value || "").trim();
47
+ if (/^@ts-ignore\b/.test(v) || /^@ts-expect-error\b/.test(v)) {
48
+ const line = c.loc?.start?.line || 1;
49
+ findings.push({
50
+ type: "ts_ignore",
51
+ severity: "WARN",
52
+ category: "TypeSafety",
53
+ file: filePath,
54
+ line,
55
+ column: c.loc?.start?.column || 0,
56
+ title: "TypeScript ignore directive used",
57
+ message: `Found ${v.split(/\s+/)[0]} - this bypasses type safety.`,
58
+ codeSnippet: snippetForLine(lines, line),
59
+ confidence: "high",
60
+ });
61
+ }
62
+ }
63
+
64
+ // General TS AST checks
65
+ traverse(ast, {
66
+ TSAnyKeyword(path) {
67
+ const loc = path.node.loc?.start;
68
+ if (!loc) return;
69
+
70
+ findings.push({
71
+ type: "any_type",
72
+ severity: "WARN",
73
+ category: "TypeSafety",
74
+ file: filePath,
75
+ line: loc.line,
76
+ column: loc.column,
77
+ title: "Usage of 'any' type",
78
+ message: "Avoid 'any' where possible. Prefer unknown + narrowing or a specific type.",
79
+ codeSnippet: snippetForLine(lines, loc.line),
80
+ confidence: "high",
81
+ });
82
+ },
83
+
84
+ TSNonNullExpression(path) {
85
+ const loc = path.node.loc?.start;
86
+ if (!loc) return;
87
+
88
+ findings.push({
89
+ type: "non_null_assertion",
90
+ severity: "WARN",
91
+ category: "TypeSafety",
92
+ file: filePath,
93
+ line: loc.line,
94
+ column: loc.column,
95
+ title: "Non-null assertion used (!)",
96
+ message: "Non-null assertions can hide real null/undefined bugs. Consider proper checks.",
97
+ codeSnippet: snippetForLine(lines, loc.line),
98
+ confidence: "med",
99
+ });
100
+ },
101
+
102
+ TSAsExpression(path) {
103
+ const loc = path.node.loc?.start;
104
+ if (!loc) return;
105
+
106
+ const typeAnn = path.node.typeAnnotation;
107
+ if (t.isTSAnyKeyword(typeAnn)) {
108
+ findings.push({
109
+ type: "unsafe_type_cast",
110
+ severity: "WARN",
111
+ category: "TypeSafety",
112
+ file: filePath,
113
+ line: loc.line,
114
+ column: loc.column,
115
+ title: "Unsafe type cast to 'any'",
116
+ message: "Casting to 'any' disables type checking. Prefer unknown + validation.",
117
+ codeSnippet: snippetForLine(lines, loc.line),
118
+ confidence: "high",
119
+ });
120
+ }
121
+ },
122
+
123
+ FunctionDeclaration(path) {
124
+ if (!path.node.loc) return;
125
+ if (!isExportedFunctionPath(path)) return;
126
+
127
+ // only flag when return type is missing AND the body has at least one return
128
+ const hasReturnType = Boolean(path.node.returnType);
129
+ if (hasReturnType) return;
130
+
131
+ let returnsValue = false;
132
+ path.traverse({
133
+ Function(inner) {
134
+ if (inner !== path) inner.skip();
135
+ },
136
+ ReturnStatement(r) {
137
+ if (r.node.argument) returnsValue = true;
138
+ },
139
+ });
140
+
141
+ if (!returnsValue) return;
142
+
143
+ const line = path.node.loc.start.line;
144
+ findings.push({
145
+ type: "missing_return_type",
146
+ severity: "INFO",
147
+ category: "TypeSafety",
148
+ file: filePath,
149
+ line,
150
+ column: path.node.loc.start.column,
151
+ title: "Exported function missing explicit return type",
152
+ message: "Public/ exported APIs should specify return types for stability and clarity.",
153
+ codeSnippet: snippetForLine(lines, line),
154
+ confidence: "low",
155
+ });
156
+ },
157
+
158
+ ExportNamedDeclaration(path) {
159
+ // export const foo = () => {}
160
+ const decl = path.node.declaration;
161
+ if (!t.isVariableDeclaration(decl)) return;
162
+
163
+ for (const d of decl.declarations) {
164
+ if (!t.isVariableDeclarator(d)) continue;
165
+ if (!t.isIdentifier(d.id) || !d.init) continue;
166
+ if (!t.isArrowFunctionExpression(d.init) && !t.isFunctionExpression(d.init)) continue;
167
+
168
+ const fnNode = d.init;
169
+ const hasReturnType = Boolean(fnNode.returnType);
170
+ if (hasReturnType) continue;
171
+
172
+ // Only if it returns a value
173
+ let returnsValue = false;
174
+ traverse(
175
+ t.file(t.program([t.expressionStatement(fnNode)])),
176
+ {
177
+ ReturnStatement(r) {
178
+ if (r.node.argument) returnsValue = true;
179
+ },
180
+ Function(inner) {
181
+ // skip nested
182
+ inner.skip();
183
+ },
184
+ },
185
+ undefined,
186
+ undefined,
187
+ undefined
188
+ );
189
+
190
+ if (!returnsValue) continue;
191
+
192
+ const loc = fnNode.loc?.start;
193
+ if (!loc) continue;
194
+
195
+ findings.push({
196
+ type: "missing_return_type",
197
+ severity: "INFO",
198
+ category: "TypeSafety",
199
+ file: filePath,
200
+ line: loc.line,
201
+ column: loc.column,
202
+ title: "Exported function missing explicit return type",
203
+ message: `Exported '${d.id.name}' should specify a return type.`,
204
+ codeSnippet: snippetForLine(lines, loc.line),
205
+ confidence: "low",
206
+ });
207
+ }
208
+ },
209
+ });
210
+
211
+ return findings;
212
+ }
213
+
214
+ module.exports = {
215
+ analyzeTypeAware,
216
+ parseCode,
217
+ };
@@ -0,0 +1,78 @@
1
+ /**
2
+ * Unsafe Regex Engine
3
+ * Detects potentially catastrophic backtracking patterns.
4
+ *
5
+ * This is heuristic-based. It aims to flag the riskiest regexes:
6
+ * - Nested quantifiers like (.+)+, (.*)*, (a|aa)+, ([\s\S]*)+
7
+ * - Ambiguous repetitions inside groups: (a|a?)+ etc
8
+ */
9
+
10
+ const { getAST, parseCode } = require("./ast-cache");
11
+ const traverse = require("@babel/traverse").default;
12
+ const t = require("@babel/types");
13
+ const { shouldExcludeFile, isTestContext, hasIgnoreDirective } = require("./file-filter");
14
+
15
+ function snippetForLine(lines, line) {
16
+ return lines[line - 1] ? lines[line - 1].trim() : "";
17
+ }
18
+
19
+ function isRiskyPattern(pattern) {
20
+ if (!pattern || typeof pattern !== "string") return false;
21
+
22
+ // Nested quantifiers: (.*)+, (.+)+, (.*)*, (.+)*
23
+ if (/\((?:[^()\\]|\\.)*([+*])\)\s*[+*]/.test(pattern)) return true;
24
+
25
+ // Very broad character classes repeated aggressively
26
+ if (/\[(?:\\s\\S|\\S\\s|\\w\\W|\\W\\w|\\d\\D|\\D\\d)\]\s*[+*]\s*[+*]/.test(pattern)) return true;
27
+
28
+ // Alternation inside repetition: (a|aa)+ or (.*|.+)+
29
+ if (/\((?:[^()\\]|\\.)*\|(?:[^()\\]|\\.)*\)\s*[+*]/.test(pattern)) return true;
30
+
31
+ // Repetition of ambiguous wildcard with backreferences (commonly risky)
32
+ if (/(\.\*|\.\+).*\1/.test(pattern)) return true;
33
+
34
+ return false;
35
+ }
36
+
37
+ function analyzeUnsafeRegex(code, filePath) {
38
+ const findings = [];
39
+
40
+ if (shouldExcludeFile(filePath)) return findings;
41
+ if (isTestContext(code, filePath)) return findings;
42
+ if (hasIgnoreDirective(code, "unsafe-regex")) return findings;
43
+
44
+ const ast = getAST(code, filePath);
45
+ if (!ast) return findings;
46
+
47
+ const lines = code.split("\n");
48
+
49
+ traverse(ast, {
50
+ RegExpLiteral(path) {
51
+ const pattern = path.node.pattern;
52
+ if (!isRiskyPattern(pattern)) return;
53
+
54
+ const loc = path.node.loc?.start;
55
+ if (!loc) return;
56
+
57
+ findings.push({
58
+ type: "unsafe_regex",
59
+ severity: "WARN",
60
+ category: "Security",
61
+ file: filePath,
62
+ line: loc.line,
63
+ column: loc.column,
64
+ title: "Potential ReDoS risk in regex",
65
+ message: `Regex pattern "${pattern}" may cause catastrophic backtracking.`,
66
+ codeSnippet: snippetForLine(lines, loc.line),
67
+ confidence: "med",
68
+ });
69
+ },
70
+ });
71
+
72
+ return findings;
73
+ }
74
+
75
+ module.exports = {
76
+ analyzeUnsafeRegex,
77
+ parseCode,
78
+ };