< Summary

Line coverage
95%
Covered lines: 44
Uncovered lines: 2
Coverable lines: 46
Total lines: 139
Line coverage: 95.6%
Branch coverage
82%
Covered branches: 23
Total branches: 28
Branch coverage: 82.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/Handlers/ConfigureEngineWithVariables.cs

#LineLine coverage
 1using System.Dynamic;
 2using System.Text.RegularExpressions;
 3using Elsa.Expressions.Models;
 4using Elsa.Extensions;
 5using Elsa.Expressions.JavaScript.Extensions;
 6using Elsa.Expressions.JavaScript.Helpers;
 7using Elsa.Expressions.JavaScript.Notifications;
 8using Elsa.Expressions.JavaScript.Options;
 9using Elsa.Mediator.Contracts;
 10using Elsa.Workflows.Activities;
 11using JetBrains.Annotations;
 12using Jint.Native;
 13using Microsoft.Extensions.Options;
 14
 15namespace Elsa.Expressions.JavaScript.Handlers;
 16
 17/// <summary>
 18/// A handler that configures the Jint engine with workflow variables.
 19/// </summary>
 20[UsedImplicitly]
 52021public partial class ConfigureEngineWithVariables(IOptions<JintOptions> options) : INotificationHandler<EvaluatingJavaSc
 22{
 37323    private bool IsEnabled => options.Value is { DisableWrappers: false, DisableVariableCopying: false };
 24
 25    /// <inheritdoc />
 26    public Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken)
 27    {
 18828        if (!IsEnabled)
 029            return Task.CompletedTask;
 30
 18831        CopyVariablesIntoEngine(notification);
 18832        return Task.CompletedTask;
 33    }
 34
 35    public Task HandleAsync(EvaluatedJavaScript notification, CancellationToken cancellationToken)
 36    {
 18537        if (!IsEnabled)
 038            return Task.CompletedTask;
 39
 18540        CopyVariablesIntoWorkflowExecutionContext(notification);
 18541        return Task.CompletedTask;
 42    }
 43
 44    private void CopyVariablesIntoWorkflowExecutionContext(EvaluatedJavaScript notification)
 45    {
 18546        var context = notification.Context;
 18547        var engine = notification.Engine;
 18548        var variablesContainer = (IDictionary<string, object?>)engine.GetValue("variables").ToObject()!;
 18549        var inputNames = GetInputNames(context).FilterInvalidVariableNames().Distinct().ToList();
 50
 39651        foreach (var (variableName, variableValue) in variablesContainer)
 52        {
 1353            if (inputNames.Contains(variableName))
 54                continue;
 55
 1356            var processedValue = variableValue is JsObject jsValue ? jsValue.ToObject() : variableValue ?? context.GetVa
 1357            context.SetVariable(variableName, processedValue);
 58        }
 18559    }
 60
 61    private void CopyVariablesIntoEngine(EvaluatingJavaScript notification)
 62    {
 18863        var engine = notification.Engine;
 18864        var context = notification.Context;
 18865        var expression = notification.Expression;
 18866        var variableNames = GetUsedVariableNames(context, expression).ToList();
 18867        var variablesContainer = (IDictionary<string, object?>)new ExpandoObject();
 68
 39269        foreach (var variableName in variableNames)
 70        {
 871            var variableValue = context.GetVariableInScope(variableName);
 872            variableValue = ObjectConverterHelper.ProcessVariableValue(engine, variableValue);
 873            variablesContainer[variableName] = variableValue;
 74        }
 75
 18876        engine.SetValue("variables", variablesContainer);
 18877    }
 78
 79    private IEnumerable<string> GetUsedVariableNames(ExpressionExecutionContext context, string expression)
 80    {
 18881        var variableNames = context.GetVariableNamesInScope().FilterInvalidVariableNames();
 82
 18883        var variableNamesInScript = ExtractVariableNamesRegex().Matches(expression)
 984            .Select(m => m.Groups[1].Value)
 18885            .ToList();
 86
 32187        return variableNames.Where(x => variableNamesInScript.Contains(x));
 88    }
 89
 90    private IEnumerable<string> GetInputNames(ExpressionExecutionContext context)
 91    {
 18592        var activityExecutionContext = context.TryGetActivityExecutionContext(out var aec) ? aec : null;
 93
 53894        while (activityExecutionContext != null)
 95        {
 35396            if (activityExecutionContext.Activity is Workflow workflow)
 97            {
 12498                var inputDefinitions = workflow.Inputs;
 99
 282100                foreach (var inputDefinition in inputDefinitions)
 17101                    yield return inputDefinition.Name;
 102            }
 103
 742104            foreach (var syntheticProperty in activityExecutionContext.Activity.SyntheticProperties)
 105            {
 18106                yield return syntheticProperty.Key;
 107            }
 108
 353109            activityExecutionContext = activityExecutionContext.ParentActivityExecutionContext;
 110        }
 185111    }
 112
 113    [GeneratedRegex(@"variables\.(\w+)(?:\.\w+)*")]
 114    private static partial Regex ExtractVariableNamesRegex();
 115}

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/obj/Release/net10.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs

File '/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Expressions.JavaScript/obj/Release/net10.0/System.Text.RegularExpressions.Generator/System.Text.RegularExpressions.Generator.RegexGenerator/RegexGenerator.g.cs' does not exist (any more).