Skip to content

Remove AttributeValueSyntax.(Try)GetRawText and move raw-text projection to Semantics.AttributeValue #14

@jonathanvdc

Description

@jonathanvdc

Description

AttributeValueSyntax currently exposes (Try)GetRawText to project syntax nodes into preserved raw text:

public abstract bool TryGetRawText(out RawSyntaxText? rawText);

public RawSyntaxText GetRawText()
{
    if (TryGetRawText(out var rawText))
    {
        return rawText!;
    }

    throw new System.InvalidOperationException("This attribute value does not provide a raw syntax-text projection.");
}

This mirrors the same layering issue seen with operation bodies (#13): syntax is being used to recover a semantic projection (raw text), and the API encourages consumers to depend on syntax shape rather than semantic structure.

Additionally, GetRawText() is exception-prone and presents an unsafe API surface.

Proposed Change

Remove AttributeValueSyntax.TryGetRawText(...) and AttributeValueSyntax.GetRawText().

Instead, introduce a method or property on Semantics.AttributeValue that provides raw-text projection where applicable.

This semantic API should:

  • return preserved raw syntax text when available, or
  • make the absence of raw text explicit (e.g., via nullability or a Try... pattern—decision left to implementation)

Motivation

  • Raw-text projection is a semantic concern, not a syntactic one
  • Avoids exposing exception-prone APIs on syntax nodes
  • Encourages consumers to work with Semantics.AttributeValue instead of probing syntax
  • Aligns with the direction of removing projection helpers from syntax types
  • Improves separation of concerns between syntax and semantics

Expected Usage After Change

Before:

var raw = attributeValueSyntax.GetRawText();

After (example direction):

if (attributeValue.TryGetRawText(out var raw))
{
    // use raw
}

or:

var raw = attributeValue.RawText; // nullable or optional

Scope

  • Remove:
    • AttributeValueSyntax.TryGetRawText(...)
    • AttributeValueSyntax.GetRawText()
  • Add a raw-text projection API to Semantics.AttributeValue
  • Update all call sites to use the semantic API instead of syntax
  • Ensure generators (if applicable) do not depend on syntax-level raw-text extraction

Benefits

  • Eliminates exception-prone APIs from syntax layer
  • Improves architectural clarity (syntax vs semantics)
  • Reduces misuse of syntax as a semantic data source
  • Moves toward a more consistent API design across the codebase

Notes

  • The new API should clearly communicate when raw text is unavailable
  • Consider whether raw-text preservation needs to be optional or guaranteed for certain attribute kinds
  • Audit for any other syntax-level projection helpers that should follow the same pattern

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions