Repository navigation
refactor(helpers): move the helpers of three analyzer rules out #103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: refactor/helpers-filter
Are you sure you want to change the base?
Changes from all commits
2a140fa
00d7d84
2e516ea
63840a7
6c2da25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| function Get-IslCredentialArgument { | ||
| <# | ||
| .SYNOPSIS | ||
| Gets what a Get-Credential call is handed as its credential. | ||
|
|
||
| .DESCRIPTION | ||
| For the interactive-call rule: Get-Credential -Credential returns a credential that is already | ||
| built, and prompts only for the password of a user name. This gives the argument of | ||
| -Credential, named by any prefix of the name, or the first positional argument, which binds to | ||
| -Credential. It gives nothing when the call names -Message, -UserName or -Title, which always | ||
| prompt, or has no such argument. A value right after another parameter belongs to that | ||
| parameter and is passed over. | ||
|
|
||
| .PARAMETER Command | ||
| The Get-Credential call. | ||
|
|
||
| .EXAMPLE | ||
| Get-IslCredentialArgument -Command $command | ||
|
|
||
| The syntax tree of $credential for Get-Credential -Credential $credential. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| System.Management.Automation.Language.Ast. The argument, or nothing. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([System.Management.Automation.Language.Ast])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [System.Management.Automation.Language.CommandAst]$Command | ||
| ) | ||
|
|
||
| foreach ($prompting in 'Message', 'UserName', 'Title') { | ||
| if (Test-IslCommandParameter -Command $Command -ParameterName $prompting) { return } | ||
| } | ||
| $elements = @($Command.CommandElements | Select-Object -Skip 1) | ||
| for ($index = 0; $index -lt $elements.Count; $index++) { | ||
| $element = $elements[$index] | ||
| if ($element.GetType().Name -eq 'CommandParameterAst') { | ||
| if (-not 'Credential'.StartsWith($element.ParameterName, 'OrdinalIgnoreCase')) { continue } | ||
| if ($element.Argument) { return $element.Argument } | ||
| if ($index + 1 -lt $elements.Count) { return $elements[$index + 1] } | ||
| return | ||
| } | ||
| # A value right after another parameter belongs to that parameter | ||
| $previous = if ($index -gt 0) { $elements[$index - 1] } else { $null } | ||
| $taken = $previous -and $previous.GetType().Name -eq 'CommandParameterAst' -and -not $previous.Argument | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing (not from this change, not blocking): a switch parameter counts as taking the next value. |
||
| if (-not $taken) { return $element } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| function Get-IslCredentialSource { | ||
| <# | ||
| .SYNOPSIS | ||
| Tells how an expression builds a credential, when it can only ever produce one. | ||
|
|
||
| .DESCRIPTION | ||
| For the interactive-call rule. The expression is recognised when it is the PSCredential | ||
| constructor, New-Object with the PSCredential type, a [pscredential] cast, or Import-Clixml, | ||
| which hands back a credential that Export-Clixml wrote. Parentheses, a pipeline of one element | ||
| and an expression statement around it are looked through. Anything else, a member or another | ||
| call included, gives nothing. | ||
|
|
||
| .PARAMETER Expression | ||
| The syntax tree of the expression. | ||
|
|
||
| .EXAMPLE | ||
| Get-IslCredentialSource -Expression $assignment.Right | ||
|
|
||
| '[pscredential]::new()' for $credential = [pscredential]::new($user, $secure). | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| System.String. How the credential is built, or nothing. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([string])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [AllowNull()] | ||
| [System.Management.Automation.Language.Ast]$Expression | ||
| ) | ||
|
|
||
| $credentialType = '^(System\.Management\.Automation\.)?PSCredential$' | ||
| # Parentheses, a one-element pipeline and the expression statement around a value are wrappers | ||
| $unwrapped = $false | ||
| while ($Expression -and -not $unwrapped) { | ||
| $kind = $Expression.GetType().Name | ||
| if ($kind -eq 'ParenExpressionAst') { $Expression = $Expression.Pipeline } | ||
| elseif ($kind -eq 'PipelineAst' -and @($Expression.PipelineElements).Count -eq 1) { | ||
| $Expression = $Expression.PipelineElements[0] | ||
| } | ||
| elseif ($kind -eq 'CommandExpressionAst') { $Expression = $Expression.Expression } | ||
| else { $unwrapped = $true } | ||
| } | ||
| if (-not $Expression) { return } | ||
| switch ($Expression.GetType().Name) { | ||
| 'CommandAst' { | ||
| $commandName = $Expression.GetCommandName() | ||
| if ($commandName -eq 'Import-Clixml') { return 'Import-Clixml' } | ||
| if ($commandName -ne 'New-Object') { return } | ||
| $typeName = $null | ||
| $elements = @($Expression.CommandElements | Select-Object -Skip 1) | ||
| for ($index = 0; $index -lt $elements.Count; $index++) { | ||
| $element = $elements[$index] | ||
| if ($element.GetType().Name -eq 'CommandParameterAst') { | ||
| if (-not 'TypeName'.StartsWith($element.ParameterName, 'OrdinalIgnoreCase')) { continue } | ||
| if ($element.Argument) { $typeName = $element.Argument.Extent.Text } | ||
| elseif ($index + 1 -lt $elements.Count) { $typeName = $elements[$index + 1].Extent.Text } | ||
| break | ||
| } | ||
| $previous = if ($index -gt 0) { $elements[$index - 1] } else { $null } | ||
| $taken = $previous -and $previous.GetType().Name -eq 'CommandParameterAst' -and | ||
| -not $previous.Argument | ||
| if (-not $taken) { $typeName = $element.Extent.Text; break } | ||
| } | ||
| if ("$typeName".Trim('''"') -match $credentialType) { return 'New-Object PSCredential' } | ||
| } | ||
| 'InvokeMemberExpressionAst' { | ||
| $onType = $Expression.Expression.GetType().Name -eq 'TypeExpressionAst' -and | ||
| $Expression.Expression.TypeName.FullName -match $credentialType | ||
| if ($onType -and "$($Expression.Member.Value)" -eq 'new') { return '[pscredential]::new()' } | ||
| } | ||
| 'ConvertExpressionAst' { | ||
| if ($Expression.Type.TypeName.FullName -match $credentialType) { return 'a [pscredential] cast' } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| function Get-IslReturnReplacement { | ||
| <# | ||
| .SYNOPSIS | ||
| Gets the edit that says what a return at script scope already does. | ||
|
|
||
| .DESCRIPTION | ||
| For the exit-code rule. A return at script scope ends the script with exit code 0, after it | ||
| has written the value it returned. The edit writes that out: the value followed by exit 0, or | ||
| exit 0 alone for a bare return. | ||
|
|
||
| .PARAMETER Return | ||
| The return statement. | ||
|
|
||
| .EXAMPLE | ||
| Get-IslReturnReplacement -Return $return | ||
|
|
||
| '1; exit 0' for return 1. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| System.String. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([string])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [System.Management.Automation.Language.ReturnStatementAst]$Return | ||
| ) | ||
|
|
||
| if ($Return.Pipeline) { "$($Return.Pipeline.Extent.Text); exit 0" } else { 'exit 0' } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| function Get-IslVariableCredentialSource { | ||
| <# | ||
| .SYNOPSIS | ||
| Tells how a variable comes to hold a credential, when every value the script gives it is one. | ||
|
|
||
| .DESCRIPTION | ||
| For the interactive-call rule. Every assignment to the variable anywhere in the script, with | ||
| or without a scope prefix, must build a credential as Get-IslCredentialSource recognises it or | ||
| be typed [pscredential], and a parameter of that name must be typed [pscredential]. The result | ||
| names each distinct way, joined by commas. When the script never gives the variable a value, | ||
| or any one of its values could be something else, the function gives nothing. | ||
|
|
||
| .PARAMETER Variable | ||
| The variable, as it is used in the call. | ||
|
|
||
| .PARAMETER Ast | ||
| The syntax tree of the whole script, where the assignments and parameters are looked for. | ||
|
|
||
| .EXAMPLE | ||
| Get-IslVariableCredentialSource -Variable $handed -Ast $Context.Ast | ||
|
|
||
| 'Import-Clixml' for a $credential that the script assigns only from Import-Clixml. | ||
|
|
||
| .INPUTS | ||
| None. | ||
|
|
||
| .OUTPUTS | ||
| System.String. How the credential is built, or nothing. | ||
| #> | ||
| [CmdletBinding()] | ||
| [OutputType([string])] | ||
| param( | ||
| [Parameter(Mandatory)] | ||
| [System.Management.Automation.Language.VariableExpressionAst]$Variable, | ||
|
|
||
| [Parameter(Mandatory)] | ||
| [System.Management.Automation.Language.Ast]$Ast | ||
| ) | ||
|
|
||
| $credentialType = '^(System\.Management\.Automation\.)?PSCredential$' | ||
| $scopePrefix = '^(script|local|private|global):' | ||
| $variableName = $Variable.VariablePath.UserPath -replace $scopePrefix, '' | ||
| $sources = [System.Collections.Generic.List[string]]::new() | ||
| $assignments = Find-IslAstNode -Ast $Ast -TypeName AssignmentStatementAst -Where { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pre-existing (not from this change, not blocking): the help promises that "every value the script gives" the variable is a credential, but only plain |
||
| param($node) | ||
| $target = $node.Left | ||
| $wrapped = $target.GetType().Name -in 'ConvertExpressionAst', 'AttributedExpressionAst' | ||
| if ($wrapped) { $target = $target.Child } | ||
| $target.GetType().Name -eq 'VariableExpressionAst' -and | ||
| ($target.VariablePath.UserPath -replace $scopePrefix, '') -eq $variableName | ||
| } | ||
| foreach ($assignment in $assignments) { | ||
| $typed = $assignment.Left.GetType().Name -eq 'ConvertExpressionAst' -and | ||
| $assignment.Left.Type.TypeName.FullName -match $credentialType | ||
| $source = if ($typed) { 'a [pscredential] variable' } | ||
| else { Get-IslCredentialSource -Expression $assignment.Right } | ||
| if (-not $source) { return } | ||
| $sources.Add($source) | ||
| } | ||
| $parameters = Find-IslAstNode -Ast $Ast -TypeName ParameterAst -Where { | ||
| param($node) | ||
| $node.Name.VariablePath.UserPath -eq $variableName | ||
| } | ||
| foreach ($parameter in $parameters) { | ||
| $typed = @($parameter.Attributes | Where-Object { | ||
| $_.GetType().Name -eq 'TypeConstraintAst' -and $_.TypeName.FullName -match $credentialType | ||
| }).Count -gt 0 | ||
| if (-not $typed) { return } | ||
| $sources.Add('a [pscredential] parameter') | ||
| } | ||
| if ($sources.Count) { @($sources | Select-Object -Unique) -join ', ' } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit (optional): this line is 111 characters, and the rest of the entry wraps at about 100. The overlong line from the previous extraction is carried along rather than rewrapped. Fix: rewrap the paragraph.