Preserve braced member access in UseConsistentWhitespace
#2140
+475
−0
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.
PR Summary
UseConsistentWhitespace'sCheckInnerBracebreaks braced member access ($a.{Prop}) by inserting whitespace after the opening and before the closing curly brace ($a.{ Prop }). The inner content of the braces must match the referenced property name exactly. No formatting of the inner content should occur.Unlike the variable curly-brace format,
${a}, which is tokenised as a variable, no special tokenisation happens for this bracing.PR introduces a new function,
GetBracedMemberAccessRanges(), inTokenOperations.cs, which walks the token list looking for the shape of a braced member access:The function returns a list of int pairs which represent a range from the startoffset of a LCurly to the endoffset of a RCurly. Formatting within these ranges could break braced member access.
In
UseConsistentWhitespacewhen checking for inner brace violations inFindInnerBraceViolations(), call the function to get the ranges to exclude. Skip whitespace checking for braces within any range.Fixes #2066
When asking copilot to look this over, it told me I could instead have used the AST, looking for
MemberExpressionAstwho'sMemberstarted with a{and ended with a}. I could then infer the offsets of the braces. I could explore that if it's a more desirable/correct solution.PR Checklist
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.