Rules/Test-ForWriteProgressWithoutID.ps1

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
function Test-ForWriteProgressWithoutID
{
    #region ScriptTokenValidation Parameter Statement
    param(
    <#
    This parameter will contain the tokens in the script, and will be automatically
    provided when this command is run within ScriptCop.
     
    This parameter should not be used directly, except for testing purposes.
    #>

    [Parameter(ParameterSetName='TestScriptToken',
        Mandatory=$true,
        ValueFromPipelineByPropertyName=$true)]
    [Management.Automation.PSToken[]]
    $ScriptToken,
    
    <#
    This parameter will contain the command that was tokenized, and will be automatically
    provided when this command is run within ScriptCop.
     
    This parameter should not be used directly, except for testing purposes.
    #>

    [Parameter(ParameterSetName='TestScriptToken',Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [Management.Automation.CommandInfo]
    $ScriptTokenCommand,
    
    <#
    This parameter contains the raw text of the script, and will be automatically
    provided when this command is run within ScriptCop
     
    This parameter should not be used directly, except for testing purposes.
    #>

    [Parameter(ParameterSetName='TestScriptToken',Mandatory=$true,ValueFromPipelineByPropertyName=$true)]
    [string]
    $ScriptText
    )
    #endregion ScriptTokenValidation Parameter Statement
            
    process {              
        $writeProgressFoundAt =@{}
        $lookingForId = $false
        $lastWriteProgress = $null
        foreach ($t in $ScriptToken) {
            if ($lookingForId -and $t.Type -eq 'CommandParameter') {
                if ($t.Content -eq '-ID') {
                    $lookingForId = $false
                    $writeProgressFoundAt.Remove($lastWriteProgress.Start)
                    $lastWriteProgress= $null
                } 
            }

            if ($lookingForId -and $t.Type -eq 'Command') {
                $lookingForId = $false 
            }

            if ($t.Content -eq 'Write-Progress' -and $t.Type -eq 'Command') {
                $writeProgressFoundAt[$t.Start] = $lastWriteProgress  = $t 
                $lookingForId = $true
            }
            
        }
        

        if ($writeProgressFoundAt.Count) {
            $writeprogreslocations = $writeProgressFoundAt.Values | Sort-Object StartLine |  ForEach-Object { "($($_.StartLine + 1), $($_.StartColumn))" }
            Write-Error "Write-Progress should have an ID (if it's not there, progress messages from nested functions can clobber each other). Write-Progres is lacking an id at: $($writeprogreslocations -join ' and ')"
        }        
    }
}