-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfix-lint-errors.js
More file actions
86 lines (69 loc) · 2.34 KB
/
fix-lint-errors.js
File metadata and controls
86 lines (69 loc) · 2.34 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
#!/usr/bin/env node
/**
* Auto-fix common ESLint errors in test files
*/
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
// Get all test files
const testDir = path.join(__dirname, 'src', '__tests__');
function fixFile(filePath) {
let content = fs.readFileSync(filePath, 'utf-8');
let modified = false;
// Fix: Remove unused 'vi' import from vitest
if (content.includes("import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';")) {
const viUsed = content.includes('vi.') || content.includes('vi(');
if (!viUsed) {
content = content.replace(
"import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';",
"import { describe, it, expect, beforeEach, afterEach } from 'vitest';"
);
modified = true;
}
}
// Fix: Add String() wrapper for template literals with numbers
content = content.replace(/\$\{(\d+|i|index)\}/g, (match, num) => {
if (/^\d+$/.test(num)) return match; // Already a literal number
return `\${String(${num})}`;
});
// Fix: Remove 'async' from arrow functions with no 'await'
// This is more complex and requires AST parsing, skip for now
// Fix: Add type assertions for error types
content = content.replace(/catch \(error\)/g, 'catch (error: unknown)');
if (modified || content !== fs.readFileSync(filePath, 'utf-8')) {
fs.writeFileSync(filePath, content);
console.log(`Fixed: ${filePath}`);
return true;
}
return false;
}
// Recursively find all .ts and .tsx files
function findTestFiles(dir) {
const files = [];
const items = fs.readdirSync(dir);
for (const item of items) {
const fullPath = path.join(dir, item);
const stat = fs.statSync(fullPath);
if (stat.isDirectory()) {
files.push(...findTestFiles(fullPath));
} else if (item.endsWith('.test.ts') || item.endsWith('.test.tsx')) {
files.push(fullPath);
}
}
return files;
}
const testFiles = findTestFiles(testDir);
let fixedCount = 0;
for (const file of testFiles) {
if (fixFile(file)) {
fixedCount++;
}
}
console.log(`\nFixed ${fixedCount} files`);
console.log('\nRunning ESLint with --fix...');
try {
execSync('npm run lint -- --fix', { stdio: 'inherit' });
} catch (error) {
console.log('ESLint --fix completed with errors (expected)');
}
console.log('\nDone!');