Skip to content

The ScriptBlockAst visitor do not work correctly ? #623

Closed
@LaurentDardenne

Description

@LaurentDardenne

I try to find all scriptblocks inside a script.

With this module only one is processed (the script) :

$Code=@'
param( $Test )

Function Measure-Sb{

 [CmdletBinding()]
 [OutputType([Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord[]])]

 Param(
       [Parameter(Mandatory = $true)]
       [ValidateNotNullOrEmpty()]
       [System.Management.Automation.Language.ScriptBlockAst] 
       # [System.Management.Automation.Language.Ast] #NOK
       # [System.Management.Automation.Language.FunctionDefinitionAst] #OK
      $ObJectAST
 )

 process { 

  [Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
    ‘Message’  = 'Test ScriptBlockAst'
    ‘Extent’   = $ObjectAST.Extent
    ‘RuleName’ = $PSCmdlet.MyInvocation.InvocationName
    ‘Severity’ = ‘Warning’
  }
 }
}

Function Test1{
 param($p1)
 "Test 1"
}

Function Test2($p1){
 "Test 2"
}

'@ 
$code > c:\temp\Test.psm1

$r=Invoke-ScriptAnalyzer -path c:\temp\Test.psm1 -CustomRulePath c:\temp\Test.psm1
$r.count
#1

When i use the FindAll() method, i get all scriptblocs :

#http://becomelotr.wordpress.com/2011/12/19/powershell-vnext-ast/
$ast=get-ast -InputScript $code
$Predicate= {
 $args[0] -is [System.Management.Automation.Language.ScriptBlockAst]
}
$res=$Ast.FindAll($Predicate, $true)
$res|% {$_.GetType()}

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    ScriptBlockAst                           System.Management.Automation.Language.Ast
True     False    ScriptBlockAst                           System.Management.Automation.Language.Ast
True     False    ScriptBlockAst                           System.Management.Automation.Language.Ast
True     False    ScriptBlockAst                           System.Management.Automation.Language.Ast


$res.Parent|? {$null -ne $_.parent}|% {$_.gettype()}
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     False    FunctionDefinitionAst                    System.Management.Automation.Language.StatementAst
True     False    FunctionDefinitionAst                    System.Management.Automation.Language.StatementAst
True     False    FunctionDefinitionAst                    System.Management.Automation.Language.StatementAst

In addition, when i use [System.Management.Automation.Language.Ast] there are no result. In this case is it also an issue ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions