|
| 1 | +#!/usr/bin/env pwsh |
| 2 | +# PreToolUse hook: Consolidated style validation for dbatools |
| 3 | +# Runs all style checks in a single PowerShell process for performance |
| 4 | + |
| 5 | +$ErrorActionPreference = "Stop" |
| 6 | + |
| 7 | +try { |
| 8 | + $inputJson = $input | Out-String | ConvertFrom-Json |
| 9 | +} catch { |
| 10 | + exit 0 |
| 11 | +} |
| 12 | + |
| 13 | +$toolInput = $inputJson.tool_input |
| 14 | +$filePath = $toolInput.file_path |
| 15 | +if (-not $filePath) { exit 0 } |
| 16 | +if ($filePath -notlike "*.ps1") { exit 0 } |
| 17 | + |
| 18 | +$content = if ($toolInput.new_string) { $toolInput.new_string } else { $toolInput.content } |
| 19 | +if (-not $content) { exit 0 } |
| 20 | + |
| 21 | +$violations = @() |
| 22 | +$lines = $content -split "`n" |
| 23 | + |
| 24 | +# Track state for multi-line constructs |
| 25 | +$inHereStringSingle = $false |
| 26 | +$inHereStringDouble = $false |
| 27 | +$inHashtable = $false |
| 28 | +$hashtableLines = @() |
| 29 | +$hashtableStart = 0 |
| 30 | +$misalignedHashtables = @() |
| 31 | + |
| 32 | +# Patterns (using double quotes with escaping) |
| 33 | +$patternComment = "^\s*#" |
| 34 | +$patternHereStringSingleStart = "@'" |
| 35 | +$patternHereStringDoubleStart = "@`"" |
| 36 | +$patternHereStringSingleEnd = "^'@" |
| 37 | +$patternHereStringDoubleEnd = "^`"@" |
| 38 | +$patternBacktick = "``\s*$" |
| 39 | +$patternBoolAttribute = "\[\s*(Parameter|CmdletBinding|OutputType|ValidateSet)\s*\([^]]*=\s*\`$(true|false)" |
| 40 | +$patternStaticNew = "::new\s*\(" |
| 41 | +$patternSingleQuote = "(?<![@])'.+'" |
| 42 | +$patternPlainSplat = "\`$splat\s*=" |
| 43 | +$patternNamedSplat = "\`$splat[A-Z][a-zA-Z0-9]*\s*=" |
| 44 | +$patternArrayList = "ArrayList" |
| 45 | +$patternGenericList = "Generic\.List" |
| 46 | +$patternStandaloneBrace = "^\s*\{\s*$" |
| 47 | +$patternPrevLineEnd = "\)\s*$" |
| 48 | +$patternControlKeyword = "(if|else|elseif|foreach|for|while|switch|try|catch|finally)\s*$" |
| 49 | +$patternHashtableStart = "@\{" |
| 50 | +$patternHashtableEnd = "^\s*\}" |
| 51 | +$patternTrailingSpace = "\s+$" |
| 52 | + |
| 53 | +for ($i = 0; $i -lt $lines.Count; $i++) { |
| 54 | + $line = $lines[$i].TrimEnd("`r") |
| 55 | + $lineNum = $i + 1 |
| 56 | + $isComment = $line -match $patternComment |
| 57 | + |
| 58 | + # Track here-string state |
| 59 | + if ($line -match $patternHereStringSingleStart) { $inHereStringSingle = $true } |
| 60 | + if ($line -match $patternHereStringDoubleStart) { $inHereStringDouble = $true } |
| 61 | + if ($line -match $patternHereStringSingleEnd) { $inHereStringSingle = $false; continue } |
| 62 | + if ($line -match $patternHereStringDoubleEnd) { $inHereStringDouble = $false; continue } |
| 63 | + if ($inHereStringSingle -or $inHereStringDouble) { continue } |
| 64 | + |
| 65 | + # Skip comments for most checks |
| 66 | + if (-not $isComment) { |
| 67 | + # 1. No backticks for line continuation |
| 68 | + if ($line -match $patternBacktick) { |
| 69 | + $violations += "Line ${lineNum}: Backtick line continuation. Use splatting instead." |
| 70 | + } |
| 71 | + |
| 72 | + # 2. No = $true in parameter attributes |
| 73 | + if ($line -match $patternBoolAttribute) { |
| 74 | + $violations += "Line ${lineNum}: Use [Parameter(Mandatory)] not = `$true syntax." |
| 75 | + } |
| 76 | + |
| 77 | + # 3. No static new method (PowerShell v3 compatibility) |
| 78 | + if ($line -match $patternStaticNew) { |
| 79 | + $violations += "Line ${lineNum}: Use New-Object for PS v3 compatibility." |
| 80 | + } |
| 81 | + |
| 82 | + # 4. No single quotes (except here-strings already handled above) |
| 83 | + if ($line -match $patternSingleQuote) { |
| 84 | + $violations += "Line ${lineNum}: Use double quotes instead of single quotes." |
| 85 | + } |
| 86 | + |
| 87 | + # 5. No plain $splat variable names |
| 88 | + if ($line -match $patternPlainSplat -and $line -notmatch $patternNamedSplat) { |
| 89 | + $violations += "Line ${lineNum}: Use `$splat<Purpose> naming (e.g., `$splatConnection)." |
| 90 | + } |
| 91 | + |
| 92 | + # 6. No ArrayList or Generic.List collection |
| 93 | + if ($line -match $patternArrayList) { |
| 94 | + $violations += "Line ${lineNum}: Output directly to pipeline, not ArrayList." |
| 95 | + } |
| 96 | + if ($line -match $patternGenericList) { |
| 97 | + $violations += "Line ${lineNum}: Output directly to pipeline, not Generic.List." |
| 98 | + } |
| 99 | + |
| 100 | + # 7. OTBS - no standalone opening brace (Allman style) |
| 101 | + if ($line -match $patternStandaloneBrace -and $i -gt 0) { |
| 102 | + $prevLine = $lines[$i - 1].TrimEnd("`r") |
| 103 | + if ($prevLine -match $patternPrevLineEnd -or $prevLine -match $patternControlKeyword) { |
| 104 | + $violations += "Line ${lineNum}: Use OTBS - opening brace on same line as statement." |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + # 8. Track hashtables for alignment check |
| 109 | + if ($line -match $patternHashtableStart) { |
| 110 | + $inHashtable = $true |
| 111 | + $hashtableLines = @() |
| 112 | + $hashtableStart = $lineNum |
| 113 | + # Don't add this line to hashtableLines - it's the opening, not an entry |
| 114 | + } elseif ($inHashtable) { |
| 115 | + if ($line -match $patternHashtableEnd) { |
| 116 | + if ($hashtableLines.Count -ge 2) { |
| 117 | + $equalsPositions = $hashtableLines | ForEach-Object { |
| 118 | + $pos = $_.IndexOf("=") |
| 119 | + if ($pos -ge 0) { $pos } |
| 120 | + } | Where-Object { $null -ne $_ } | Select-Object -Unique |
| 121 | + if ($equalsPositions.Count -gt 1) { |
| 122 | + $misalignedHashtables += "Lines $hashtableStart-$lineNum" |
| 123 | + } |
| 124 | + } |
| 125 | + $inHashtable = $false |
| 126 | + } elseif ($line -match "=" -and $line.Trim() -ne "") { |
| 127 | + $hashtableLines += $line |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + # 9. No trailing spaces (check even in comments) |
| 133 | + if ($line -match $patternTrailingSpace) { |
| 134 | + $violations += "Line ${lineNum}: Trailing whitespace." |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +# Add hashtable alignment violations |
| 139 | +foreach ($ht in $misalignedHashtables) { |
| 140 | + $violations += "${ht}: Hashtable = signs must be vertically aligned." |
| 141 | +} |
| 142 | + |
| 143 | +if ($violations.Count -gt 0) { |
| 144 | + $summary = ($violations | Select-Object -First 5) -join "`n" |
| 145 | + $more = if ($violations.Count -gt 5) { "`n... and $($violations.Count - 5) more violations" } else { "" } |
| 146 | + [Console]::Error.WriteLine("BLOCKED: dbatools style violations:`n$summary$more") |
| 147 | + exit 2 |
| 148 | +} |
| 149 | + |
| 150 | +exit 0 |
0 commit comments