@vibecheckai/cli 3.3.0 → 3.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.
Files changed (170) hide show
  1. package/bin/registry.js +389 -269
  2. package/bin/runners/cli-utils.js +2 -33
  3. package/bin/runners/context/generators/cursor.js +49 -2
  4. package/bin/runners/lib/agent-firewall/learning/learning-engine.js +849 -0
  5. package/bin/runners/lib/analyzers.js +599 -142
  6. package/bin/runners/lib/audit-logger.js +532 -0
  7. package/bin/runners/lib/authority/authorities/architecture.js +364 -0
  8. package/bin/runners/lib/authority/authorities/compliance.js +341 -0
  9. package/bin/runners/lib/authority/authorities/human.js +343 -0
  10. package/bin/runners/lib/authority/authorities/quality.js +420 -0
  11. package/bin/runners/lib/authority/authorities/security.js +228 -0
  12. package/bin/runners/lib/authority/index.js +293 -0
  13. package/bin/runners/lib/authority-badge.js +425 -425
  14. package/bin/runners/lib/bundle/bundle-intelligence.js +846 -0
  15. package/bin/runners/lib/cli-charts.js +368 -0
  16. package/bin/runners/lib/cli-config-display.js +405 -0
  17. package/bin/runners/lib/cli-demo.js +275 -0
  18. package/bin/runners/lib/cli-errors.js +438 -0
  19. package/bin/runners/lib/cli-help-formatter.js +439 -0
  20. package/bin/runners/lib/cli-interactive-menu.js +509 -0
  21. package/bin/runners/lib/cli-prompts.js +441 -0
  22. package/bin/runners/lib/cli-scan-cards.js +362 -0
  23. package/bin/runners/lib/compliance-reporter.js +710 -0
  24. package/bin/runners/lib/conductor/index.js +671 -0
  25. package/bin/runners/lib/easy/README.md +123 -0
  26. package/bin/runners/lib/easy/index.js +140 -0
  27. package/bin/runners/lib/easy/interactive-wizard.js +788 -0
  28. package/bin/runners/lib/easy/one-click-firewall.js +564 -0
  29. package/bin/runners/lib/easy/zero-config-reality.js +714 -0
  30. package/bin/runners/lib/engines/accessibility-engine.js +218 -18
  31. package/bin/runners/lib/engines/api-consistency-engine.js +335 -30
  32. package/bin/runners/lib/engines/async-patterns-engine.js +444 -0
  33. package/bin/runners/lib/engines/bundle-size-engine.js +433 -0
  34. package/bin/runners/lib/engines/confidence-scoring.js +276 -0
  35. package/bin/runners/lib/engines/context-detection.js +264 -0
  36. package/bin/runners/lib/engines/cross-file-analysis-engine.js +292 -27
  37. package/bin/runners/lib/engines/database-patterns-engine.js +429 -0
  38. package/bin/runners/lib/engines/duplicate-code-engine.js +354 -0
  39. package/bin/runners/lib/engines/empty-catch-engine.js +127 -17
  40. package/bin/runners/lib/engines/env-variables-engine.js +458 -0
  41. package/bin/runners/lib/engines/error-handling-engine.js +437 -0
  42. package/bin/runners/lib/engines/false-positive-prevention.js +630 -0
  43. package/bin/runners/lib/engines/framework-adapters/index.js +607 -0
  44. package/bin/runners/lib/engines/framework-detection.js +508 -0
  45. package/bin/runners/lib/engines/import-order-engine.js +429 -0
  46. package/bin/runners/lib/engines/mock-data-engine.js +53 -10
  47. package/bin/runners/lib/engines/naming-conventions-engine.js +544 -0
  48. package/bin/runners/lib/engines/noise-reduction-engine.js +452 -0
  49. package/bin/runners/lib/engines/orchestrator.js +334 -0
  50. package/bin/runners/lib/engines/performance-issues-engine.js +176 -36
  51. package/bin/runners/lib/engines/react-patterns-engine.js +457 -0
  52. package/bin/runners/lib/engines/security-vulnerabilities-engine.js +382 -54
  53. package/bin/runners/lib/engines/type-aware-engine.js +263 -39
  54. package/bin/runners/lib/engines/vibecheck-engines/index.js +122 -13
  55. package/bin/runners/lib/engines/vibecheck-engines/lib/ai-hallucination-engine.js +806 -0
  56. package/bin/runners/lib/engines/vibecheck-engines/lib/hardcoded-secrets-engine.js +373 -73
  57. package/bin/runners/lib/engines/vibecheck-engines/lib/smart-fix-engine.js +577 -0
  58. package/bin/runners/lib/engines/vibecheck-engines/lib/vibe-score-engine.js +543 -0
  59. package/bin/runners/lib/engines/vibecheck-engines.js +514 -0
  60. package/bin/runners/lib/enhanced-features/index.js +305 -0
  61. package/bin/runners/lib/enhanced-output.js +631 -0
  62. package/bin/runners/lib/enterprise.js +300 -0
  63. package/bin/runners/lib/entitlements-v2.js +161 -478
  64. package/bin/runners/lib/firewall/command-validator.js +351 -0
  65. package/bin/runners/lib/firewall/config.js +341 -0
  66. package/bin/runners/lib/firewall/content-validator.js +519 -0
  67. package/bin/runners/lib/firewall/index.js +101 -0
  68. package/bin/runners/lib/firewall/path-validator.js +256 -0
  69. package/bin/runners/lib/html-proof-report.js +350 -700
  70. package/bin/runners/lib/intelligence/cross-repo-intelligence.js +817 -0
  71. package/bin/runners/lib/mcp-utils.js +425 -0
  72. package/bin/runners/lib/missions/plan.js +46 -6
  73. package/bin/runners/lib/missions/templates.js +232 -0
  74. package/bin/runners/lib/output/index.js +1022 -0
  75. package/bin/runners/lib/policy-engine.js +652 -0
  76. package/bin/runners/lib/polish/autofix/accessibility-fixes.js +333 -0
  77. package/bin/runners/lib/polish/autofix/async-handlers.js +273 -0
  78. package/bin/runners/lib/polish/autofix/dead-code.js +280 -0
  79. package/bin/runners/lib/polish/autofix/imports-optimizer.js +344 -0
  80. package/bin/runners/lib/polish/autofix/index.js +200 -0
  81. package/bin/runners/lib/polish/autofix/remove-consoles.js +209 -0
  82. package/bin/runners/lib/polish/autofix/strengthen-types.js +245 -0
  83. package/bin/runners/lib/polish/backend-checks.js +148 -0
  84. package/bin/runners/lib/polish/documentation-checks.js +111 -0
  85. package/bin/runners/lib/polish/frontend-checks.js +168 -0
  86. package/bin/runners/lib/polish/index.js +71 -0
  87. package/bin/runners/lib/polish/infrastructure-checks.js +131 -0
  88. package/bin/runners/lib/polish/library-detection.js +175 -0
  89. package/bin/runners/lib/polish/performance-checks.js +100 -0
  90. package/bin/runners/lib/polish/security-checks.js +148 -0
  91. package/bin/runners/lib/polish/utils.js +203 -0
  92. package/bin/runners/lib/prompt-builder.js +540 -0
  93. package/bin/runners/lib/proof-certificate.js +634 -0
  94. package/bin/runners/lib/reality/accessibility-audit.js +946 -0
  95. package/bin/runners/lib/reality/api-contract-validator.js +1012 -0
  96. package/bin/runners/lib/reality/chaos-engineering.js +1084 -0
  97. package/bin/runners/lib/reality/performance-tracker.js +1077 -0
  98. package/bin/runners/lib/reality/scenario-generator.js +1404 -0
  99. package/bin/runners/lib/reality/visual-regression.js +852 -0
  100. package/bin/runners/lib/reality-profiler.js +717 -0
  101. package/bin/runners/lib/replay/flight-recorder-viewer.js +1160 -0
  102. package/bin/runners/lib/review/ai-code-review.js +832 -0
  103. package/bin/runners/lib/rules/custom-rule-engine.js +985 -0
  104. package/bin/runners/lib/sbom-generator.js +641 -0
  105. package/bin/runners/lib/scan-output-enhanced.js +512 -0
  106. package/bin/runners/lib/scan-output.js +65 -19
  107. package/bin/runners/lib/security/owasp-scanner.js +939 -0
  108. package/bin/runners/lib/ship-output.js +18 -25
  109. package/bin/runners/lib/terminal-ui.js +113 -1
  110. package/bin/runners/lib/unified-cli-output.js +603 -430
  111. package/bin/runners/lib/upsell.js +90 -338
  112. package/bin/runners/lib/validators/contract-validator.js +283 -0
  113. package/bin/runners/lib/validators/dead-export-detector.js +279 -0
  114. package/bin/runners/lib/validators/dep-audit.js +245 -0
  115. package/bin/runners/lib/validators/env-validator.js +319 -0
  116. package/bin/runners/lib/validators/index.js +120 -0
  117. package/bin/runners/lib/validators/license-checker.js +252 -0
  118. package/bin/runners/lib/validators/route-validator.js +290 -0
  119. package/bin/runners/runAIAgent.js +5 -10
  120. package/bin/runners/runAgent.js +3 -0
  121. package/bin/runners/runApprove.js +1233 -1200
  122. package/bin/runners/runAuth.js +22 -1
  123. package/bin/runners/runAuthority.js +528 -0
  124. package/bin/runners/runCheckpoint.js +4 -24
  125. package/bin/runners/runClassify.js +862 -859
  126. package/bin/runners/runConductor.js +772 -0
  127. package/bin/runners/runContainer.js +366 -0
  128. package/bin/runners/runContext.js +3 -0
  129. package/bin/runners/runDoctor.js +28 -41
  130. package/bin/runners/runEasy.js +410 -0
  131. package/bin/runners/runFirewall.js +3 -0
  132. package/bin/runners/runFirewallHook.js +3 -0
  133. package/bin/runners/runFix.js +76 -66
  134. package/bin/runners/runGuard.js +411 -18
  135. package/bin/runners/runIaC.js +372 -0
  136. package/bin/runners/runInit.js +10 -60
  137. package/bin/runners/runMcp.js +11 -12
  138. package/bin/runners/runPolish.js +240 -64
  139. package/bin/runners/runPromptFirewall.js +5 -12
  140. package/bin/runners/runProve.js +20 -55
  141. package/bin/runners/runReality.js +68 -59
  142. package/bin/runners/runReport.js +31 -5
  143. package/bin/runners/runRuntime.js +5 -8
  144. package/bin/runners/runScan.js +194 -1273
  145. package/bin/runners/runShip.js +695 -47
  146. package/bin/runners/runTruth.js +3 -0
  147. package/bin/runners/runValidate.js +7 -11
  148. package/bin/runners/runVibe.js +791 -0
  149. package/bin/runners/runWatch.js +14 -23
  150. package/bin/vibecheck.js +179 -65
  151. package/mcp-server/index.js +202 -636
  152. package/mcp-server/lib/api-client.cjs +7 -299
  153. package/mcp-server/package.json +1 -1
  154. package/mcp-server/tier-auth.js +175 -574
  155. package/mcp-server/tools-v3.js +800 -505
  156. package/mcp-server/tools.js +495 -0
  157. package/package.json +1 -1
  158. package/bin/runners/lib/engines/vibecheck-engines/lib/ast-cache.js +0 -164
  159. package/bin/runners/lib/engines/vibecheck-engines/lib/code-quality-engine.js +0 -291
  160. package/bin/runners/lib/engines/vibecheck-engines/lib/console-logs-engine.js +0 -83
  161. package/bin/runners/lib/engines/vibecheck-engines/lib/dead-code-engine.js +0 -198
  162. package/bin/runners/lib/engines/vibecheck-engines/lib/deprecated-api-engine.js +0 -275
  163. package/bin/runners/lib/engines/vibecheck-engines/lib/empty-catch-engine.js +0 -167
  164. package/bin/runners/lib/engines/vibecheck-engines/lib/file-filter.js +0 -217
  165. package/bin/runners/lib/engines/vibecheck-engines/lib/mock-data-engine.js +0 -140
  166. package/bin/runners/lib/engines/vibecheck-engines/lib/parallel-processor.js +0 -164
  167. package/bin/runners/lib/engines/vibecheck-engines/lib/performance-issues-engine.js +0 -234
  168. package/bin/runners/lib/engines/vibecheck-engines/lib/type-aware-engine.js +0 -217
  169. package/bin/runners/lib/engines/vibecheck-engines/lib/unsafe-regex-engine.js +0 -78
  170. package/mcp-server/index-v1.js +0 -698
