Skip to content

C#: preserve whitespace in auto-property accessors and null-conditional assignments#7803

Merged
macsux merged 1 commit into
mainfrom
macsux/create-pr-v2
May 27, 2026
Merged

C#: preserve whitespace in auto-property accessors and null-conditional assignments#7803
macsux merged 1 commit into
mainfrom
macsux/create-pr-v2

Conversation

@macsux
Copy link
Copy Markdown
Contributor

@macsux macsux commented May 27, 2026

Summary

  • Auto-implemented accessors like get ; now round-trip: AccessorDeclaration.ExpressionBody holds a J.Empty whose Prefix carries the whitespace between the keyword and the trailing ;. The printer treats an Empty body element as the auto-impl signal and emits ; without =>.
  • Null-conditional targets that resolve to Assignment, AssignmentOperation, or CsUnary (e.g. rgb?.Length = 0, Changed -= OnChanged) now keep their leading whitespace by routing through a shared ApplyConditionalAccessPrefix helper.

Test plan

  • dotnet test covering the new AutoPropertyWithSpaceBeforeSemicolon, NullConditionalPropertyAssignmentPreservesLeadingNewline, and NullConditionalEventUnsubscribePreservesLeadingNewline cases

…al assignments

- Model auto-implemented accessors (e.g. `get ;`) by storing a J.Empty in
  AccessorDeclaration.ExpressionBody whose Prefix carries the whitespace
  between the keyword and the trailing ';'. Printer emits ';' without '=>'
  when the body element is Empty.
- Extend ApplyConditionalAccessPrefix to cover Assignment, AssignmentOperation,
  and CsUnary segments so null-conditional targets (e.g. `rgb?.Length = 0`,
  `Changed -= OnChanged`) keep their leading whitespace on round-trip.
- Add regression tests for both cases.
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite May 27, 2026
@macsux macsux merged commit af065b2 into main May 27, 2026
1 check passed
@macsux macsux deleted the macsux/create-pr-v2 branch May 27, 2026 19:59
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite May 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant