| | | 1 | | using System.Text; |
| | | 2 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Contracts; |
| | | 3 | | using Elsa.Expressions.JavaScript.TypeDefinitions.Models; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Expressions.JavaScript.TypeDefinitions.Services; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | | 8 | | public class TypeDefinitionDocumentRenderer : ITypeDefinitionDocumentRenderer |
| | | 9 | | { |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | public string Render(TypeDefinitionsDocument document) |
| | | 12 | | { |
| | 0 | 13 | | var stringBuilder = new StringBuilder(); |
| | | 14 | | |
| | 0 | 15 | | foreach (var functionDefinition in document.Functions) |
| | 0 | 16 | | Render(functionDefinition, stringBuilder); |
| | | 17 | | |
| | 0 | 18 | | foreach (var typeDefinition in document.Types) |
| | 0 | 19 | | Render(typeDefinition, stringBuilder); |
| | | 20 | | |
| | 0 | 21 | | foreach (var variableDefinition in document.Variables) |
| | 0 | 22 | | Render(variableDefinition, stringBuilder); |
| | | 23 | | |
| | 0 | 24 | | return stringBuilder.ToString(); |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | private void Render(FunctionDefinition functionDefinition, StringBuilder output) |
| | | 28 | | { |
| | 0 | 29 | | var returnType = functionDefinition.ReturnType != null ? $": {functionDefinition.ReturnType}" : ""; |
| | 0 | 30 | | output.AppendLine($"declare function {functionDefinition.Name}({RenderParameters(functionDefinition.Parameters)} |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | private void RenderMethod(FunctionDefinition functionDefinition, StringBuilder output) |
| | | 34 | | { |
| | 0 | 35 | | var returnType = functionDefinition.ReturnType != null ? $" => {functionDefinition.ReturnType}" : ""; |
| | 0 | 36 | | output.AppendLine($"{functionDefinition.Name}: ({RenderParameters(functionDefinition.Parameters)}){returnType};" |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | private void Render(TypeDefinition typeDefinition, StringBuilder output) |
| | | 40 | | { |
| | 0 | 41 | | output.AppendLine($"declare {typeDefinition.DeclarationKeyword} {typeDefinition.Name} {{"); |
| | | 42 | | |
| | 0 | 43 | | if (typeDefinition.DeclarationKeyword == "enum") |
| | | 44 | | { |
| | 0 | 45 | | foreach (var property in typeDefinition.Properties) |
| | 0 | 46 | | RenderEnumMember(property, output); |
| | | 47 | | } |
| | | 48 | | else |
| | | 49 | | { |
| | 0 | 50 | | foreach (var property in typeDefinition.Properties) |
| | 0 | 51 | | Render(property, output); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | foreach (var method in typeDefinition.Methods) |
| | 0 | 55 | | RenderMethod(method, output); |
| | | 56 | | |
| | 0 | 57 | | output.AppendLine("}"); |
| | 0 | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | private void Render(PropertyDefinition property, StringBuilder output) => output.AppendLine($"{property.Name}{(prope |
| | 0 | 61 | | private void RenderEnumMember(PropertyDefinition property, StringBuilder output) => output.AppendLine($"{property.Na |
| | 0 | 62 | | private void Render(VariableDefinition variable, StringBuilder output) => output.AppendLine($"declare var {variable. |
| | 0 | 63 | | string RenderParameter(ParameterDefinition parameter) => $"{parameter.Name}{(parameter.IsOptional ? "?" : "")}: {par |
| | 0 | 64 | | string RenderParameters(IEnumerable<ParameterDefinition> parameters) => string.Join(", ", parameters.Select(RenderPa |
| | | 65 | | } |