Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type inference for $_ #17716

Merged

Conversation

MartinGC94
Copy link
Contributor

@MartinGC94 MartinGC94 commented Jul 19, 2022

PR Summary

Fixes the type inference for $_ in pipelines with more than 1 command by making it infer the type of the previous command of the expression rather than the first.

-Edit: Decided to make a full fix for $_ and also fix it when the scriptblock is inside an array of arguments like for Select-Object or Foreach-Object ls | ForEach-Object -Process {$_.<Tab>},{$_.<Tab>}
Also fixes a scenario where a catch block is given invalid types so there's no completion for $_ inside the catch block.
Fixes: #17613
Edit2: The test failures helped me discover a different issue, strings are enumerated as chars by the type inference. This has now been fixed.

Full list of scenarios where $_ is now properly being inferred:

  • Member invocation scriptblocks like $Collection.Where({$_})
  • Scriptblocks used as command arguments like: ls | where {$_}
  • Switch statements like: switch ("hello") {'Hello'{$_}}
  • Catch statements like: try{} catch {$_}
  • Trap statements like: trap {$_}

PR Context

PR Checklist

@ghost ghost assigned daxian-dbw Jul 19, 2022
@iSazonov iSazonov added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Jul 20, 2022
@MartinGC94 MartinGC94 requested a review from iSazonov July 20, 2022 22:14
@MartinGC94 MartinGC94 changed the title Fix type inference for $_ in pipelines with more than 1 command Improve type inference for $_ Jul 20, 2022
Co-authored-by: Ilya <darpa@yandex.ru>
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but it would be great if more comments can be added.

@MartinGC94
Copy link
Contributor Author

Sorry for pushing new updates after the review, I forgot that there were other instances where $_ is set. Hopefully I have them all now.

@iSazonov iSazonov self-requested a review July 22, 2022 16:19
Copy link
Member

@daxian-dbw daxian-dbw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thank you, @MartinGC94!

Co-authored-by: Ilya <darpa@yandex.ru>
@daxian-dbw daxian-dbw merged commit 3d4e294 into PowerShell:master Jul 25, 2022
@ghost
Copy link

ghost commented Aug 12, 2022

🎉v7.3.0-preview.7 has been released which incorporates this pull request.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Tab completion should find members for $_ in a typed catch block
3 participants