< 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
5%
Covered lines: 3
Uncovered lines: 57
Coverable lines: 60
Total lines: 116
Line coverage: 5%
Branch coverage
0%
Covered branches: 0
Total branches: 28
Branch coverage: 0%
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%210%
GetMethodDefinitions()0%4260%
GetMethodParameters()0%2040%
GetPropertyDefinitions()0%7280%
GetDeclarationKeyword(...)0%110100%

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>
 32319    public TypeDescriber(ITypeAliasRegistry typeAliasRegistry)
 20    {
 32321        _typeAliasRegistry = typeAliasRegistry;
 32322    }
 23
 24    /// <inheritdoc />
 25    public TypeDefinition DescribeType(Type type)
 26    {
 027        var typeDefinition = new TypeDefinition
 028        {
 029            DeclarationKeyword = GetDeclarationKeyword(type),
 030            Name = type.Name,
 031            Properties = GetPropertyDefinitions(type).DistinctBy(x => x.Name).ToList(),
 032            Methods = GetMethodDefinitions(type).DistinctBy(x => x.Name).ToList()
 033        };
 34
 035        return typeDefinition;
 36    }
 37
 38    private IEnumerable<FunctionDefinition> GetMethodDefinitions(Type type)
 39    {
 040        if(type.IsEnum)
 041            yield break;
 42
 43#pragma warning disable IL2070
 044        var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
 045            .Where(x => !x.IsSpecialName)
 046            .Where(x => x.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
 047            .ToList();
 48#pragma warning restore IL2070
 49
 050        foreach (var method in methods)
 51        {
 052            yield return new FunctionDefinition
 053            {
 054                Name = method.Name,
 055                Parameters = GetMethodParameters(method).ToList(),
 056                ReturnType = _typeAliasRegistry.TryGetAlias(method.ReturnType, out var alias) ? alias : "any"
 057            };
 58        }
 059    }
 60
 61    private IEnumerable<ParameterDefinition> GetMethodParameters(MethodInfo method)
 62    {
 063        var parameters = method.GetParameters();
 64
 065        foreach (var parameter in parameters)
 66        {
 067            yield return new ParameterDefinition
 068            {
 069                Name = parameter.Name!,
 070                Type = _typeAliasRegistry.TryGetAlias(parameter.ParameterType, out var alias) ? alias : "any",
 071                IsOptional = parameter.IsOptional
 072            };
 73        }
 074    }
 75
 76    private IEnumerable<PropertyDefinition> GetPropertyDefinitions([DynamicallyAccessedMembers(DynamicallyAccessedMember
 77    {
 78        // If the type is an enum, enumerate its members.
 079        if (type.IsEnum)
 80        {
 081            foreach (var name in Enum.GetNames(type))
 82            {
 083                yield return new PropertyDefinition
 084                {
 085                    Name = name,
 086                    Type = "string",
 087                    IsOptional = false,
 088                };
 89            }
 90
 091            yield break;
 92        }
 93
 094        var properties = type.GetProperties();
 95
 096        foreach (var property in properties)
 97        {
 098            yield return new PropertyDefinition
 099            {
 0100                Name = property.Name,
 0101                Type = _typeAliasRegistry.TryGetAlias(property.PropertyType, out var alias) ? alias : "any",
 0102                IsOptional = property.PropertyType.IsNullableType()
 0103            };
 104        }
 0105    }
 106
 107    private static string GetDeclarationKeyword(Type type) =>
 0108        type switch
 0109        {
 0110            { IsInterface: true } => "interface",
 0111            { IsClass: true } => "class",
 0112            { IsEnum: true } => "enum",
 0113            { IsValueType: true, IsEnum: false } => "class",
 0114            _ => "interface"
 0115        };
 116}