Repository navigation
Alias "?" is changed to "%" by Invoke-Formatter #1209
Copy link
Copy link
Closed
Description
Invoke-Formatter will convert "?" to "%".
Before formatting
1..10 | ? { ($_ % 2) -eq 0 }After formatting
1..10 | % { ($_ % 2) -eq 0 }I found this bug when I research PowerShell/vscode-powershell#1842 and PowerShell/vscode-powershell#1849 .
It's a ridiculous bug.
Steps to reproduce
In the standalone Windows PowerShell console.
PS C:\> Get-Module PSScriptAnalyzer -ListAvailable
PS C:\> Install-Module PSScriptAnalyzer -Force -Scope CurrentUser
PS C:\> Get-Module PSScriptAnalyzer -ListAvailable
Directory: C:\Users\Administrator\Documents\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 1.18.0 PSScriptAnalyzer {Get-ScriptAnalyzerRule, Invoke-ScriptAnalyzer, Invoke-Formatter}
PS C:\> Invoke-Formatter '1..10 | ? { ($_ % 2) -eq 0 }' -Verbose
VERBOSE: Using settings file at C:\Users\Administrator\Documents\WindowsPowerShell\Modules\PSScriptAnalyzer\1.18.0\Settings\CodeFormatting.psd1.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSPlaceCloseBrace rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSPlaceOpenBrace rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSUseConsistentWhitespace rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSUseConsistentIndentation rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSAlignAssignmentStatement rule.
VERBOSE: Found 0 violations.
VERBOSE: Fixed 0 violations.
VERBOSE: Analyzing Script Definition.
VERBOSE: Running PSUseCorrectCasing rule.
VERBOSE: Found 1 violations.
VERBOSE: Fixed 1 violations.
1..10 | % { ($_ % 2) -eq 0 }Environment data
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.17763.316
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.316
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
PS C:\> (Get-Module -ListAvailable PSScriptAnalyzer).Version | ForEach-Object { $_.ToString() }
1.18.0
Reactions are currently unavailable
Activity
mkht commentedon Apr 6, 2019
I confirmed that the issue is also occurred in the macOS 10.14 with PSCore 6.2 .
bergmeister commentedon Apr 6, 2019
Thanks for reporting this. I had a look at it and the bug seems to be caused by the CommandInfo lookup here, where PSSA takes the first element, when it executes
Get-Command -Name '?' -ErrorAction SilentlyContinue. Executing it locally shows why it replaces it with%then as shown below. The way the CommandInfo lookup function worked has not changed in the last releases in terms of functionality but the addition of the new formatting ruleUseCorrectCasingmakes this issue surface only now although I do not understand why PowerShell returns%and other aliases when being asked for?Concluding, I think we might have to change the default setting of this rule to be not enabled by default in the VS-Code extension to prevent to minimise the damage (although people are encourage to use source control and look at the diff). I'd hope that we can patch PSSA at some point then and modify the rule to look only for certain CommandTypes and not aliases. I'd also like to raise the question in the PowerShell repo as to why PowerShell returns such a result. cc TylerLeonhardt Steve Lee (@SteveL-MSFT)
B-Art commentedon Nov 12, 2023
A bit of topic, but why not use: