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
It seems to be making the implicit assumption that the direct children of the CommandAst are read from the AST in token order. This is, apparently, not the case.
Looking at the AST of our example Invoke-Foo 3>&1 1>&1 2>&1:
We see that evidentially, the MergingRedirectionAst nodes are read from the tree in stream order (very interesting!).
Note the start of each MergingRedirectionAst nodes extent - the number after [ - is not in order down the list. The first node read from the tree is the second token, 1>&1, followed by the last token, 2>&1, and finally the first of the redirects in our input command, 3>&1.
I'm not smart enough to confirm this within the main Powershell Repo's parser - but I've tried with various orders of Invoke-Foo 3>&1 2>&1 1>&1 5>&1 4>&1 6>&1 and it appears to be consistent.
My unrefined naive solution would be to sort the command parameters (the direct children of the CommandAst), first by their Extents StartLineNumber, then by the StartColumnNumber. Then proceed as planned.
This works as expected and our repro code correctly produces:
Invoke-Foo3>&11>&12>&1
I would need to look at how badly this sorting impacts performance and for any edge cases.
Activity
SydneyhSmith commentedon May 7, 2024
Thanks @nixuno we were able to reproduce this
liamjpeters commentedon May 13, 2024
This is quite an interesting one 😀. A slightly simplified repro in PSScriptAnalyzer is:
Results in:
The
PSUseConsistentWhitespacerule, whenCheckParameteristrue, is looking over all theCommandAstnodes.For each
CommandAst, it's finding all the direct children of the currentCommandAstnode.PSScriptAnalyzer/Rules/UseConsistentWhitespace.cs
Lines 399 to 400 in e1dc126
It's checking then that each one sequentially is separated by a 1 character gap.
PSScriptAnalyzer/Rules/UseConsistentWhitespace.cs
Lines 410 to 411 in e1dc126
It seems to be making the implicit assumption that the direct children of the
CommandAstare read from the AST in token order. This is, apparently, not the case.Looking at the AST of our example
Invoke-Foo 3>&1 1>&1 2>&1:We see that evidentially, the
MergingRedirectionAstnodes are read from the tree in stream order (very interesting!).I'm not smart enough to confirm this within the main Powershell Repo's parser - but I've tried with various orders of
Invoke-Foo 3>&1 2>&1 1>&1 5>&1 4>&1 6>&1and it appears to be consistent.My unrefined naive solution would be to sort the command parameters (the direct children of the
CommandAst), first by their ExtentsStartLineNumber, then by theStartColumnNumber. Then proceed as planned.This works as expected and our repro code correctly produces:
I would need to look at how badly this sorting impacts performance and for any edge cases.