Metalama / / API Documentation / Aspect API / Metalama.​Framework.​Validation

Namespace Metalama.Framework.Validation

This is namespace allows you to validate your code, the code that uses your aspects, or the code that references the code that uses your aspects.

Overview

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

From these methods, call the amender.With method exposed on the builder or amender parameter, then call Validate or ValidateReferences. These methods allow you to register a delegate. This delegate is then called and receive 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 use 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 useful to be aware of which revision of the code model is validated.

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

  • By default, fabrics validate the source code. By calling 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 DiagnosticLocation ReferencedDeclaration ReferencedType ReferenceKinds Syntax } class IValidatorReceiver { ValidateReferences() Validate() ReportDiagnostic() SuppressDiagnostic() SuggestCodeFix() } ValidatorDelegate~DeclarationValidationContext~ <-- IValidatorReceiver : registers ` ValidatorDelegate~ReferenceValidationContext~` <-- IValidatorReceiver : registers DeclarationValidationContext <-- ValidatorDelegate~DeclarationValidationContext~ : receives ReferenceValidationContext <-- ` ValidatorDelegate~ReferenceValidationContext~` : receives class IValidatorReceiverSelector { With() AfterAllAspects() BeforeAnyAspect() } IValidatorReceiver <-- IValidatorReceiverSelector : creates IValidatorReceiverSelector <-- IValidatorReceiverSelector IValidatorReceiverSelector <|-- IAspectBuilder : derives from IValidatorReceiverSelector <|-- IAmender : derives from IAspectBuilder <-- IAspect : receives IAmender <-- Fabric : receives

Namespace Members

Classes

DeclarationValidationContext

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

ReferenceKinds

Enumerates all kinds of references.

ReferenceValidationContext

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

ReferenceValidator

A validator that can validate code references.

Validator<TContext>

A base class for validators.

ValidatorDelegate<TContext>

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

Interfaces

IValidatorReceiver

IValidatorReceiver<TDeclaration>

IValidatorReceiverSelector<TTarget>

An interface that allows aspects and fabrics to register validators for the initial or final compilation version.