Refactor TypeDefinition rendering and improve VariableTypeDefinitionProvider

- Adjusted `RenderEnumMember` to include a trailing comma in rendered enums.
- Excluded compiler-generated methods in `TypeDescriber`.
- Extended `VariableTypeDefinitionProvider` to handle `ManagementOptions` and refined variable type exclusion logic.
This commit is contained in:
Sipke Schoorstra 2025-09-16 21:40:18 +02:00
parent d4a2a5f03a
commit 07986de7a8
No known key found for this signature in database
GPG key ID: 5C10502B28A4268F
3 changed files with 21 additions and 15 deletions

View file

@ -1,36 +1,38 @@
using System.Dynamic;
using Elsa.Extensions;
using Elsa.JavaScript.TypeDefinitions.Abstractions;
using Elsa.JavaScript.TypeDefinitions.Contracts;
using Elsa.JavaScript.TypeDefinitions.Models;
using Elsa.Workflows.Management.Options;
using JetBrains.Annotations;
using Microsoft.Extensions.Options;
namespace Elsa.JavaScript.Providers;
/// <summary>
/// Produces <see cref="TypeDefinition"/>s for variable types.
/// </summary>
internal class VariableTypeDefinitionProvider(ITypeDescriber typeDescriber) : TypeDefinitionProvider
[UsedImplicitly]
internal class VariableTypeDefinitionProvider(ITypeDescriber typeDescriber, IOptions<ManagementOptions> options) : TypeDefinitionProvider
{
protected override IEnumerable<TypeDefinition> GetTypeDefinitions(TypeDefinitionContext context)
{
var excludedTypes = new Func<Type, bool>[]
{
type => type == typeof(ExpandoObject),
type => typeof(IDictionary<string, object>).IsAssignableFrom(type),
type => type == typeof(object)
type => type.IsPrimitive,
type => type.ContainsGenericParameters,
type => type.IsGenericType && type.GetGenericTypeDefinition() == typeof(IDictionary<,>),
type => type == typeof(object),
type => type == typeof(string)
};
var variables = context.WorkflowGraph.Workflow.Variables;
var variableTypeQuery =
from variable in variables
let variableType = variable.GetVariableType()
where (variableType.IsClass || variableType.IsInterface || variableType.IsEnum) && !variableType.IsPrimitive && !excludedTypes.Any(x => x(variableType))
var variableTypes =
from variableDescriptor in options.Value.VariableDescriptors
let variableType = variableDescriptor.Type
where (variableType.IsClass || variableType.IsInterface || variableType.IsEnum) && !excludedTypes.Any(x => x(variableType))
select variableType;
var variableTypes = variableTypeQuery.Distinct();
foreach (var variableType in variableTypes)
foreach (var variableType in variableTypes.Distinct())
{
yield return typeDescriber.DescribeType(variableType);
}

View file

@ -58,7 +58,7 @@ public class TypeDefinitionDocumentRenderer : ITypeDefinitionDocumentRenderer
}
private void Render(PropertyDefinition property, StringBuilder output) => output.AppendLine($"{property.Name}{(property.IsOptional ? "?" : "")}: {property.Type};");
private void RenderEnumMember(PropertyDefinition property, StringBuilder output) => output.AppendLine($"{property.Name} = \"{property.Name}\";");
private void RenderEnumMember(PropertyDefinition property, StringBuilder output) => output.AppendLine($"{property.Name} = \"{property.Name}\",");
private void Render(VariableDefinition variable, StringBuilder output) => output.AppendLine($"declare var {variable.Name}: {variable.Type};");
string RenderParameter(ParameterDefinition parameter) => $"{parameter.Name}{(parameter.IsOptional ? "?" : "")}: {parameter.Type}";
string RenderParameters(IEnumerable<ParameterDefinition> parameters) => string.Join(", ", parameters.Select(RenderParameter));

View file

@ -1,5 +1,6 @@
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Runtime.CompilerServices;
using Elsa.Extensions;
using Elsa.JavaScript.Contracts;
using Elsa.JavaScript.TypeDefinitions.Contracts;
@ -40,7 +41,10 @@ public class TypeDescriber : ITypeDescriber
yield break;
#pragma warning disable IL2070
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static).Where(x => !x.IsSpecialName).ToList();
var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static)
.Where(x => !x.IsSpecialName)
.Where(x => x.GetCustomAttribute<CompilerGeneratedAttribute>() == null)
.ToList();
#pragma warning restore IL2070
foreach (var method in methods)