-
Notifications
You must be signed in to change notification settings - Fork 393
PSUseConsistantWhiteSpace #2089
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
Comments
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 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:#using namespace somethin
$ht = @{
variable = 3
other = 4
}
$alpha = @{
beta =
delta = 4
}
'@ Passing output of ast tree:
Input script when failing:using namespace something
$ht = @{
variable = 3
other = 4
}
$alpha = @{
beta =
delta = 4
}
'@ Failing output of ast tree:
I added some logging inside the Visit function of the FindAstPositionVisitor.cs class private AstVisitAction Visit(Ast ast)
{
Console.WriteLine($"Visiting {ast.GetType().Name}");
if (ast.Extent.StartOffset > searchPosition.Offset || ast.Extent.EndOffset <= searchPosition.Offset)
{
Console.WriteLine($" Skipping Kids...");
return AstVisitAction.SkipChildren;
}
Console.WriteLine($" Setting As Position...");
AstPosition = ast;
return AstVisitAction.Continue;
} 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 :) |
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 |
Created a Pull request if you could have it run the tests when you get a chance, they seem to be passing locally. |
Fixed by #2091 |
Steps to reproduce
Attaching our PSD fille for the analyzer config
We have
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.
This does not work ...
This works in our setup...
The text was updated successfully, but these errors were encountered: