Skip to content

Commit e9040c6

Browse files
committed
Improvement in splat handling to address #12
1 parent 7f3c403 commit e9040c6

File tree

1 file changed

+50
-51
lines changed

1 file changed

+50
-51
lines changed

src/PSAppDeployToolkit.Tools/PSScriptAnalyzer/Measure-ADTCompatibility.psm1

Lines changed: 50 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2636,51 +2636,6 @@ function Measure-ADTCompatibility
26362636
$parameterName = $boundParameter.Key
26372637
$variableName = $boundParameter.Value.Value.VariablePath.UserPath
26382638

2639-
if ($variableName -and $functionMappings.$functionName.TransformParameters.$parameterName -is [ScriptBlock])
2640-
{
2641-
# Find the last assignment of the variable before the current command
2642-
[ScriptBlock]$variableAssignmentPredicate = {
2643-
param ([System.Management.Automation.Language.Ast]$Ast)
2644-
$Ast -is [System.Management.Automation.Language.AssignmentStatementAst] -and $Ast.Left.Extent.Text -match "\`$$variableName$" -and ($Ast.Extent.StartLineNumber -lt $commandAst.Extent.StartLineNumber -or ($Ast.Extent.StartLineNumber -eq $commandAst.Extent.StartLineNumber -and $Ast.Extent.StartColumnNumber -lt $commandAst.Extent.StartColumnNumber))
2645-
}
2646-
[System.Management.Automation.Language.Ast]$variableAssignmentAst = $ScriptBlockAst.FindAll($variableAssignmentPredicate, $true) | Select-Object -Last 1
2647-
2648-
if ($variableAssignmentAst)
2649-
{
2650-
$newVariableContent = ForEach-Object -InputObject $variableAssignmentAst.Right.Extent.Text -Process $functionMappings[$functionName].TransformParameters[$parameterName]
2651-
$newVariableContent = $newVariableContent -replace '^-\w+\s+`?' # Remove -Parameter name + space, plus potential backtick/linebreak
2652-
2653-
$outputMessage = "Modify variable:`n$($variableAssignmentAst.Left.Extent.Text)` = $newVariableContent"
2654-
2655-
# Create a CorrectionExtent object for the suggested correction
2656-
$objParams = @{
2657-
TypeName = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent'
2658-
ArgumentList = @(
2659-
$variableAssignmentAst.Extent.StartLineNumber
2660-
$variableAssignmentAst.Extent.EndLineNumber
2661-
$variableAssignmentAst.Extent.StartColumnNumber
2662-
$variableAssignmentAst.Extent.EndColumnNumber
2663-
"$($variableAssignmentAst.Left.Extent.Text) = $newVariableContent"
2664-
$MyInvocation.MyCommand.Definition
2665-
'More information: https://psappdeploytoolkit.com/docs/reference/variables'
2666-
)
2667-
}
2668-
$correctionExtent = New-Object @objParams
2669-
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$($objParams.TypeName)]
2670-
$suggestedCorrections.Add($correctionExtent) | Out-Null
2671-
2672-
# Output the diagnostic record in the format expected by the ScriptAnalyzer
2673-
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
2674-
Message = $outputMessage
2675-
Extent = $variableAssignmentAst.Extent
2676-
RuleName = 'Measure-ADTCompatibility'
2677-
Severity = 'Warning'
2678-
RuleSuppressionID = 'ADTCompatibilitySuppression'
2679-
SuggestedCorrections = $suggestedCorrections
2680-
}
2681-
}
2682-
}
2683-
26842639
# Process all hashtable definitions that splat to legacy functions
26852640
if ($boundParameter.Value.Value.Splatted)
26862641
{
@@ -2791,6 +2746,50 @@ function Measure-ADTCompatibility
27912746
}
27922747
}
27932748
}
2749+
elseif ($variableName -and $functionMappings.$functionName.TransformParameters.$parameterName -is [ScriptBlock])
2750+
{
2751+
# Find the last assignment of the variable before the current command
2752+
[ScriptBlock]$variableAssignmentPredicate = {
2753+
param ([System.Management.Automation.Language.Ast]$Ast)
2754+
$Ast -is [System.Management.Automation.Language.AssignmentStatementAst] -and $Ast.Left.Extent.Text -match "\`$$variableName$" -and ($Ast.Extent.StartLineNumber -lt $commandAst.Extent.StartLineNumber -or ($Ast.Extent.StartLineNumber -eq $commandAst.Extent.StartLineNumber -and $Ast.Extent.StartColumnNumber -lt $commandAst.Extent.StartColumnNumber))
2755+
}
2756+
[System.Management.Automation.Language.Ast]$variableAssignmentAst = $ScriptBlockAst.FindAll($variableAssignmentPredicate, $true) | Select-Object -Last 1
2757+
2758+
if ($variableAssignmentAst)
2759+
{
2760+
$newVariableContent = ForEach-Object -InputObject $variableAssignmentAst.Right.Extent.Text -Process $functionMappings[$functionName].TransformParameters[$parameterName]
2761+
$newVariableContent = $newVariableContent -replace '^-\w+\s+`?' # Remove -Parameter name + space, plus potential backtick/linebreak
2762+
2763+
$outputMessage = "Modify variable:`n$($variableAssignmentAst.Left.Extent.Text)` = $newVariableContent"
2764+
2765+
# Create a CorrectionExtent object for the suggested correction
2766+
$objParams = @{
2767+
TypeName = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent'
2768+
ArgumentList = @(
2769+
$variableAssignmentAst.Extent.StartLineNumber
2770+
$variableAssignmentAst.Extent.EndLineNumber
2771+
$variableAssignmentAst.Extent.StartColumnNumber
2772+
$variableAssignmentAst.Extent.EndColumnNumber
2773+
"$($variableAssignmentAst.Left.Extent.Text) = $newVariableContent"
2774+
$MyInvocation.MyCommand.Definition
2775+
'More information: https://psappdeploytoolkit.com/docs/reference/variables'
2776+
)
2777+
}
2778+
$correctionExtent = New-Object @objParams
2779+
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$($objParams.TypeName)]
2780+
$suggestedCorrections.Add($correctionExtent) | Out-Null
2781+
2782+
# Output the diagnostic record in the format expected by the ScriptAnalyzer
2783+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
2784+
Message = $outputMessage
2785+
Extent = $variableAssignmentAst.Extent
2786+
RuleName = 'Measure-ADTCompatibility'
2787+
Severity = 'Warning'
2788+
RuleSuppressionID = 'ADTCompatibilitySuppression'
2789+
SuggestedCorrections = $suggestedCorrections
2790+
}
2791+
}
2792+
}
27942793
}
27952794
}
27962795

