Background
PR #12957's resolution phase has duplicated logic in two areas:
-
Source span merging: The pattern of computing a merged SourceSpan from a first and last child span appears in multiple places (both ComponentTagHelperResolver and LegacyTagHelperResolver). This should be extracted into a shared helper.
-
Duplicated condition branches: Some condition branches in the resolvers share identical lines of code that could be factored out.
What to change
1. Extract MergeSourceSpans helper
Several places compute a merged span like:
var mergedSpan = new SourceSpan(
firstSource.FilePath,
firstSource.AbsoluteIndex,
firstSource.LineIndex,
firstSource.CharacterIndex,
lastSource.AbsoluteIndex + lastSource.Length - firstSource.AbsoluteIndex,
lastSource.LineIndex - firstSource.LineIndex,
lastSource.EndCharacterIndex);
Extract this into a static helper method, e.g.:
private static SourceSpan MergeSourceSpans(SourceSpan first, SourceSpan last)
2. Share duplicated lines between conditions
Review the resolver methods for condition branches that have identical setup/teardown code and factor common lines out of the if/else.
Files to update
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultTagHelperResolutionPhase.ComponentTagHelperResolver.cs
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultTagHelperResolutionPhase.LegacyTagHelperResolver.cs
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultTagHelperResolutionPhase.cs
Notes
- Pure refactoring — no behavioral changes.
- Search for
AbsoluteIndex + .*Length - .*AbsoluteIndex to find the span merging sites.
ref: PR #12957 review comments #12957 (comment) and #12957 (comment)
Background
PR #12957's resolution phase has duplicated logic in two areas:
Source span merging: The pattern of computing a merged
SourceSpanfrom a first and last child span appears in multiple places (both ComponentTagHelperResolver and LegacyTagHelperResolver). This should be extracted into a shared helper.Duplicated condition branches: Some condition branches in the resolvers share identical lines of code that could be factored out.
What to change
1. Extract
MergeSourceSpanshelperSeveral places compute a merged span like:
Extract this into a static helper method, e.g.:
2. Share duplicated lines between conditions
Review the resolver methods for condition branches that have identical setup/teardown code and factor common lines out of the if/else.
Files to update
src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultTagHelperResolutionPhase.ComponentTagHelperResolver.cssrc/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultTagHelperResolutionPhase.LegacyTagHelperResolver.cssrc/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/DefaultTagHelperResolutionPhase.csNotes
AbsoluteIndex + .*Length - .*AbsoluteIndexto find the span merging sites.ref: PR #12957 review comments #12957 (comment) and #12957 (comment)