Skip to content

Commit 558619f

Browse files
committed
Add pre-commit hook to auto-remove ESLint warnings
When developers modify files that are in the ESLint warnings list, this pre-commit hook automatically removes those files from the warnings list in eslint.config.mjs. This enforces that any touched file must have its ESLint errors fixed before committing. The update-eslint-warnings script runs before lint-staged in the pre-commit hook, ensuring modified files are subject to full ESLint error checking rather than just warnings. This helps gradually clean up the codebase as developers work on different files.
1 parent 95640b9 commit 558619f

File tree

2 files changed

+114
-2
lines changed

2 files changed

+114
-2
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"maestro": "node -r sucrase/register ./scripts/runMaestro.ts",
4646
"maestro:ios": "node -r sucrase/register ./scripts/runMaestro.ts test --include-tags all,ios maestro",
4747
"maestro:android": "node -r sucrase/register ./scripts/runMaestro.ts test --include-tags all,android maestro",
48-
"precommit": "npm run localize && lint-staged && tsc && npm test",
48+
"precommit": "npm run localize && npm run update-eslint-warnings && lint-staged && tsc && npm test",
4949
"prepare.ios": "(cd ios; pod repo update; pod install)",
5050
"prepare": "husky install && ./scripts/prepare.sh",
5151
"server": "node ./loggingServer.js",
@@ -57,7 +57,8 @@
5757
"updateVersion": "node -r sucrase/register scripts/updateVersion.ts",
5858
"updot": "EDGE_MODE=development updot",
5959
"verify": "npm run lint && npm run typechain && npm run tsc && npm run test",
60-
"watch": "npm test -- --watch"
60+
"watch": "npm test -- --watch",
61+
"update-eslint-warnings": "node -r sucrase/register ./scripts/updateEslintWarnings.ts"
6162
},
6263
"lint-staged": {
6364
"*.{js,jsx,ts,tsx}": "eslint"

scripts/updateEslintWarnings.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
#!/usr/bin/env node
2+
import { execSync } from 'child_process'
3+
import fs from 'fs'
4+
import path from 'path'
5+
6+
// Get the root directory
7+
const rootDir = path.resolve(__dirname, '..')
8+
const eslintConfigPath = path.join(rootDir, 'eslint.config.mjs')
9+
10+
// Get list of staged files
11+
function getStagedFiles(): string[] {
12+
try {
13+
const output = execSync(
14+
'git diff --cached --name-only --diff-filter=ACMR',
15+
{ encoding: 'utf8' }
16+
)
17+
return output
18+
.trim()
19+
.split('\n')
20+
.filter(file => file.length > 0)
21+
} catch (error) {
22+
console.error('Error getting staged files:', error)
23+
return []
24+
}
25+
}
26+
27+
// Remove staged files from the warnings list
28+
function removeStagedFilesFromWarningsList(): void {
29+
const stagedFiles = getStagedFiles()
30+
if (stagedFiles.length === 0) {
31+
console.log('No staged files to process')
32+
return
33+
}
34+
35+
console.log('Processing staged files:', stagedFiles.length)
36+
37+
// Read the config file
38+
let configContent = fs.readFileSync(eslintConfigPath, 'utf8')
39+
40+
// Track what was removed
41+
let removedCount = 0
42+
const removedFiles: string[] = []
43+
44+
// Process each staged file
45+
for (const stagedFile of stagedFiles) {
46+
// Create regex to match the file in the array with surrounding quotes and possible comma
47+
// This handles files with quotes like 'file.ts' or "file.ts"
48+
const patterns = [
49+
new RegExp(
50+
`^(\\s*)(['"])${stagedFile.replace(
51+
/[.*+?^${}()|[\]\\]/g,
52+
'\\$&'
53+
)}\\2,?\\s*$`,
54+
'gm'
55+
),
56+
new RegExp(
57+
`^(\\s*)(['"])${stagedFile.replace(
58+
/[.*+?^${}()|[\]\\]/g,
59+
'\\$&'
60+
)}\\2,?\\s*\n`,
61+
'gm'
62+
)
63+
]
64+
65+
for (const pattern of patterns) {
66+
const matches = configContent.match(pattern)
67+
if (matches != null) {
68+
configContent = configContent.replace(pattern, '')
69+
removedFiles.push(stagedFile)
70+
removedCount++
71+
break
72+
}
73+
}
74+
}
75+
76+
if (removedCount > 0) {
77+
// Clean up any double newlines that might have been created
78+
configContent = configContent.replace(/\n\s*\n\s*\n/g, '\n\n')
79+
80+
// Fix any trailing commas before the closing bracket
81+
configContent = configContent.replace(/,(\s*\])/g, '$1')
82+
83+
// Write the updated config
84+
fs.writeFileSync(eslintConfigPath, configContent, 'utf8')
85+
86+
// Stage the updated ESLint config
87+
try {
88+
execSync(`git add ${eslintConfigPath}`)
89+
console.log(
90+
`\nRemoved ${removedCount} file(s) from ESLint warnings list:`
91+
)
92+
removedFiles.forEach(file => {
93+
console.log(` - ${file}`)
94+
})
95+
console.log('\nUpdated eslint.config.mjs has been staged')
96+
} catch (error) {
97+
console.error('Error staging ESLint config:', error)
98+
process.exit(1)
99+
}
100+
} else {
101+
console.log('No staged files found in the ESLint warnings list')
102+
}
103+
}
104+
105+
// Main execution
106+
try {
107+
removeStagedFilesFromWarningsList()
108+
} catch (error) {
109+
console.error('Error:', error)
110+
process.exit(1)
111+
}

0 commit comments

Comments
 (0)