-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-ts-errors.cjs
More file actions
executable file
·125 lines (101 loc) · 3 KB
/
fix-ts-errors.cjs
File metadata and controls
executable file
·125 lines (101 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env node
/**
* Script to fix common TypeScript errors in KB-MCP codebase
*/
const fs = require('fs');
const path = require('path');
const fixes = [
// Fix unused variables by prefixing with underscore
{
pattern: /(\w+): error TS6133: '(\w+)' is declared but its value is never read\./,
fix: (content, match) => {
const varName = match[2];
// Don't prefix if already prefixed
if (varName.startsWith('_')) return content;
// Common patterns to fix
const patterns = [
new RegExp(`\\b${varName}\\b(?=\\s*[:=,)])`),
new RegExp(`\\b${varName}\\b(?=\\s*\\})`),
new RegExp(`\\b${varName}\\b(?=\\s*,)`),
new RegExp(`\\b${varName}\\b(?=\\s*\\))`),
];
for (const pattern of patterns) {
content = content.replace(pattern, `_${varName}`);
}
return content;
}
},
// Fix property access errors
{
pattern: /distance\.cosine/g,
replacement: 'distance.cosine || distance.cosineDistance || distance'
},
// Fix missing properties by using optional chaining
{
pattern: /(\w+)\.port/g,
replacement: '$1.port || $1.connection?.port || 3000'
},
// Fix Timer type issues
{
pattern: /clearInterval\((\w+)\)/g,
replacement: 'clearInterval($1 as any)'
},
// Fix undefined assignments
{
pattern: /max: this\.config\.max_connections,/g,
replacement: 'max: this.config.max_connections ?? 10,'
},
{
pattern: /acquireTimeoutMillis: this\.config\.connection_timeout,/g,
replacement: 'acquireTimeoutMillis: this.config.connection_timeout ?? 30000,'
},
];
function fixFile(filePath) {
if (!fs.existsSync(filePath)) return false;
let content = fs.readFileSync(filePath, 'utf8');
let changed = false;
for (const fix of fixes) {
if (fix.pattern && fix.replacement) {
const newContent = content.replace(fix.pattern, fix.replacement);
if (newContent !== content) {
content = newContent;
changed = true;
}
}
}
if (changed) {
fs.writeFileSync(filePath, content);
console.log(`Fixed: ${filePath}`);
return true;
}
return false;
}
// Get all TypeScript files
function getAllTsFiles(dir) {
const files = [];
function traverse(currentDir) {
const items = fs.readdirSync(currentDir);
for (const item of items) {
const fullPath = path.join(currentDir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory() && !item.includes('node_modules') && !item.includes('.git')) {
traverse(fullPath);
} else if (item.endsWith('.ts') && !item.endsWith('.d.ts')) {
files.push(fullPath);
}
}
}
traverse(dir);
return files;
}
// Run fixes
const srcDir = path.join(__dirname, 'src');
const tsFiles = getAllTsFiles(srcDir);
console.log(`Found ${tsFiles.length} TypeScript files`);
let totalFixed = 0;
for (const file of tsFiles) {
if (fixFile(file)) {
totalFixed++;
}
}
console.log(`Fixed ${totalFixed} files`);