Skip to content

Commit b989653

Browse files
committed
Process variables first
1 parent e9040c6 commit b989653

File tree

1 file changed

+66
-66
lines changed

1 file changed

+66
-66
lines changed

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

Lines changed: 66 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2624,7 +2624,72 @@ function Measure-ADTCompatibility
26242624
}
26252625
[System.Management.Automation.Language.Ast[]]$commandAsts = $ScriptBlockAst.FindAll($commandPredicate, $true)
26262626

2627-
# 1. Process all variable contents used by legacy functions
2627+
# 1. Process all legacy variables
2628+
foreach ($variableAst in $variableAsts)
2629+
{
2630+
$variableName = $variableAst.VariablePath.UserPath
2631+
$newVariable = $variableMappings[$variableName]
2632+
2633+
if ([string]::IsNullOrWhiteSpace($newVariable))
2634+
{
2635+
$outputMessage = "The variable [`$$variableName] is deprecated and no longer available."
2636+
$suggestedCorrections = $null
2637+
}
2638+
else
2639+
{
2640+
if ($newVariable -match 'ADTSession')
2641+
{
2642+
$outputMessage = "The variable [`$$variableName] is now a session variable and no longer directly available. Use [$newVariable] instead."
2643+
}
2644+
elseif ($newVariable -match 'ADTConfig')
2645+
{
2646+
$outputMessage = "The variable [`$$variableName] is now a config variable and no longer directly available. Use [$newVariable] instead."
2647+
}
2648+
elseif ($newVariable -match 'ADTString')
2649+
{
2650+
$outputMessage = "The variable [`$$variableName] is now a localization string variable and no longer directly available. Use [$newVariable] instead."
2651+
}
2652+
else
2653+
{
2654+
$outputMessage = "The variable [`$$variableName] is deprecated. Use [$newVariable] instead."
2655+
}
2656+
2657+
if ($newVariable -like '*.*' -and $variableAst.Parent.StringConstantType -in [System.Management.Automation.Language.StringConstantType]'DoubleQuoted', [System.Management.Automation.Language.StringConstantType]'DoubleQuotedHereString')
2658+
{
2659+
# Wrap variable in $() if it is contains a . and is used in a double-quoted string
2660+
$newVariable = "`$($newVariable)"
2661+
}
2662+
2663+
# Create a CorrectionExtent object for the suggested correction
2664+
$objParams = @{
2665+
TypeName = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent'
2666+
ArgumentList = @(
2667+
$variableAst.Extent.StartLineNumber
2668+
$variableAst.Extent.EndLineNumber
2669+
$variableAst.Extent.StartColumnNumber
2670+
$variableAst.Extent.EndColumnNumber
2671+
$newVariable
2672+
$MyInvocation.MyCommand.Definition
2673+
'More information: https://psappdeploytoolkit.com/docs/reference/variables'
2674+
)
2675+
}
2676+
$correctionExtent = New-Object @objParams
2677+
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$($objParams.TypeName)]
2678+
$suggestedCorrections.Add($correctionExtent) | Out-Null
2679+
}
2680+
2681+
# Output the diagnostic record in the format expected by the ScriptAnalyzer
2682+
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
2683+
Message = $outputMessage
2684+
Extent = $variableAst.Extent
2685+
RuleName = 'Measure-ADTCompatibility'
2686+
Severity = 'Warning'
2687+
RuleSuppressionID = 'ADTCompatibilitySuppression'
2688+
SuggestedCorrections = $suggestedCorrections
2689+
}
2690+
}
2691+
2692+
# 2. Process all variable contents used by legacy functions
26282693
foreach ($commandAst in $commandAsts)
26292694
{
26302695
$functionName = $commandAst.GetCommandName()
@@ -2793,71 +2858,6 @@ function Measure-ADTCompatibility
27932858
}
27942859
}
27952860

2796-
# 2. Process all legacy variables
2797-
foreach ($variableAst in $variableAsts)
2798-
{
2799-
$variableName = $variableAst.VariablePath.UserPath
2800-
$newVariable = $variableMappings[$variableName]
2801-
2802-
if ([string]::IsNullOrWhiteSpace($newVariable))
2803-
{
2804-
$outputMessage = "The variable [`$$variableName] is deprecated and no longer available."
2805-
$suggestedCorrections = $null
2806-
}
2807-
else
2808-
{
2809-
if ($newVariable -match 'ADTSession')
2810-
{
2811-
$outputMessage = "The variable [`$$variableName] is now a session variable and no longer directly available. Use [$newVariable] instead."
2812-
}
2813-
elseif ($newVariable -match 'ADTConfig')
2814-
{
2815-
$outputMessage = "The variable [`$$variableName] is now a config variable and no longer directly available. Use [$newVariable] instead."
2816-
}
2817-
elseif ($newVariable -match 'ADTString')
2818-
{
2819-
$outputMessage = "The variable [`$$variableName] is now a localization string variable and no longer directly available. Use [$newVariable] instead."
2820-
}
2821-
else
2822-
{
2823-
$outputMessage = "The variable [`$$variableName] is deprecated. Use [$newVariable] instead."
2824-
}
2825-
2826-
if ($newVariable -like '*.*' -and $variableAst.Parent.StringConstantType -in [System.Management.Automation.Language.StringConstantType]'DoubleQuoted', [System.Management.Automation.Language.StringConstantType]'DoubleQuotedHereString')
2827-
{
2828-
# Wrap variable in $() if it is contains a . and is used in a double-quoted string
2829-
$newVariable = "`$($newVariable)"
2830-
}
2831-
2832-
# Create a CorrectionExtent object for the suggested correction
2833-
$objParams = @{
2834-
TypeName = 'Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.CorrectionExtent'
2835-
ArgumentList = @(
2836-
$variableAst.Extent.StartLineNumber
2837-
$variableAst.Extent.EndLineNumber
2838-
$variableAst.Extent.StartColumnNumber
2839-
$variableAst.Extent.EndColumnNumber
2840-
$newVariable
2841-
$MyInvocation.MyCommand.Definition
2842-
'More information: https://psappdeploytoolkit.com/docs/reference/variables'
2843-
)
2844-
}
2845-
$correctionExtent = New-Object @objParams
2846-
$suggestedCorrections = New-Object System.Collections.ObjectModel.Collection[$($objParams.TypeName)]
2847-
$suggestedCorrections.Add($correctionExtent) | Out-Null
2848-
}
2849-
2850-
# Output the diagnostic record in the format expected by the ScriptAnalyzer
2851-
[Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic.DiagnosticRecord]@{
2852-
Message = $outputMessage
2853-
Extent = $variableAst.Extent
2854-
RuleName = 'Measure-ADTCompatibility'
2855-
Severity = 'Warning'
2856-
RuleSuppressionID = 'ADTCompatibilitySuppression'
2857-
SuggestedCorrections = $suggestedCorrections
2858-
}
2859-
}
2860-
28612861
# 3. Redefine legacy functions
28622862
foreach ($commandAst in $commandAsts)
28632863
{

0 commit comments

Comments
 (0)