Open o-l-a-v opened 4 months ago
This looks to be being caused by rule PSAlignAssignmentStatement
, not PSUseConsistentWhitespace
.
The simplest case where I can reproduce this is:
Invoke-Formatter -ScriptDefinition "@{'Key'='Value'}" -Settings @{
Rules = @{
PSAlignAssignmentStatement = @{
Enable = $true
CheckHashtable = $true
}
}
}
Results in:
@{'Key' ='Value'}
The rule seems to be opinionated about there being a single space after the key name when it does it's alignment.
The rule has some logic to check if the hashtable is on multiple lines and, if not, should not check it for violations. This logic does not take into account the case where the hashtable has a single key-value pair. The function HasPropertiesOnSeparateLines
returns true in this case.
This is why in the test case @o-l-a-v highlighted, the property hashtable passed to Sort-Object
is formatted (It has a single key-value pair), whereas the property hashtable passed to Select-Object
is not formatted (it has 2 key-value pairs on the same line).
So:
Invoke-Formatter -ScriptDefinition "@{'Key'='Value';'key2'='value2'}" -Settings @{
Rules = @{
PSAlignAssignmentStatement = @{
Enable = $true
CheckHashtable = $true
}
}
}
Results in:
@{'Key'='Value';'key2'='value2'}
@o-l-a-v - If you set the below, you won't see this behaviour - you'll also not get nice alignment of your hashtable property-value pairs though 😅.
"powershell.codeFormatting.alignPropertyValuePairs": false
I can do a small PR to add a check to see if there's just the 1 key-value pair, and if so, not to check it for violations - that should resolve this - assuming that's the desired behaviour.
Great reasearch @liamjpeters. Your described behavior for a fix is what I'd want.
Great details. Happy to support you in making a pull request, from my own experience I know though it's hard to write generic code without special cases like that and fixing them without breaking something else, we have got good test coverage though.
Prerequisites
Summary
vscode-powershell inserts a space before an operator in some cases, even with:
PowerShell Version
Visual Studio Code Version
Extension Version
Steps to Reproduce
What it ends up looking like:
Notice it only adds a whitespace after
'Expression'
inSort-Object -Property
.Visuals
No response
Logs
No response