Skip to content

Extract shared helpers for source span merging and duplicated resolution logic #13001

Description

@chsienki

Background

PR #12957's resolution phase has duplicated logic in two areas:

  1. 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.

  2. 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)

Metadata

Metadata

Labels

area-compilerUmbrella for all compiler issues

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions