Open sandboxFocusImprove this doc

Namespace Metalama.Framework.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.

Validated revision of the code model

Since aspects can modify the code model, it can be beneficial to be aware of which revision of the code model is being validated.

  • The ValidateReferences always validates the source code. References introduced by aspects cannot be validated.

  • By default, fabrics validate the source code. By invoking AfterAllAspects(), fabrics can validate the code model after all aspects have been applied.

  • By default, aspects validate the code as it is before they are executed (see Aspect composition). Call AfterAllAspects() to validate the code after all aspects have been applied, or BeforeAnyAspect() to validate the source code.

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.

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.

ReferenceKindsExtension

Extension methods for ReferenceKinds.

ReferenceValidationContext

The context object passed to the single parameter of validators added using ValidateReferences<TValidator>(Func<TDeclaration, TValidator>).

ReferenceValidator

ValidatorReceiverExtensions

Extension methods for IValidatorReceiver<TDeclaration>.

Validator<TContext>

A base class for validators.

Structs

DeclarationValidationContext

The context object passed to the single parameter of validators added using Validate(ValidatorDelegate<DeclarationValidationContext>).

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.

Interfaces

IValidatorReceiver

The non-generic base interface for IValidatorReceiver<TDeclaration>. Represents a set of declarations to which validators, diagnostics and code fix suggestions can be added. This interface exposes LINQ-like methods that can be combined in complex queries.

IValidatorReceiver<TDeclaration>

Represents a set of declarations to which validators, diagnostics, code fix suggestions, and options can be added. This interface exposes LINQ-like methods that can be combined in complex queries.

IValidatorReceiver<TDeclaration, TTag>

Enums

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.

ReferenceKinds

Enumerates all kinds of references.

ReferenceValidationOptions

Options for validators added by ValidateInboundReferences(Action<ReferenceValidationContext>, ReferenceGranularity, ReferenceKinds, ReferenceValidationOptions) when supplying a delegate.

Delegates

ValidatorDelegate<TContext>

A delegate for validator methods. The TContext type argument can be either DeclarationValidationContext or ReferenceValidationContext.