GeezerVison is showing github.com — Improve pipeline indentation handling in UseConsistentIndentation rule by liamjpeters · Pull Request #2173 · PowerShell/PSScriptAnalyzer
Conversation
PR Summary
-
Two small improvements in the UseConsistentIndentation rule's handing of pipeline indentation. Flagged in this issue and issue comment.
-
Rule never finds the ends of nested pipelines, as it had a fast-return when it reached a pipeline who's end position was beyond the current tokens line. It would always find the outer/parent pipeline (which contains the nested pipeline and spans past it) and return early, empty-handed.
This would lead to some cases of "runaway" indents.
PR sorts the pipelines, in place, by their end line number and column number. This allows the nested pipeline to be found first (as they end before their parent pipeline).
-
Rule was using a single counter to keep track of pipeline indentation and then removing it all when it found a pipeline end.
There were some edge cases that were, even with the first issue resolved, not quite right.
PR adds in tracking of each pipeline's indentation contribution separately.
-
Small improvements in the UseConsistentIndentation rule's handling of indentation where opening braces are on the same line. Previously, adjacent openers like .foreach({, @(@{, or ([PSCustomObject]@{ each incremented the indentation level, producing double or triple indentation. Now only the last unclosed opener on a line affects indentation, fixing common patterns like .foreach({...}) and $list.Add([PSCustomObject]@{...}).
Copilot generated (and I reviewed) a bunch of tests to cover these cases. It's really getting quite good at generating tests now 🤖
Fixes #1680
Fixes #1933
Fixes #2159
Potentially also Fixes the main issue raised in #1168
PR Checklist
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work — this hits #1933, #1680, and #2159 with one coherent change.
The two structural moves are exactly the ones the bugs called for:
- Replacing the flat
currentIndentationLevelIncreaseDueToPipelines counter with a Dictionary<PipelineAst, int> so each pipeline's contribution is unwound only when that pipeline ends — that's the actual fix for the runaway-indent-on-nested-pipelines bug.
- The end-position sort on
pipelineAsts so inner pipelines are matched before outer ones (and the comment explaining why MatchingPipelineAstEnd's early-break needs that ordering) is a nice clarity improvement.
The unification of paren/curly opener handling under a single openerSkippedIndentation stack with HasUnclosedOpenerBeforeLineEnd is a clean refactor of the previous lParenSkippedIndentation-only logic, and the depth-relative scan (return false on our own closer, true on a nested unclosed opener at line end) is correct for the multiple-openers-on-a-line cases. All 90 tests pass locally.
LGTM — approving.
— Drafted by Copilot (Claude Opus 4.7)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|