-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using System; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
using System.Runtime.CompilerServices; | ||
using DotVVM.Framework.Utils; | ||
|
||
namespace DotVVM.Framework.Compilation.Binding | ||
{ | ||
/// <summary> Checks that assigned properties have setters without the <see cref="IsExternalInit" /> attribute. | ||
Check failure on line 9 in src/Framework/Framework/Compilation/Binding/InitOnlyPropertyCheckingVisitor.cs
|
||
/// <paramref name="staticError"/> specifies if the error should be thrown immediately, or only when actually executed at runtime. </summary> | ||
public class InitOnlyPropertyCheckingVisitor(bool staticError = true): ExpressionVisitor | ||
{ | ||
public static InitOnlyPropertyCheckingVisitor Instance { get; } = new InitOnlyPropertyCheckingVisitor(true); | ||
public static InitOnlyPropertyCheckingVisitor InstanceDynamicError { get; } = new InitOnlyPropertyCheckingVisitor(false); | ||
|
||
protected override Expression VisitBinary(BinaryExpression node) | ||
{ | ||
if (node is { NodeType: ExpressionType.Assign, Left: MemberExpression { Member: PropertyInfo assignedProperty } } && | ||
assignedProperty.IsInitOnly()) | ||
{ | ||
var message = $"Property '{assignedProperty.DeclaringType!.Name}.{assignedProperty.Name}' is init-only and cannot be assigned to in bindings executed server-side. You can only assign to such properties in staticCommand bindings executed on the client."; | ||
if (staticError) | ||
{ | ||
throw new Exception(message); | ||
} | ||
else | ||
{ | ||
return Expression.Throw(Expression.New(typeof(Exception).GetConstructor([typeof(string)]), [Expression.Constant(message)])); | ||
Check failure on line 28 in src/Framework/Framework/Compilation/Binding/InitOnlyPropertyCheckingVisitor.cs
|
||
} | ||
} | ||
|
||
return base.VisitBinary(node); | ||
} | ||
} | ||
} |