@@ -2942,13 +2942,16 @@ private void SetLastAssignment(Ast ast, bool enumerate = false, bool redirection
2942
2942
}
2943
2943
}
2944
2944
2945
- private void SetLastAssignmentType ( PSTypeName typeName , int assignmentOffset )
2945
+ private void SetLastAssignmentType ( PSTypeName typeName , IScriptExtent assignmentExtent )
2946
2946
{
2947
- if ( LastAssignmentOffset < assignmentOffset )
2947
+ if ( LastAssignmentOffset < assignmentExtent . StartOffset && ! VariableTarget . Extent . IsWithin ( assignmentExtent ) )
2948
2948
{
2949
+ // If the variable we are inferring the value of is inside this assignment then the assignment is invalid
2950
+ // For example: $x = 1..10; Get-Random 2>variable:x -InputObject ($x.<Tab>) here the variable should be inferred based on the initial 1..10 assignment
2951
+ // and not the error redirected variable.
2949
2952
ClearAssignmentData ( ) ;
2950
2953
LastAssignmentType = typeName ;
2951
- LastAssignmentOffset = assignmentOffset ;
2954
+ LastAssignmentOffset = assignmentExtent . StartOffset ;
2952
2955
}
2953
2956
}
2954
2957
@@ -3109,17 +3112,17 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
3109
3112
{
3110
3113
case "ErrorVariable" :
3111
3114
case "ev" :
3112
- SetLastAssignmentType ( new PSTypeName ( typeof ( List < ErrorRecord > ) ) , commandAst . Extent . StartOffset ) ;
3115
+ SetLastAssignmentType ( new PSTypeName ( typeof ( List < ErrorRecord > ) ) , commandAst . Extent ) ;
3113
3116
break ;
3114
3117
3115
3118
case "WarningVariable" :
3116
3119
case "wv" :
3117
- SetLastAssignmentType ( new PSTypeName ( typeof ( List < WarningRecord > ) ) , commandAst . Extent . StartOffset ) ;
3120
+ SetLastAssignmentType ( new PSTypeName ( typeof ( List < WarningRecord > ) ) , commandAst . Extent ) ;
3118
3121
break ;
3119
3122
3120
3123
case "InformationVariable" :
3121
3124
case "iv" :
3122
- SetLastAssignmentType ( new PSTypeName ( typeof ( List < InformationalRecord > ) ) , commandAst . Extent . StartOffset ) ;
3125
+ SetLastAssignmentType ( new PSTypeName ( typeof ( List < InformationalRecord > ) ) , commandAst . Extent ) ;
3123
3126
break ;
3124
3127
3125
3128
case "OutVariable" :
@@ -3166,23 +3169,23 @@ public override AstVisitAction VisitCommand(CommandAst commandAst)
3166
3169
switch ( fileRedirection . FromStream )
3167
3170
{
3168
3171
case RedirectionStream . Error :
3169
- SetLastAssignmentType ( new PSTypeName ( typeof ( ErrorRecord ) ) , commandAst . Extent . StartOffset ) ;
3172
+ SetLastAssignmentType ( new PSTypeName ( typeof ( ErrorRecord ) ) , commandAst . Extent ) ;
3170
3173
break ;
3171
3174
3172
3175
case RedirectionStream . Warning :
3173
- SetLastAssignmentType ( new PSTypeName ( typeof ( WarningRecord ) ) , commandAst . Extent . StartOffset ) ;
3176
+ SetLastAssignmentType ( new PSTypeName ( typeof ( WarningRecord ) ) , commandAst . Extent ) ;
3174
3177
break ;
3175
3178
3176
3179
case RedirectionStream . Verbose :
3177
- SetLastAssignmentType ( new PSTypeName ( typeof ( VerboseRecord ) ) , commandAst . Extent . StartOffset ) ;
3180
+ SetLastAssignmentType ( new PSTypeName ( typeof ( VerboseRecord ) ) , commandAst . Extent ) ;
3178
3181
break ;
3179
3182
3180
3183
case RedirectionStream . Debug :
3181
- SetLastAssignmentType ( new PSTypeName ( typeof ( DebugRecord ) ) , commandAst . Extent . StartOffset ) ;
3184
+ SetLastAssignmentType ( new PSTypeName ( typeof ( DebugRecord ) ) , commandAst . Extent ) ;
3182
3185
break ;
3183
3186
3184
3187
case RedirectionStream . Information :
3185
- SetLastAssignmentType ( new PSTypeName ( typeof ( InformationRecord ) ) , commandAst . Extent . StartOffset ) ;
3188
+ SetLastAssignmentType ( new PSTypeName ( typeof ( InformationRecord ) ) , commandAst . Extent ) ;
3186
3189
break ;
3187
3190
3188
3191
default :
0 commit comments