Open sandboxFocusImprove this doc

Namespace Metalama.Extensions.Validation

This namespace enables validation of your code, the code that utilizes your aspects, or the code that references the code that uses your aspects.

Overview

Aspects can register validators from their implementation of IAspect.BuildAspect*, and fabrics from their implementation of AmendType, AmendNamespace or AmendProject.

From these methods, invoke the amender.SelectMany method exposed on the builder or amender parameter, combined with further calls to Where, Select or SelectMany, then call Validate or ValidateInboundReferences. These methods allow you to register a delegate. This delegate is subsequently called and receives a context object of type DeclarationValidationContext or ReferenceValidationContext. The delegate can then analyze the code or reference, and report diagnostics.

The ReportDiagnostic, SuppressDiagnostic and SuggestCodeFix methods are provided for convenience and utilize Validate.

To validate whether an aspect is eligible for a declaration (which involves validating the compilation before the aspect has been applied), implement the BuildEligibility aspect method.

Class Diagram

classDiagram

    class ValidatorDelegate~DeclarationValidationContext~{
        <<delegate>>
        Invoke(context)
    }

     class ` ValidatorDelegate~ReferenceValidationContext~`{
        <<delegate>>
        Invoke(context)
    }

    class DeclarationValidationContext {
        AspectState
        Declaration
        Diagnostics
    }

    class ReferenceValidationContext {
        AspectState
        ReferenceEnd Origin
        ReferenceEnd Destination
        Syntax
    }

class InboundReferenceValidator {
    ValidateReferences
}

    class ReferenceEnd {
        Declaration
        Member
        Type
        Namespace
        Assembly
        References
    }

    class ReferenceInstance {
        DiagnosticLocation
        ReferencingDeclaration
        Source
        ReferenceKind
    }


    class IValidatorReceiver {
        Select()
        SelectMany()
        Where()
        AfterAllAspects()
        BeforeAnyAspect()
        ValidateInboundReferences()
        Validate()
        ReportDiagnostic()
        SuppressDiagnostic()
        SuggestCodeFix()
    }

    class IAspectBuilder {
    Outbound
    }

    ValidatorDelegate~DeclarationValidationContext~ <-- IValidatorReceiver : registers
    ` ValidatorDelegate~ReferenceValidationContext~` <-- IValidatorReceiver : registers
    InboundReferenceValidator <-- IValidatorReceiver : registers

    DeclarationValidationContext <-- ValidatorDelegate~DeclarationValidationContext~  : receives
    DeclarationValidationContext <-- InboundReferenceValidator : receives
    ReferenceValidationContext <-- ` ValidatorDelegate~ReferenceValidationContext~` : receives
    ReferenceValidationContext o-- ReferenceEnd
    ReferenceEnd o-- ReferenceInstance



    IValidatorReceiver <|-- IAmender : derives from
    IAspectBuilder <-- IAspect : receives
    IAmender <-- Fabric : receives
     IValidatorReceiver <-- IAspectBuilder : exposes


Namespace Members

Classes

BaseReferenceValidator

Abstract base class for validator of code references.

DeclarationValidationContext

The context object passed to the single parameter of validators added using ValidationQueryExtensions methods.

InboundReferenceValidator

A reference validator that validates outbound references. Its ValidateReferences(ReferenceValidationContext) method will be called for all declarations that are referencing the declaration to which this validator is added.

ReferenceEnd

Represents an end of a code reference. Code references have two ends: a referenced one (the Origin end) and a referencing one (the Destination end).

ReferenceGranularityExtension

Extensions of the ReferenceGranularity enum.

ReferenceValidationContext

The context object passed to the single parameter of validators added by extension methods of the ReferenceValidationQueryExtensions class.

ReferenceValidationQueryExtensions

ReferenceValidator

ValidationQueryExtensions

Validator<TContext>

A base class for validators.

Structs

DeclarationValidationOptions

ReferenceDetail

Represents a single reference in a ReferenceValidationContext. This class is exposed by the Details property.

ReferenceValidationDiagnosticSink

Encapsulates an IDiagnosticSink and the default target of diagnostics, suppressions, and code fixes.

Enums

DeclarationValidationTime

ReferenceEndRole

Identifies the role of the ReferenceEnd.

ReferenceGranularity

Levels of granularity on which a validation can be performed. The order of enum values are from the coarsest to the finest level. The finer the granularity of a validator, the more resources it will consume.

ReferenceValidationOptions

Options for validators added by ReferenceValidationQueryExtensions methods when supplying a delegate.