Skip to content

Commit

Permalink
Merge pull request #21 from hydrostack/20-fix-compiler-warning-cs4014
Browse files Browse the repository at this point in the history
Change Expression<Action> to LambdaExpression for handlers
  • Loading branch information
kjeske authored Apr 28, 2024
2 parents afcaab2 + a4d78b1 commit 7e0ccfb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ internal static class ExpressionExtensions
private const string JsIndicationStart = "HYDRO_JS(";
private const string JsIndicationEnd = ")HYDRO_JS";

public static (string Name, IDictionary<string, object> Parameters)? GetNameAndParameters(this Expression<Action> expression)
public static (string Name, IDictionary<string, object> Parameters)? GetNameAndParameters(this LambdaExpression expression)
{
if (expression is not { Body: MethodCallExpression methodCall })
{
Expand Down Expand Up @@ -40,7 +40,7 @@ internal static object EvaluateExpressionValue(Expression expression)
{
case ConstantExpression constantExpression:
return constantExpression.Value;

case MemberExpression memberExpression:
var objectMember = Expression.Convert(memberExpression, typeof(object));
var getterLambda = Expression.Lambda<Func<object>>(objectMember);
Expand Down
19 changes: 12 additions & 7 deletions src/TagHelpers/HydroOnTagHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ public sealed class HydroOnTagHelper : TagHelper
{
private const string HandlersPrefix = "hydro-on:";

private IDictionary<string, Expression<Action>> _handlers;
private IDictionary<string, Expression> _handlers;

/// <summary />
[HtmlAttributeName(DictionaryAttributePrefix = HandlersPrefix)]
public IDictionary<string, Expression<Action>> Handlers
public IDictionary<string, Expression> Handlers
{
get => _handlers ??= new Dictionary<string, Expression<Action>>(StringComparer.OrdinalIgnoreCase);
get => _handlers ??= new Dictionary<string, Expression>(StringComparer.OrdinalIgnoreCase);
set => _handlers = value;
}

Expand Down Expand Up @@ -52,7 +52,12 @@ public override void Process(TagHelperContext context, TagHelperOutput output)

foreach (var eventItem in _handlers)
{
var jsExpression = GetJsExpression(eventItem.Value);
if (eventItem.Value is not LambdaExpression actionExpression)
{
throw new InvalidOperationException($"Wrong event handler statement in component for {modelType.Namespace}");
}

var jsExpression = GetJsExpression(actionExpression);

if (jsExpression == null)
{
Expand All @@ -70,7 +75,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output)
}
}

private static string GetJsExpression(Expression<Action> expression)
private static string GetJsExpression(LambdaExpression expression)
{
var clientAction = GetJsClientActionExpression(expression);

Expand All @@ -82,7 +87,7 @@ private static string GetJsExpression(Expression<Action> expression)
return GetJsInvokeExpression(expression);
}

private static string GetJsClientActionExpression(Expression<Action> expression)
private static string GetJsClientActionExpression(LambdaExpression expression)
{
if (expression is not { Body: MethodCallExpression methodCall }
|| methodCall.Method.DeclaringType != typeof(HydroClientActions))
Expand All @@ -101,7 +106,7 @@ private static string GetJsClientActionExpression(Expression<Action> expression)
}
}

private static string GetJsInvokeExpression(Expression<Action> expression)
private static string GetJsInvokeExpression(LambdaExpression expression)
{
var eventData = expression.GetNameAndParameters();

Expand Down

0 comments on commit 7e0ccfb

Please sign in to comment.