Uh oh!
There was an error while loading. Please reload this page.
Modernize alias expansion to use the language parser - #2312
Conversation
The `powerShell/expandAlias` handler built an embedded PowerShell here-string that called `[System.Management.Automation.PsParser]::Tokenize` to find command tokens. That legacy tokenizer is Windows PowerShell / full-framework only and carries the parsing limitations that come with it. This rewrites the logic in C# against the modern, cross-edition APIs: - Tokenize with `Parser.ParseInput` and select command-name tokens via `TokenFlags.CommandName` instead of the legacy `PSTokenType.Command`. - Resolve every distinct name to its alias definition in a single `Get-Command -CommandType Alias` round-trip rather than re-defining a helper function and querying one token at a time. - Escape wildcard metacharacters with `WildcardPattern.Escape` so aliases whose names are patterns -- `?` (Where-Object) and `%` (ForEach-Object) -- resolve to themselves. The old script did this by hand with a leading backtick. - Rebuild the text by substituting from the highest offset down so the earlier extents stay valid as the length changes, preserving the existing behavior for multiple aliases on a line and multi-token definitions. Scripts with no aliases are returned unchanged. The JSON-RPC contract is untouched. I extended the E2E tests with a multi-alias line that includes the wildcard-named `?` alias, which the naive modern approach would mishandle. Fixes#2108 Drafted by Copilot (Claude Opus 4.8). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR modernizes the ExpandAliasHandler by replacing a legacy approach that repeatedly defined and executed an internal PowerShell script using the obsolete PsParser tokenizer with a modern implementation that uses the System.Management.Automation.Language.Parser API directly from C#. This addresses issue #2108.
Changes:
- Replaced the inline PowerShell script with C# code that uses
Parser.ParseInputto tokenize the input and identify command-name tokens, resolves aliases via a singleGet-Commandcall, and performs backward string substitution usingStringBuilder. - Added proper handling of wildcard metacharacter alias names (like
?and%) viaWildcardPattern.Escape. - Added a new E2E test that exercises multiple aliases on one line, including wildcard metacharacter aliases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/PowerShellEditorServices/Services/PowerShell/Handlers/ExpandAliasHandler.cs | Rewrote alias expansion to use the modern language parser and C#-native token manipulation instead of an inline PowerShell script with the legacy PsParser. |
test/PowerShellEditorServices.Test.E2E/LanguageServerProtocolMessageTests.cs | Added E2E test for expanding multiple aliases (including ? and %) in a single request. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Andy Jordan (andyleejordan)
commented
Jun 16, 2026
Well that's an old TODO solved and the tests pass so... |
Justin Grote (JustinGrote)
left a comment
There was a problem hiding this comment.
Yeah looks fine to me. As a follow-on issue we should probably update the rename handler to use this same code path if it isn't already.
Uh oh!
There was an error while loading. Please reload this page.
Fixes#2108.
The
powerShell/expandAliashandler built an embedded PowerShell here-string that called[System.Management.Automation.PsParser]::Tokenizeto find command tokens. That legacy tokenizer is Windows PowerShell / full-framework only and carries its parsing limitations.This rewrites the logic in C# against the modern, cross-edition APIs:
Parser.ParseInputand select command-name tokens viaTokenFlags.CommandNameinstead of the legacyPSTokenType.Command.Get-Command -CommandType Aliasround-trip, rather than re-defining a helper function and querying one token at a time.WildcardPattern.Escapeso aliases whose names are patterns —?(Where-Object) and%(ForEach-Object) — resolve to themselves. The old script did this by hand with a leading backtick.Scripts with no aliases are returned unchanged, and the JSON-RPC contract (
IExpandAliasHandler/powerShell/expandAlias/ExpandAliasParams.Text→ExpandAliasResult.Text) is untouched.Testing
Extended the E2E tests with a multi-alias line that includes the wildcard-named
?alias —gci | ? Name | % Name→Get-ChildItem | Where-Object Name | ForEach-Object Name— which the naive modern approach would mishandle. Both ExpandAlias E2E tests pass.