Improve pipeline indentation handling in UseConsistentIndentation rule#2173
Merged
andyleejordan merged 4 commits intoMay 4, 2026
Merged
Conversation
…thub.com/liamjpeters/PSScriptAnalyzer into PowerShell#1933-PipelineIndentationIssue
andyleejordan
approved these changes
May 4, 2026
Member
andyleejordan
left a comment
There was a problem hiding this comment.
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
currentIndentationLevelIncreaseDueToPipelinescounter with aDictionary<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
pipelineAstsso 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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Summary
Two small improvements in the
UseConsistentIndentationrule'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
UseConsistentIndentationrule'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
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.