You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
PSUseConsistentWhitespace with IgnoreAssignmentOperatorInsideHashTable = $true
PSAlignAssignmentStatement with Enabled = $true and CheckHashTable = $true
It generally works as advertised... except...
When a using statement is present... we get a "Use space before and after binary and assignment operators.PSScriptAnalyzer(PSUseConsistentWhitespace)" error.
$SoHappy=@{
not='other'theSame='casting sucks.'
}
``
Expected behavior
-----------------
No linting error.
Actual behavior
---------------
Seems to check inside hashtables when a using statement of ANY kind is present. The fixer works still though so it doesn't feel like something inherent with the type, maybe something to do with how using are tokenized? D:\Symphony\devops-scripts\Symphony\Symphony.Azdo\Happy.ps1Environment dataD:\Symphony\devops-scripts\Symphony\Symphony.Azdo\Sad.ps1----------------Present in a bunch of envs including our build farm...and using the latest version (1.24.0)Attaching file[PSScriptAnalyzerSettings.txt](https://github.com/user-attachments/files/19352089/PSScriptAnalyzerSettings.txt)
I pulled the code to see what was happening cause I was curious, it seems to come down to somewhat weird behavior of the UsingStatementAst vs other kinds of Asts like say ScriptBlockAst which flip the SkipChildren to a Continue inside it's the internalvisit but the internal visit of the UsingStatementAst just passes the SkipChildren through instead of flipping it to a continue when visiting a
I can't tell if this is a bug in the system automation stuff or some weird expected behavior, if you parse a file with using statements, once the visitor hits the script block, sets that as it's visit and then exits out after hitting the UsingStatementAst (usually the next one in my testing),
as a result the type of the AST returned is a ScriptBlockAst instead of a HashTableAst here the UseConsistantWhiteSpace.cs and an error is reported instead of being skipped...
Quick Fix:
I don't believe PSScriptAnalyzer does anything related using statements currently? if that's accurate then simply returning AstVisitAction.Continue instead of passing the UsingStatementAst down to your visit function function fixed it locally for me.
Otherwise some specialized logic inside your Visit function will likely be required. Of course it could also be it's a bug in the system automation but I feel like that'll be longer fix unless y'all are on the same team :)
Interesting. Feel free to try submit a potential fix, we have a suite of tests that will tell if anything else breaks so it's worth a try. PSSA just gets the AST from PowerShell's parser and works off that with the code that you've seen. You can use the ShowPsAst module to look at the AST whether the using statement makes a difference to what gets passed to PSSA, if that is not different then it should be fixable within PSSA
Activity
AlexandraDorey commentedon Mar 20, 2025
Well that was interesting...->
I pulled the code to see what was happening cause I was curious, it seems to come down to somewhat weird behavior of the UsingStatementAst vs other kinds of Asts like say ScriptBlockAst which flip the SkipChildren to a Continue inside it's the internalvisit but the internal visit of the UsingStatementAst just passes the SkipChildren through instead of flipping it to a continue when visiting a
InternalVisit of UsingStatementAst
vs
Internal Visit of NamedBlock
I can't tell if this is a bug in the system automation stuff or some weird expected behavior, if you parse a file with using statements, once the visitor hits the script block, sets that as it's visit and then exits out after hitting the UsingStatementAst (usually the next one in my testing),
Investigation reproduction:
Input to pester test passing:
Passing output of ast tree:
Input script when failing:
Failing output of ast tree:
I added some logging inside the Visit function of the FindAstPositionVisitor.cs class
and the output when passing was:
and when failing it was:
as a result the type of the AST returned is a ScriptBlockAst instead of a HashTableAst here the UseConsistantWhiteSpace.cs and an error is reported instead of being skipped...
Quick Fix:
I don't believe PSScriptAnalyzer does anything related using statements currently? if that's accurate then simply returning AstVisitAction.Continue instead of passing the UsingStatementAst down to your visit function function fixed it locally for me.
Otherwise some specialized logic inside your Visit function will likely be required. Of course it could also be it's a bug in the system automation but I feel like that'll be longer fix unless y'all are on the same team :)
bergmeister commentedon Apr 1, 2025
Interesting. Feel free to try submit a potential fix, we have a suite of tests that will tell if anything else breaks so it's worth a try. PSSA just gets the AST from PowerShell's parser and works off that with the code that you've seen. You can use the ShowPsAst module to look at the AST whether the using statement makes a difference to what gets passed to PSSA, if that is not different then it should be fixable within PSSA
AlexandraDorey commentedon Apr 1, 2025
Created a Pull request if you could have it run the tests when you get a chance, they seem to be passing locally.
#2091
bergmeister commentedon Apr 4, 2025
Fixed by #2091