|
| 1 | +using namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic |
| 2 | + |
| 3 | +function Measure-LowercaseKeyword { |
| 4 | + <# |
| 5 | + .SYNOPSIS |
| 6 | + PowerShell keywords and constants should be in lowercase. |
| 7 | +
|
| 8 | + .DESCRIPTION |
| 9 | + PowerShell keywords (function, if, foreach, etc.) and constants ($true, $false, $null) |
| 10 | + should use lowercase for consistency and best practices. |
| 11 | +
|
| 12 | + .EXAMPLE |
| 13 | + Measure-LowercaseKeyword -ScriptBlockAst $ScriptBlockAst |
| 14 | +
|
| 15 | + .INPUTS |
| 16 | + [System.Management.Automation.Language.ScriptBlockAst] |
| 17 | +
|
| 18 | + .OUTPUTS |
| 19 | + [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]] |
| 20 | + #> |
| 21 | + [CmdletBinding()] |
| 22 | + [OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])] |
| 23 | + param ( |
| 24 | + [Parameter(Mandatory = $true)] |
| 25 | + [ValidateNotNullOrEmpty()] |
| 26 | + [System.Management.Automation.Language.ScriptBlockAst] $ScriptBlockAst |
| 27 | + ) |
| 28 | + |
| 29 | + process { |
| 30 | + $Results = @() |
| 31 | + |
| 32 | + # Define keywords and constants we want to check |
| 33 | + $Keywords = @('function', 'foreach', 'if', 'else', 'elseif', 'return', 'switch', 'param', |
| 34 | + 'begin', 'process', 'end', 'in', 'do', 'while', 'until', 'for', 'trap', |
| 35 | + 'throw', 'catch', 'try', 'finally', 'data', 'dynamicparam', 'break', |
| 36 | + 'continue', 'exit', 'class', 'enum', 'using') |
| 37 | + $Constants = @('$true', '$false', '$null') |
| 38 | + |
| 39 | + try { |
| 40 | + # Get all tokens from the script |
| 41 | + $Tokens = @() |
| 42 | + $ParseErrors = @() |
| 43 | + [void][System.Management.Automation.Language.Parser]::ParseInput( |
| 44 | + $ScriptBlockAst.ToString(), |
| 45 | + [ref]$Tokens, |
| 46 | + [ref]$ParseErrors |
| 47 | + ) |
| 48 | + |
| 49 | + if ($ParseErrors.Count -gt 0) { |
| 50 | + return $Results |
| 51 | + } |
| 52 | + |
| 53 | + # Track processed token positions to avoid duplicates |
| 54 | + $ProcessedTokens = @{} |
| 55 | + |
| 56 | + foreach ($Token in $Tokens) { |
| 57 | + $TokenText = $Token.Text |
| 58 | + $LowerTokenText = $TokenText.ToLower() |
| 59 | + |
| 60 | + # Create a unique key for this token position |
| 61 | + $TokenKey = "$($Token.Extent.StartLineNumber):$($Token.Extent.StartColumnNumber):$TokenText" |
| 62 | + |
| 63 | + # Skip if we've already processed this exact token at this position |
| 64 | + if ($ProcessedTokens.ContainsKey($TokenKey)) { |
| 65 | + continue |
| 66 | + } |
| 67 | + |
| 68 | + # Check keywords (check text content directly to be more reliable) |
| 69 | + if ($Keywords -contains $LowerTokenText -and $TokenText -cne $LowerTokenText) { |
| 70 | + $Results += [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ |
| 71 | + Message = "Keyword '$TokenText' should be lowercase ('$LowerTokenText')." |
| 72 | + Extent = $Token.Extent |
| 73 | + RuleName = $PSCmdlet.MyInvocation.InvocationName |
| 74 | + Severity = 'Warning' |
| 75 | + } |
| 76 | + $ProcessedTokens[$TokenKey] = $true |
| 77 | + } |
| 78 | + # Check constants |
| 79 | + elseif ($Constants -contains $LowerTokenText -and $TokenText -cne $LowerTokenText) { |
| 80 | + $Results += [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{ |
| 81 | + Message = "Constant '$TokenText' should be lowercase ('$LowerTokenText')." |
| 82 | + Extent = $Token.Extent |
| 83 | + RuleName = $PSCmdlet.MyInvocation.InvocationName |
| 84 | + Severity = 'Warning' |
| 85 | + } |
| 86 | + $ProcessedTokens[$TokenKey] = $true |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return $Results |
| 91 | + } |
| 92 | + catch { |
| 93 | + $PSCmdlet.ThrowTerminatingError($_) |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +Export-ModuleMember -Function Measure-LowercaseKeyword |
0 commit comments