|
| 1 | +# tools/agents/Agent05_VariableHygiene.ps1 |
| 2 | +[CmdletBinding(SupportsShouldProcess)] |
| 3 | +param( |
| 4 | + [switch]$Apply, |
| 5 | + [ValidateSet('remove','use','suppress')][string]$Mode = 'remove' |
| 6 | +) |
| 7 | + |
| 8 | +$branch = 'lint/var-hygiene' |
| 9 | +$targets = @('scripts\smoke-test.ps1') # expand if more files pop |
| 10 | + |
| 11 | +function Ensure-Branch { |
| 12 | + if (-not (git rev-parse --is-inside-work-tree 2>$null)) { throw "Run from repo root." } |
| 13 | + git checkout -B $branch |
| 14 | +} |
| 15 | + |
| 16 | +function Clean-File { |
| 17 | + param($path) |
| 18 | + $text = Get-Content -LiteralPath $path -Raw |
| 19 | + $orig = $text |
| 20 | + |
| 21 | + switch ($Mode) { |
| 22 | + 'remove' { |
| 23 | + # Remove simple "expectedPort = ..." assignment line if not referenced elsewhere |
| 24 | + if ($text -match '(?m)^\s*\$expectedPort\s*=') { |
| 25 | + if ($text -notmatch '(?m)\$expectedPort[^\s=]') { |
| 26 | + $text = $text -replace '(?m)^\s*\$expectedPort\s*=.*\r?\n', '' |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + 'use' { |
| 31 | + # Convert to intentional use (logs it once) |
| 32 | + if ($text -match '(?m)^\s*\$expectedPort\s*=\s*(.+)$') { |
| 33 | + if ($text -notmatch '(?m)\$expectedPort[^\s=]') { |
| 34 | + $text = $text -replace '(?m)^\s*\$expectedPort\s*=\s*(.+)$', '$expectedPort = $1' + "`r`n" + 'Write-Status -Level Verbose -Message "expectedPort=$expectedPort"' |
| 35 | + if ($text -notmatch '(?m)^\s*Import-Module\s+\.\\scripts\\modules\\Out\.psm1\b') { |
| 36 | + $text = "Import-Module .\scripts\modules\Out.psm1`r`n" + $text |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + 'suppress' { |
| 42 | + # Add a targeted suppression if the assignment is truly a placeholder |
| 43 | + $supp = "[Diagnostics.CodeAnalysis.SuppressMessage('PSUseDeclaredVarsMoreThanAssignments','Justification=""Placeholder for future checks""')]" |
| 44 | + if ($text -notmatch [regex]::Escape($supp)) { |
| 45 | + $text = $supp + "`r`n" + $text |
| 46 | + } |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + if ($text -ne $orig) { |
| 51 | + Write-Host "Would update: $path" |
| 52 | + if ($Apply) { |
| 53 | + Set-Content -LiteralPath $path -Value $text -Encoding UTF8 |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +Ensure-Branch |
| 59 | +$targets | Where-Object { Test-Path $_ } | ForEach-Object { Clean-File $_ } |
| 60 | + |
| 61 | +if ($Apply) { |
| 62 | + git add . |
| 63 | + git commit -m "lint: resolve declared-but-unused variables per PSUseDeclaredVarsMoreThanAssignments" |
| 64 | + Write-Host "Branch ready: $branch" |
| 65 | +} else { |
| 66 | + Write-Host "Dry run complete. Re-run with -Apply to write changes." |
| 67 | +} |
0 commit comments