Repository: PowerShell/PowerShell
File: src/System.Management.Automation/engine/parser/TypeInferenceVisitor.cs (C#)

Code before:
            /// <summary>
            /// The inferred type from the most recent assignment. This is only used for stream redirections to variables, or the special OutVariable common parameters.
            /// </summary>
            internal PSTypeName LastAssignmentType;
            
            /// <summary>
            /// Whether or not the types from the last assignment should be enumerated.
            /// For assignments made by the PipelineVariable parameter or the foreach statement.
            /// </summary>
            internal bool EnumerateAssignment;
            
            /// <summary>
            /// Whether or not the last assignment was via command redirection.
            /// </summary>
            internal bool RedirectionAssignment;

            /// <summary>
            /// The Ast of the scope we are currently analyzing.
            /// </summary>
            internal Ast ScopeDefinitionAst;
            internal int StopSearchOffset;
            private int LastAssignmentOffset = -1;

            private void SetLastAssignment(Ast ast, bool enumerate = false, bool redirectionAssignment = false)
            {
                if (LastAssignmentOffset < ast.Extent.StartOffset && !VariableTarget.Extent.IsWithin(ast.Extent))
                {
                    ClearAssignmentData();
                    LastAssignment = ast;
                    EnumerateAssignment = enumerate;
                    RedirectionAssignment = redirectionAssignment;
                    LastAssignmentOffset = ast.Extent.StartOffset;
                }
            }

            private void SetLastAssignmentType(PSTypeName typeName, int assignmentOffset)
            {
                if (LastAssignmentOffset < assignmentOffset)
                {
                    ClearAssignmentData();
                    LastAssignmentType = typeName;
                    LastAssignmentOffset = assignmentOffset;
                }
            }

            private void ClearAssignmentData()
            {
                LastAssignment = null;
                LastAssignmentType = null;
                EnumerateAssignment = false;
                RedirectionAssignment = false;

Review comment (suggestion):
@MartinGC94 Could you please add a comment to explain why we need the `!VariableTarget.Extent.IsWithin(ast.Extent)` part to make it easier to understand? I think it'd be good to include the example `$x = Get-Random; $x = $x.Where{$_.<tab>}` in the comment. Thanks!

Code after:
            /// <summary>
            /// The inferred type from the most recent assignment. This is only used for stream redirections to variables, or the special OutVariable common parameters.
            /// </summary>
            internal PSTypeName LastAssignmentType;
            
            /// <summary>
            /// Whether or not the types from the last assignment should be enumerated.
            /// For assignments made by the PipelineVariable parameter or the foreach statement.
            /// </summary>
            internal bool EnumerateAssignment;
            
            /// <summary>
            /// Whether or not the last assignment was via command redirection.
            /// </summary>
            internal bool RedirectionAssignment;

            /// <summary>
            /// The Ast of the scope we are currently analyzing.
            /// </summary>
            internal Ast ScopeDefinitionAst;
            internal int StopSearchOffset;
            private int LastAssignmentOffset = -1;

            private void SetLastAssignment(Ast ast, bool enumerate = false, bool redirectionAssignment = false)
            {
                if (LastAssignmentOffset < ast.Extent.StartOffset && !VariableTarget.Extent.IsWithin(ast.Extent))
                {
                    // If the variable we are inferring the value of is inside this assignment then the assignment is invalid
                    // For example: $x = Get-Random; $x = $x.Where{$_.<Tab>} here the value should be inferred based on Get-Random and not $x = $x...
                    ClearAssignmentData();
                    LastAssignment = ast;
                    EnumerateAssignment = enumerate;
                    RedirectionAssignment = redirectionAssignment;
                    LastAssignmentOffset = ast.Extent.StartOffset;
                }
            }

            private void SetLastAssignmentType(PSTypeName typeName, int assignmentOffset)
            {
                if (LastAssignmentOffset < assignmentOffset)
                {
                    ClearAssignmentData();
                    LastAssignmentType = typeName;
                    LastAssignmentOffset = assignmentOffset;
                }
            }

            private void ClearAssignmentData()
            {
                LastAssignment = null;
                LastAssignmentType = null;