Skip to content

SA1504: preserve comments and blank lines in the multi-line fix - #4311

Merged
AArnott merged 1 commit into
masterfrom
aarnott-sa1504-trivia-preservation
Oct 15, 2026
Merged

AArnott merged 1 commit into
masterfrom
aarnott-sa1504-trivia-preservation

Conversation

@AArnott

@AArnott AArnott commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

The SA1504 multi-line code fix discarded comments and blank lines when rewriting existing accessor bodies, including the other accessor when expanding an expression-bodied getter or setter.

Fixes #4310

Preserve trivia while adding the line breaks and indentation required for multi-line accessors. Keep existing inline block-comment compaction where it cannot discard line comments, directives, or blank lines. Diagnostics and the single-line code action are unchanged; all work remains in the code fix, with no analyzer performance impact.

Regression coverage includes getters and setters, properties and indexers, event accessors, arrow and throw expressions, comment-only bodies, directives, disabled text, and Fix All. The initial four getter/setter property/indexer cases failed against unchanged master product code with diffs showing the reported comment and blank-line loss. Tests use explicit before/after snippets, and the testing skill records the review guidance on readable fixtures and raw interpolated strings.

Validation: full Debug solution build succeeded with zero warnings and errors. All SA1504 tests passed across the C# 6-15 test projects (503 test executions). No rule documentation changes are included because this restores expected behavior.

Keep comments, directives, and blank lines while expanding accessors, without changing diagnostics or single-line conversion. Preserve existing inline-comment formatting and add readable regression coverage and test-authoring guidance.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@AArnott
AArnott marked this pull request as ready for review October 15, 2026 16:41
Copilot AI balanced review requested due to automatic review settings October 15, 2026 16:41
@codecov

codecov Bot commented Oct 15, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.81818% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 97.63%. Comparing base (ea51bca) to head (e060362).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #4311      +/-   ##
==========================================
+ Coverage   97.62%   97.63%   +0.01%     
==========================================
  Files        1308     1308              
  Lines      142251   142748     +497     
  Branches     4749     4756       +7     
==========================================
+ Hits       138869   139377     +508     
+ Misses       2156     2146      -10     
+ Partials     1226     1225       -1     
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@AArnott
AArnott merged commit 9869738 into master Oct 15, 2026
30 checks passed
@AArnott
AArnott deleted the aarnott-sa1504-trivia-preservation branch October 15, 2026 16:43

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Header formatting still removes existing accessor separators and loses indentation after modifier line comments.

2 open findings
What changed in this PR

Updates the SA1504 multi-line code fix to preserve comments and blank lines, addressing #4310 without changing diagnostics or the single-line action.

Changes:

  • Preserves trivia when expanding block and expression-bodied accessors.
  • Adds regression coverage for accessor variants and Fix All.
  • Documents guidance for readable test fixtures.
File Description
test/​StyleCop.Analyzers.Test/​LayoutRules/​SA1504UnitTests.cs Adds block-accessor trivia and Fix All tests.
test/​StyleCop.Analyzers.Test.CSharp7/​LayoutRules/​SA1504ArrowAccessorCSharp7UnitTests.cs Adds expression-bodied accessor regression tests.
src/​StyleCop.Analyzers.CodeFixes/​LayoutRules/​SA1504CodeFixProvider.cs Revises multi-line trivia preservation.
.agents/​skills/​testing-and-coverage/​SKILL.md Adds readable fixture guidance.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

.WithTrailingTrivia(newTrailingTrivia);
.WithLeadingTrivia(insertIdentation
? ReformatLeadingTriviaAsMultipleLines(ReformatAccessorHeaderTrivia(keyword.LeadingTrivia), indentation)
: ReformatAccessorHeaderTrivia(keyword.LeadingTrivia))
return trivia.All(t => t.IsKind(SyntaxKind.WhitespaceTrivia)
|| t.IsKind(SyntaxKind.EndOfLineTrivia)
|| t.IsKind(SyntaxKind.MultiLineCommentTrivia))
&& !trivia.ContainsBlankLines(startsOnNewLine: false)
This was referenced Nov 11, 2026
This was referenced Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SA1504 - The fixer do not preserve the comments and empty lines in the get or set block

2 participants