@@ -0,0 +1,200 @@
1
+ /**
2
+ * Polish Auto-Fix Passes
3
+ *
4
+ * Collection of automated code fixes for production readiness.
5
+ * Each pass transforms code using Babel AST without requiring an LLM.
6
+ *
7
+ * Usage:
8
+ * vibecheck polish # Dry-run (show what would change)
9
+ * vibecheck polish --apply # Apply changes
10
+ * vibecheck polish --passes remove-consoles,dead-code
11
+ */
12
+
13
+ "use strict";
14
+
15
+ const { removeConsoles } = require("./remove-consoles");
16
+ const { strengthenTypes } = require("./strengthen-types");
17
+ const { wrapAsyncHandlers } = require("./async-handlers");
18
+ const { removeDeadCode } = require("./dead-code");
19
+ const { fixAccessibility } = require("./accessibility-fixes");
20
+ const { optimizeImports } = require("./imports-optimizer");
21
+
22
+ /**
23
+ * All available polish passes
24
+ * Order matters - some passes should run before others
25
+ */
26
+ const POLISH_PASSES = [
27
+ {
28
+ id: "remove-consoles",
29
+ name: "Remove console statements",
30
+ description: "Remove console.log/debug/info (keep error/warn)",
31
+ fn: removeConsoles,
32
+ category: "cleanup",
33
+ safe: true, // Safe to auto-apply
34
+ },
35
+ {
36
+ id: "dead-code",
37
+ name: "Remove dead code",
38
+ description: "Remove unused imports and unreachable code",
39
+ fn: removeDeadCode,
40
+ category: "cleanup",
41
+ safe: true,
42
+ },
43
+ {
44
+ id: "optimize-imports",
45
+ name: "Optimize imports",
46
+ description: "Sort, dedupe, and organize imports",
47
+ fn: optimizeImports,
48
+ category: "style",
49
+ safe: true,
50
+ },
51
+ {
52
+ id: "strengthen-types",
53
+ name: "Strengthen types",
54
+ description: "Replace `any` with inferred types where possible",
55
+ fn: strengthenTypes,
56
+ category: "types",
57
+ safe: false, // Review recommended
58
+ },
59
+ {
60
+ id: "async-handlers",
61
+ name: "Wrap async handlers",
62
+ description: "Add try-catch to unhandled async functions",
63
+ fn: wrapAsyncHandlers,
64
+ category: "error-handling",
65
+ safe: false, // Review recommended
66
+ },
67
+ {
68
+ id: "accessibility",
69
+ name: "Fix accessibility",
70
+ description: "Add missing alt, aria-label, and role attributes",
71
+ fn: fixAccessibility,
72
+ category: "accessibility",
73
+ safe: false, // Review recommended - may add empty placeholders
74
+ },
75
+ ];
76
+
77
+ /**
78
+ * Get all available passes
79
+ */
80
+ function getAvailablePasses() {
81
+ return POLISH_PASSES.map(({ id, name, description, category, safe }) => ({
82
+ id,
83
+ name,
84
+ description,
85
+ category,
86
+ safe,
87
+ }));
88
+ }
89
+
90
+ /**
91
+ * Get passes by category
92
+ */
93
+ function getPassesByCategory(category) {
94
+ return POLISH_PASSES.filter((p) => p.category === category);
95
+ }
96
+
97
+ /**
98
+ * Get safe passes only (can be auto-applied)
99
+ */
100
+ function getSafePasses() {
101
+ return POLISH_PASSES.filter((p) => p.safe);
102
+ }
103
+
104
+ /**
105
+ * Run specified polish passes
106
+ *
107
+ * @param {string} targetPath - Directory to process
108
+ * @param {Object} options
109
+ * @param {boolean} options.dryRun - If true, don't modify files
110
+ * @param {string[]} options.passes - Pass IDs to run (default: all)
111
+ * @param {boolean} options.safeOnly - Only run safe passes
112
+ * @returns {Object} Results from all passes
113
+ */
114
+ async function runPolishPasses(targetPath, options = {}) {
115
+ const {
116
+ dryRun = true,
117
+ passes = null,
118
+ safeOnly = false,
119
+ verbose = false,
120
+ } = options;
121
+
122
+ // Determine which passes to run
123
+ let passesToRun = POLISH_PASSES;
124
+
125
+ if (safeOnly) {
126
+ passesToRun = passesToRun.filter((p) => p.safe);
127
+ }
128
+
129
+ if (passes && passes.length > 0) {
130
+ passesToRun = passesToRun.filter((p) => passes.includes(p.id));
131
+ }
132
+
133
+ const results = {
134
+ passes: [],
135
+ totalChanges: 0,
136
+ totalFilesModified: new Set(),
137
+ errors: [],
138
+ dryRun,
139
+ };
140
+
141
+ for (const pass of passesToRun) {
142
+ const startTime = Date.now();
143
+
144
+ try {
145
+ const passResult = await pass.fn(targetPath, { dryRun });
146
+
147
+ results.passes.push({
148
+ id: pass.id,
149
+ name: pass.name,
150
+ changes: passResult.changes.length,
151
+ files: passResult.files,
152
+ errors: passResult.errors || [],
153
+ duration: Date.now() - startTime,
154
+ summary: passResult.summary,
155
+ });
156
+
157
+ results.totalChanges += passResult.changes.length;
158
+ passResult.files.forEach((f) => results.totalFilesModified.add(f));
159
+
160
+ if (passResult.errors?.length > 0) {
161
+ results.errors.push(...passResult.errors.map((e) => ({ pass: pass.id, ...e })));
162
+ }
163
+ } catch (err) {
164
+ results.passes.push({
165
+ id: pass.id,
166
+ name: pass.name,
167
+ changes: 0,
168
+ files: [],
169
+ errors: [{ error: err.message }],
170
+ duration: Date.now() - startTime,
171
+ failed: true,
172
+ });
173
+
174
+ results.errors.push({
175
+ pass: pass.id,
176
+ error: err.message,
177
+ });
178
+ }
179
+ }
180
+
181
+ // Convert Set to Array
182
+ results.totalFilesModified = Array.from(results.totalFilesModified);
183
+
184
+ return results;
185
+ }
186
+
187
+ module.exports = {
188
+ POLISH_PASSES,
189
+ runPolishPasses,
190
+ getAvailablePasses,
191
+ getPassesByCategory,
192
+ getSafePasses,
193
+ // Individual passes
194
+ removeConsoles,
195
+ strengthenTypes,
196
+ wrapAsyncHandlers,
197
+ removeDeadCode,
198
+ fixAccessibility,
199
+ optimizeImports,
200
+ };
@@ -0,0 +1,209 @@
1
+ /**
2
+ * Remove Console Statements - Auto-fix Polish Pass
3
+ *
4
+ * Removes console.log, console.debug, console.info, console.trace
5
+ * Preserves console.error, console.warn (these are intentional)
6
+ *
7
+ * Uses Babel AST transformation for safe, accurate modifications.
8
+ */
9
+
10
+ "use strict";
11
+
12
+ const { parse } = require("@babel/parser");
13
+ const traverse = require("@babel/traverse").default || require("@babel/traverse");
14
+ const generate = require("@babel/generator").default || require("@babel/generator");
15
+ const fs = require("fs");
16
+ const path = require("path");
17
+ const { findAllFilesSync, readFileSafeSync } = require("../utils");
18
+
19
+ // Console methods to REMOVE (development/debug)
20
+ const REMOVE_METHODS = new Set([
21
+ "log",
22
+ "debug",
23
+ "info",
24
+ "trace",
25
+ "dir",
26
+ "dirxml",
27
+ "table",
28
+ "count",
29
+ "countReset",
30
+ "group",
31
+ "groupCollapsed",
32
+ "groupEnd",
33
+ "time",
34
+ "timeEnd",
35
+ "timeLog",
36
+ "timeStamp",
37
+ "profile",
38
+ "profileEnd",
39
+ "clear",
40
+ ]);
41
+
42
+ // Console methods to KEEP (production-appropriate)
43
+ const KEEP_METHODS = new Set(["error", "warn", "assert"]);
44
+
45
+ /**
46
+ * Parse JavaScript/TypeScript code into AST
47
+ */
48
+ function parseCode(code, filePath) {
49
+ const isTypeScript = filePath.endsWith(".ts") || filePath.endsWith(".tsx");
50
+ const isJSX = filePath.endsWith(".jsx") || filePath.endsWith(".tsx");
51
+
52
+ const plugins = ["decorators-legacy", "classProperties", "classPrivateProperties", "classPrivateMethods"];
53
+
54
+ if (isTypeScript) {
55
+ plugins.push("typescript");
56
+ }
57
+ if (isJSX || isTypeScript) {
58
+ plugins.push("jsx");
59
+ }
60
+
61
+ return parse(code, {
62
+ sourceType: "module",
63
+ plugins,
64
+ errorRecovery: true, // Continue parsing even with errors
65
+ });
66
+ }
67
+
68
+ /**
69
+ * Remove console statements from a single file
70
+ * @param {string} filePath - Path to file
71
+ * @param {Object} options - { dryRun: boolean }
72
+ * @returns {Object} - { changes: Array, modified: boolean }
73
+ */
74
+ function processFile(filePath, options = {}) {
75
+ const { dryRun = true } = options;
76
+ const changes = [];
77
+
78
+ const code = readFileSafeSync(filePath);
79
+ if (!code) return { changes: [], modified: false };
80
+
81
+ // Skip if file doesn't contain console
82
+ if (!code.includes("console.")) {
83
+ return { changes: [], modified: false };
84
+ }
85
+
86
+ let ast;
87
+ try {
88
+ ast = parseCode(code, filePath);
89
+ } catch (err) {
90
+ // Can't parse, skip file
91
+ return { changes: [], modified: false, error: `Parse error: ${err.message}` };
92
+ }
93
+
94
+ let modified = false;
95
+ const relativePath = path.relative(process.cwd(), filePath);
96
+
97
+ traverse(ast, {
98
+ CallExpression(nodePath) {
99
+ const { node } = nodePath;
100
+
101
+ // Check for console.method() pattern
102
+ if (
103
+ node.callee.type === "MemberExpression" &&
104
+ node.callee.object.type === "Identifier" &&
105
+ node.callee.object.name === "console" &&
106
+ node.callee.property.type === "Identifier"
107
+ ) {
108
+ const method = node.callee.property.name;
109
+
110
+ // Should we remove this method?
111
+ if (REMOVE_METHODS.has(method)) {
112
+ const line = node.loc?.start?.line || "?";
113
+ const column = node.loc?.start?.column || "?";
114
+
115
+ changes.push({
116
+ file: relativePath,
117
+ line,
118
+ column,
119
+ type: "remove-console",
120
+ detail: `console.${method}`,
121
+ code: code.substring(node.start, Math.min(node.end, node.start + 60)),
122
+ });
123
+
124
+ // Remove the entire expression statement
125
+ if (nodePath.parentPath.type === "ExpressionStatement") {
126
+ nodePath.parentPath.remove();
127
+ } else {
128
+ // If it's used as a value (rare), replace with undefined
129
+ nodePath.replaceWith({ type: "Identifier", name: "undefined" });
130
+ }
131
+
132
+ modified = true;
133
+ }
134
+ }
135
+ },
136
+ });
137
+
138
+ // Write changes if not dry run and file was modified
139
+ if (modified && !dryRun) {
140
+ try {
141
+ const output = generate(
142
+ ast,
143
+ {
144
+ retainLines: true,
145
+ compact: false,
146
+ concise: false,
147
+ },
148
+ code
149
+ );
150
+ fs.writeFileSync(filePath, output.code, "utf8");
151
+ } catch (err) {
152
+ return { changes, modified: false, error: `Generate error: ${err.message}` };
153
+ }
154
+ }
155
+
156
+ return { changes, modified };
157
+ }
158
+
159
+ /**
160
+ * Remove console statements from all files in a directory
161
+ * @param {string} targetPath - Directory to process
162
+ * @param {Object} options - { dryRun: boolean }
163
+ * @returns {Object} - { changes: Array, files: Array }
164
+ */
165
+ async function removeConsoles(targetPath, options = {}) {
166
+ const { dryRun = true } = options;
167
+
168
+ // Find all JavaScript/TypeScript files
169
+ const pattern = /\.(js|jsx|ts|tsx|mjs|cjs)$/;
170
+ const files = findAllFilesSync(targetPath, pattern);
171
+
172
+ const allChanges = [];
173
+ const modifiedFiles = [];
174
+ const errors = [];
175
+
176
+ for (const file of files) {
177
+ const result = processFile(file, { dryRun });
178
+
179
+ if (result.error) {
180
+ errors.push({ file, error: result.error });
181
+ }
182
+
183
+ if (result.changes.length > 0) {
184
+ allChanges.push(...result.changes);
185
+ }
186
+
187
+ if (result.modified) {
188
+ modifiedFiles.push(path.relative(process.cwd(), file));
189
+ }
190
+ }
191
+
192
+ return {
193
+ changes: allChanges,
194
+ files: modifiedFiles,
195
+ errors,
196
+ summary: {
197
+ filesScanned: files.length,
198
+ filesModified: modifiedFiles.length,
199
+ statementsRemoved: allChanges.length,
200
+ },
201
+ };
202
+ }
203
+
204
+ module.exports = {
205
+ removeConsoles,
206
+ processFile,
207
+ REMOVE_METHODS,
208
+ KEEP_METHODS,
209
+ };
@@ -0,0 +1,245 @@
1
+ /**
2
+ * Strengthen Types - Auto-fix Polish Pass
3
+ *
4
+ * Replaces `any` types with more specific types where inferrable:
5
+ * - Function parameters with usage patterns
6
+ * - Return types from return statements
7
+ * - Variable declarations with assignments
8
+ *
9
+ * Uses Babel AST transformation with TypeScript plugin.
10
+ */
11
+
12
+ "use strict";
13
+
14
+ const { parse } = require("@babel/parser");
15
+ const traverse = require("@babel/traverse").default || require("@babel/traverse");
16
+ const generate = require("@babel/generator").default || require("@babel/generator");
17
+ const t = require("@babel/types");
18
+ const fs = require("fs");
19
+ const path = require("path");
20
+ const { findAllFilesSync, readFileSafeSync } = require("../utils");
21
+
22
+ /**
23
+ * Infer type from a value expression
24
+ */
25
+ function inferTypeFromValue(node) {
26
+ if (!node) return null;
27
+
28
+ switch (node.type) {
29
+ case "StringLiteral":
30
+ case "TemplateLiteral":
31
+ return t.tsStringKeyword();
32
+
33
+ case "NumericLiteral":
34
+ return t.tsNumberKeyword();
35
+
36
+ case "BooleanLiteral":
37
+ return t.tsBooleanKeyword();
38
+
39
+ case "NullLiteral":
40
+ return t.tsNullKeyword();
41
+
42
+ case "ArrayExpression":
43
+ if (node.elements.length > 0) {
44
+ const elementType = inferTypeFromValue(node.elements[0]);
45
+ if (elementType) {
46
+ return t.tsArrayType(elementType);
47
+ }
48
+ }
49
+ return t.tsArrayType(t.tsUnknownKeyword());
50
+
51
+ case "ObjectExpression":
52
+ return t.tsTypeLiteral([]);
53
+
54
+ case "ArrowFunctionExpression":
55
+ case "FunctionExpression":
56
+ return t.tsFunctionType(null, [], t.tsVoidKeyword());
57
+
58
+ case "CallExpression":
59
+ // Common patterns
60
+ if (node.callee.type === "Identifier") {
61
+ if (node.callee.name === "Promise") {
62
+ return t.tsTypeReference(t.identifier("Promise"), t.tsTypeParameterInstantiation([t.tsUnknownKeyword()]));
63
+ }
64
+ }
65
+ return null;
66
+
67
+ case "NewExpression":
68
+ if (node.callee.type === "Identifier") {
69
+ const typeName = node.callee.name;
70
+ if (["Date", "Map", "Set", "WeakMap", "WeakSet", "RegExp", "Error"].includes(typeName)) {
71
+ return t.tsTypeReference(t.identifier(typeName));
72
+ }
73
+ }
74
+ return null;
75
+
76
+ default:
77
+ return null;
78
+ }
79
+ }
80
+
81
+ /**
82
+ * Parse TypeScript code into AST
83
+ */
84
+ function parseCode(code, filePath) {
85
+ return parse(code, {
86
+ sourceType: "module",
87
+ plugins: ["typescript", "jsx", "decorators-legacy", "classProperties"],
88
+ errorRecovery: true,
89
+ });
90
+ }
91
+
92
+ /**
93
+ * Process a single TypeScript file
94
+ */
95
+ function processFile(filePath, options = {}) {
96
+ const { dryRun = true } = options;
97
+ const changes = [];
98
+
99
+ // Only process TypeScript files
100
+ if (!filePath.endsWith(".ts") && !filePath.endsWith(".tsx")) {
101
+ return { changes: [], modified: false };
102
+ }
103
+
104
+ const code = readFileSafeSync(filePath);
105
+ if (!code) return { changes: [], modified: false };
106
+
107
+ // Quick check - does file contain 'any'
108
+ if (!code.includes(": any") && !code.includes(":any")) {
109
+ return { changes: [], modified: false };
110
+ }
111
+
112
+ let ast;
113
+ try {
114
+ ast = parseCode(code, filePath);
115
+ } catch (err) {
116
+ return { changes: [], modified: false, error: `Parse error: ${err.message}` };
117
+ }
118
+
119
+ let modified = false;
120
+ const relativePath = path.relative(process.cwd(), filePath);
121
+
122
+ traverse(ast, {
123
+ // Variable declarations: const x: any = "value" -> const x: string = "value"
124
+ VariableDeclarator(nodePath) {
125
+ const { node } = nodePath;
126
+
127
+ if (
128
+ node.id.type === "Identifier" &&
129
+ node.id.typeAnnotation?.typeAnnotation?.type === "TSAnyKeyword" &&
130
+ node.init
131
+ ) {
132
+ const inferredType = inferTypeFromValue(node.init);
133
+ if (inferredType) {
134
+ const line = node.loc?.start?.line || "?";
135
+ changes.push({
136
+ file: relativePath,
137
+ line,
138
+ type: "strengthen-type",
139
+ detail: `${node.id.name}: any -> inferred type`,
140
+ from: "any",
141
+ to: generate(inferredType).code,
142
+ });
143
+
144
+ node.id.typeAnnotation.typeAnnotation = inferredType;
145
+ modified = true;
146
+ }
147
+ }
148
+ },
149
+
150
+ // Function parameters: Replace (x: any) with (x: unknown) as safer default
151
+ TSAnyKeyword(nodePath) {
152
+ const parent = nodePath.parent;
153
+ const grandParent = nodePath.parentPath?.parent;
154
+
155
+ // Skip if it's in a type assertion or generic
156
+ if (parent.type === "TSTypeAssertion" || parent.type === "TSAsExpression") {
157
+ return;
158
+ }
159
+
160
+ // For parameters without clear inference, use `unknown` (safer than `any`)
161
+ if (
162
+ parent.type === "TSTypeAnnotation" &&
163
+ grandParent?.type === "Identifier" &&
164
+ nodePath.parentPath?.parentPath?.parent?.type === "FunctionDeclaration"
165
+ ) {
166
+ const line = nodePath.node.loc?.start?.line || "?";
167
+
168
+ // Only replace if it's a standalone `any` parameter
169
+ changes.push({
170
+ file: relativePath,
171
+ line,
172
+ type: "strengthen-type",
173
+ detail: `Parameter type: any -> unknown`,
174
+ suggestion: "Consider using a more specific type",
175
+ from: "any",
176
+ to: "unknown",
177
+ });
178
+
179
+ // Replace any with unknown (safer)
180
+ nodePath.replaceWith(t.tsUnknownKeyword());
181
+ modified = true;
182
+ }
183
+ },
184
+ });
185
+
186
+ // Write changes if not dry run
187
+ if (modified && !dryRun) {
188
+ try {
189
+ const output = generate(ast, { retainLines: true }, code);
190
+ fs.writeFileSync(filePath, output.code, "utf8");
191
+ } catch (err) {
192
+ return { changes, modified: false, error: `Generate error: ${err.message}` };
193
+ }
194
+ }
195
+
196
+ return { changes, modified };
197
+ }
198
+
199
+ /**
200
+ * Strengthen types in all TypeScript files
201
+ */
202
+ async function strengthenTypes(targetPath, options = {}) {
203
+ const { dryRun = true } = options;
204
+
205
+ // Only TypeScript files
206
+ const pattern = /\.(ts|tsx)$/;
207
+ const files = findAllFilesSync(targetPath, pattern);
208
+
209
+ const allChanges = [];
210
+ const modifiedFiles = [];
211
+ const errors = [];
212
+
213
+ for (const file of files) {
214
+ const result = processFile(file, { dryRun });
215
+
216
+ if (result.error) {
217
+ errors.push({ file, error: result.error });
218
+ }
219
+
220
+ if (result.changes.length > 0) {
221
+ allChanges.push(...result.changes);
222
+ }
223
+
224
+ if (result.modified) {
225
+ modifiedFiles.push(path.relative(process.cwd(), file));
226
+ }
227
+ }
228
+
229
+ return {
230
+ changes: allChanges,
231
+ files: modifiedFiles,
232
+ errors,
233
+ summary: {
234
+ filesScanned: files.length,
235
+ filesModified: modifiedFiles.length,
236
+ typesStrengthened: allChanges.length,
237
+ },
238
+ };
239
+ }
240
+
241
+ module.exports = {
242
+ strengthenTypes,
243
+ processFile,
244
+ inferTypeFromValue,
245
+ };