Format and lint blank lines when selections start or end on blank lines #1338
All Vouch review confidence checks passed
Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 447 lines diff across 5 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.365
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #1338"
general
weight: 0.35
dylansturg
Summary
When formatting or linting a line range (
--lines) that includes blanklines before the first selected token (or selects only blank lines),
swift-format formatandswift-format lintpreviously ignoredmaximumBlankLinesunless the token preceding the blank lines was alsoselected, and leaked formatting onto the unselected line following a
selected blank line:
Several interactions caused this:
1. In SwiftSyntax, blank lines between two tokens (or at the start of a
file) live in the following token's
leadingTrivia, whichTokenStreamCreator.visit(_ token:)processes inextractLeadingTriviabefore callinggenerateEnableFormattingIfNecessaryattoken.positionAfterSkippingLeadingTrivia. When the preceding tokenwas outside the selection,
appendNewlinesmerged the blank linesinto
tokens[lastBreakIndex]while formatting was still disabled,and
.enableFormattingattoken.positionAfterSkippingLeadingTriviacopied all preceding newlines verbatim from the original source.
2. Conversely, when a selection included a token and ended on a
following blank line (
--lines 2:3),extractLeadingTrivianevercalled
generateDisableFormattingIfNecessaryafter the newline run,so formatting remained enabled for the first token on the next
unselected line and re-indented it. Similarly, disabling formatting
at
endOfFileToken.endPositionBeforeTrailingTriviabeforeappendAfterTokensAndTrailingComments(endOfFileToken)dropped thefile's final trailing newline when a selection ended on trailing
blank lines at EOF.
3. In
PrettyPrintBuffer.writeNewlines, blank-line indentation checkednumber >= 1rather thannumber + consecutiveNewlineCount >= 1,skipping indentation on the first blank line when
writeVerbatimAfterEnablingFormattinghad already written thepreceding line's terminating newline (
consecutiveNewlineCount == 1).4. In
WhitespaceLinter.compareWhitespace, excess line breaks were onlydiagnosed on the first
excessUserLinesruns (starting at theterminating newline of the unselected preceding line, which
WhitespaceLinter.diagnosefiltered out as outside the selection),and
formattedRunsIterator.next()was advanced even on deleted userlines, causing a spurious
[TrailingWhitespace]finding when excessblank lines preceded an indented line.
Fix all four places:
extractLeadingTrivia(and trailing blank lines at
endOfFileToken). When a run containsat least one blank line whose range overlaps or touches
selection,enable formatting at the start of the first blank line before the
break token, and call
generateDisableFormattingIfNecessaryat theend of the newline run.
after(node.endOfFileToken)onendOfFileTokenso the final newline at EOF is preserved.number + consecutiveNewlineCount >= 1when indenting blanklines in
PrettyPrintBuffer.writeNewlines.WhitespaceLinter.compareWhitespace, attributeremainingExcessLinesto newline runs withincontext.selectionandonly advance
formattedRunsIteratoron non-deleted runs