Skip to content

Improve pipeline indentation handling in UseConsistentIndentation rule#2173

Merged
andyleejordan merged 4 commits into
PowerShell:mainfrom
liamjpeters:#1933-PipelineIndentationIssue
May 4, 2026
Merged

Improve pipeline indentation handling in UseConsistentIndentation rule#2173
andyleejordan merged 4 commits into
PowerShell:mainfrom
liamjpeters:#1933-PipelineIndentationIssue

Conversation

@liamjpeters
Copy link
Copy Markdown
Contributor

@liamjpeters liamjpeters commented Apr 2, 2026

PR Summary

  • Two small improvements in the UseConsistentIndentation rule's handing of pipeline indentation. Flagged in this issue and issue comment.

    1. 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).

    2. 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

@liamjpeters liamjpeters requested review from a team and bergmeister as code owners April 2, 2026 12:22
Copy link
Copy Markdown
Member

@andyleejordan andyleejordan left a 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)

@andyleejordan andyleejordan merged commit d97ddd4 into PowerShell:main May 4, 2026
4 checks passed
@liamjpeters liamjpeters deleted the #1933-PipelineIndentationIssue branch May 5, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants