You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SA1003: Preserve constructor initializer line breaks in code fix - #4280
The SA1003 code fix collapsed a constructor initializer onto the constructor declaration when the colon was at the end of a line. The fix now moves the colon to the initializer's line, retaining the source line break, indentation, and comments. SA1128 is unchanged.
The regression test failed on master with a fixed-text diff: master produced public MyType() : base(3) instead of the expected separate-line initializer. It also verifies two constructors, preserved comments, configured indentation settings, and Fix All.
Keep a constructor initializer on its own indented line when fixing a colon at end of line. Add regression and Fix All coverage, and document the resulting layout.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Mirror the new constructor initializer example to satisfy documentation localization parity.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Keep the colon's leading trivia when it contains a single-line comment so the line-comment terminator is not lost during relocation. Add a regression test for comments before the colon.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
This drops any EndOfLineTrivia owned by followingToken: the loop keeps only whitespace, and line 121 then removes the complete leading trivia list. With a blank line between : and base/this, the fix therefore deletes that blank line and concatenates its whitespace into the colon's indentation. Preserve the full whitespace/end-of-line sequence (and add a blank-line regression case) so this trivia-preserving path does not alter unrelated layout.
Keep the Czech SA1003 edition current with the new constructor initializer guidance and example.\n\nCo-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
When the constructor declaration already ends with an inline comment, its trailing trivia includes the comment-terminating end-of-line. WithoutTrailingWhitespace() intentionally preserves that terminator, so appending the colon's end-of-line adds a blank line: MyType() // note\n :\n base() becomes MyType() // note\n\n : base(). Keep only one terminating end-of-line here, and add this inline-comment case to the regression test.
This special case only recognizes // trivia. A block comment on the colon's line (for example, MyType()\n /* note */ :\n base()) is also stored in token.LeadingTrivia, but it takes the else branch: WithoutLeadingWhitespace() removes the comment's indentation and the initializer indentation is then inserted between the comment and colon, yielding /* note */ : base(). Treat any leading comment/non-whitespace trivia like the single-line-comment case, and add the equivalent regression so the fix preserves both the comment and its layout.
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.63%. Comparing base (dfaef0c) to head (aa54c89). ⚠️ Report is 5 commits behind head on master.
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
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.
Fixes #2613
The SA1003 code fix collapsed a constructor initializer onto the constructor declaration when the colon was at the end of a line. The fix now moves the colon to the initializer's line, retaining the source line break, indentation, and comments. SA1128 is unchanged.
The regression test failed on master with a fixed-text diff: master produced
public MyType() : base(3)instead of the expected separate-line initializer. It also verifies two constructors, preserved comments, configured indentation settings, and Fix All.Validation:
dotnet test test\StyleCop.Analyzers.Test --framework net10.0 --filter FullyQualifiedName~SA1003(31 passed); latestStyleCop.Analyzers.Test.CSharp15SA1003 and SA1128 suites (64 passed);dotnet build StyleCopAnalyzers.slnx --configuration Debug --no-restore --verbosity minimal -m:1(0 warnings, 0 errors).Performance: analyzer code is unchanged; the added syntax/trivia work runs only when the SA1003 code fix is invoked.