< Summary

Information
Class: Elsa.Expressions.JavaScript.TypeDefinitions.Services.TypeDescriber
Assembly: Elsa.Expressions.JavaScript
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/TypeDefinitions/Services/TypeDescriber.cs
Line coverage
96%
Covered lines: 58
Uncovered lines: 2
Coverable lines: 60
Total lines: 116
Line coverage: 96.6%
Branch coverage
89%
Covered branches: 25
Total branches: 28
Branch coverage: 89.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
DescribeType(...)100%11100%
GetMethodDefinitions()100%66100%
GetMethodParameters()100%44100%
GetPropertyDefinitions()100%88100%
GetDeclarationKeyword(...)70%121075%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/TypeDefinitions/Services/TypeDescriber.cs

#LineLine coverage
 1using System.Diagnostics.CodeAnalysis;
 2using System.Reflection;
 3using System.Runtime.CompilerServices;
 4using Elsa.Extensions;
 5using Elsa.Expressions.JavaScript.Contracts;
 6using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts;
 7using Elsa.Expressions.JavaScript.TypeDefinitions.Models;
 8
 9namespace Elsa.Expressions.JavaScript.TypeDefinitions.Services;
 10
 11/// <inheritdoc />
 12public class TypeDescriber : ITypeDescriber
 13{
 14    private readonly ITypeAliasRegistry _typeAliasRegistry;
 15
 16    /// <summary>
 17    /// Constructor.
 18    /// </summary>
 40119    public TypeDescriber(ITypeAliasRegistry typeAliasRegistry)
 20    {
 40121        _typeAliasRegistry = typeAliasRegistry;
 40122    }
 23
 24    /// <inheritdoc />
 25    public TypeDefinition DescribeType(Type type)
 26    {
 827        var typeDefinition = new TypeDefinition
 828        {
 829            DeclarationKeyword = GetDeclarationKeyword(type),
 830            Name = type.Name,
 3931            Properties = GetPropertyDefinitions(type).DistinctBy(x => x.Name).ToList(),
 21932            Methods = GetMethodDefinitions(type).DistinctBy(x => x.Name).ToList()
 833        };
 34
 835        return typeDefinition;
 36    }
 37
 38    private IEnumerable<FunctionDefinition> GetMethodDefinitions(Type type)
 39    {
 840        if(type.IsEnum)
 141            yield break;
 42
 43#pragma warning disable IL2070
 744        var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
 33645            .Where(x => !x.IsSpecialName)
 21946            .Where(x => x.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
 747            .ToList();
 48#pragma warning restore IL2070
 49
 45250        foreach (var method in methods)
 51        {
 21952            yield return new FunctionDefinition
 21953            {
 21954                Name = method.Name,
 21955                Parameters = GetMethodParameters(method).ToList(),
 21956                ReturnType = _typeAliasRegistry.TryGetAlias(method.ReturnType, out var alias) ? alias : "any"
 21957            };
 58        }
 759    }
 60
 61    private IEnumerable<ParameterDefinition> GetMethodParameters(MethodInfo method)
 62    {
 21963        var parameters = method.GetParameters();
 64
 101065        foreach (var parameter in parameters)
 66        {
 28667            yield return new ParameterDefinition
 28668            {
 28669                Name = parameter.Name!,
 28670                Type = _typeAliasRegistry.TryGetAlias(parameter.ParameterType, out var alias) ? alias : "any",
 28671                IsOptional = parameter.IsOptional
 28672            };
 73        }
 21974    }
 75
 76    private IEnumerable<PropertyDefinition> GetPropertyDefinitions([DynamicallyAccessedMembers(DynamicallyAccessedMember
 77    {
 78        // If the type is an enum, enumerate its members.
 879        if (type.IsEnum)
 80        {
 881            foreach (var name in Enum.GetNames(type))
 82            {
 383                yield return new PropertyDefinition
 384                {
 385                    Name = name,
 386                    Type = "string",
 387                    IsOptional = false,
 388                };
 89            }
 90
 191            yield break;
 92        }
 93
 794        var properties = type.GetProperties();
 95
 8696        foreach (var property in properties)
 97        {
 3698            yield return new PropertyDefinition
 3699            {
 36100                Name = property.Name,
 36101                Type = _typeAliasRegistry.TryGetAlias(property.PropertyType, out var alias) ? alias : "any",
 36102                IsOptional = property.PropertyType.IsNullableType()
 36103            };
 104        }
 7105    }
 106
 107    private static string GetDeclarationKeyword(Type type) =>
 8108        type switch
 8109        {
 0110            { IsInterface: true } => "interface",
 6111            { IsClass: true } => "class",
 1112            { IsEnum: true } => "enum",
 1113            { IsValueType: true, IsEnum: false } => "class",
 0114            _ => "interface"
 8115        };
 116}