@@ -2899,7 +2898,12 @@ function Measure-ADTCompatibility
28992898
$outputMessage.AppendLine("-$($boundParameter.Key) is deprecated.") | Out-Null
29002899
continue
29012900
}
2902-
if ($boundParameter.Key -in $functionMappings[$functionName].TransformParameters.Keys)
2901+
if ($boundParameter.Value.Value.Splatted)
2902+
{
2903+
# This is a splatted parameter, e.g. @params, retain the original value
2904+
$NewParam = $boundParameter.Value.Value.Extent.Text
2905+
}
2906+
elseif ($boundParameter.Key -in $functionMappings[$functionName].TransformParameters.Keys)
29032907
{
29042908
if ($functionMappings[$functionName].TransformParameters[$boundParameter.Key] -is [ScriptBlock])
29052909
{
@@ -2953,11 +2957,6 @@ function Measure-ADTCompatibility
29532957
# This is a switch bound with a value, e.g. -Switch:$true
29542958
$newParam = $boundParameters.Value.Value.Parent.Extent.Text
29552959
}
2956-
elseif ($boundParameter.Value.Value.Splatted)
2957-
{
2958-
# This is a splatted parameter, e.g. @params, retain the original value
2959-
$NewParam = $boundParameter.Value.Value.Extent.Text
2960-
}
29612960
elseif ($boundParameter.Key -match '^\d+$')
29622961
{
29632962
# This is an unrecognized positional parameter, pass through as-is

0 commit comments

Comments
 (0)