Skip to content

Support $_ type inference in magic methods #11803

@SeeminglyScience

Description

@SeeminglyScience

Summary of the new feature/enhancement

The type of $_ can be inferred for ForEach-Object/Where-Object but not in the ForEach and Where magic methods:

$script = '@(0..10).ForEach{ $_.'
TabExpansion2 -inputScript $script -cursorColumn $script.Length

Should return the same as

$script = '@(0..10) | ForEach-Object { $_.'
TabExpansion2 -inputScript $script -cursorColumn $script.Length

Proposed technical implementation details (optional)

private void InferTypeFrom(VariableExpressionAst variableExpressionAst, List<PSTypeName> inferredTypes)
{
// We don't need to handle drive qualified variables, we can usually get those values
// without needing to "guess" at the type.
var astVariablePath = variableExpressionAst.VariablePath;
if (!astVariablePath.IsVariable)
{
// Not a variable - the caller should have already tried going to session state
// to get the item and hence it's type, but that must have failed. Don't try again.
return;
}
Ast parent = variableExpressionAst.Parent;
if (astVariablePath.IsUnqualified &&
(SpecialVariables.IsUnderbar(astVariablePath.UserPath)
|| astVariablePath.UserPath.EqualsOrdinalIgnoreCase(SpecialVariables.PSItem)))
{

The code above is the start of $_ type inference, a case just needs to be added for ForEach/Where similar to the others.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Interactive-IntelliSensetab completion

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions