Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/Handlebars.Test/IssueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ public void Issue661_ExplicitCrLfInTemplateIsPreservedVerbatim()
{
var handlebars = Handlebars.Create();
handlebars.RegisterHelper("Split", (context, arguments) =>
((string) arguments[0]).Split(((string) arguments[1])[0]));
((string) arguments[0]!).Split(((string) arguments[1]!)[0]));

var template = handlebars.Compile("{{#each (Split \"a;b;c\" ';')}}\r\n{{@Key}}:{{@Index}}:{{this}}\r\n{{/each}}");

Expand Down
2 changes: 1 addition & 1 deletion source/Handlebars/Compiler/CompilationContext.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
4 changes: 2 additions & 2 deletions source/Handlebars/Compiler/FunctionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.ExpressionShortcuts;
using HandlebarsDotNet.Polyfills;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
1 change: 0 additions & 1 deletion source/Handlebars/Compiler/HandlebarsCompiler.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Expressions.Shortcuts;
using HandlebarsDotNet.Compiler.Lexer;
using HandlebarsDotNet.IO;
using HandlebarsDotNet.PathStructure;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using HandlebarsDotNet.Pools;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler.Middlewares
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.Decorators;
using HandlebarsDotNet.ExpressionShortcuts;
using HandlebarsDotNet.Helpers;
using HandlebarsDotNet.Helpers.BlockHelpers;
using HandlebarsDotNet.PathStructure;
using HandlebarsDotNet.Polyfills;
using HandlebarsDotNet.Runtime;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Expressions.Shortcuts;
using HandlebarsDotNet.Collections;
using HandlebarsDotNet.EqualityComparers;
using HandlebarsDotNet.ExpressionShortcuts;
using HandlebarsDotNet.Runtime;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.Decorators;
using HandlebarsDotNet.Helpers;
using HandlebarsDotNet.Helpers.BlockHelpers;
using HandlebarsDotNet.PathStructure;
using HandlebarsDotNet.Runtime;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.ExpressionShortcuts;
using HandlebarsDotNet.ObjectDescriptors;
using HandlebarsDotNet.PathStructure;
using HandlebarsDotNet.Polyfills;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down Expand Up @@ -127,7 +127,7 @@ public static void Iterate(

if (!ObjectDescriptor.TryCreate(target, out var descriptor))
{
throw new HandlebarsRuntimeException($"Cannot create ObjectDescriptor for type {descriptor.DescribedType}");
throw new HandlebarsRuntimeException($"Cannot create ObjectDescriptor for type {target.GetType()}");
}

if (descriptor.Iterator == null) throw new HandlebarsRuntimeException($"Type {descriptor.DescribedType} does not support iteration");
Expand Down
58 changes: 19 additions & 39 deletions source/Handlebars/Compiler/Translation/Expression/PartialBinder.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq.Expressions;
using System.Text;
using Expressions.Shortcuts;
using HandlebarsDotNet.IO;
using HandlebarsDotNet.PathStructure;
using HandlebarsDotNet.Polyfills;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand All @@ -33,26 +30,27 @@ protected override Expression VisitPartialExpression(PartialExpression pex)
? FunctionBuilder.Compile(new[] { pex.Fallback }, CompilationContext, out decorators)
: null;

if (decorators.Count > 0)
var bindingContext = CompilationContext.Args.BindingContext;
var writer = CompilationContext.Args.EncodedWriter;

var parentContext = bindingContext;
if (pex.Argument != null || partialBlockTemplate != null)
{
var bindingContext = CompilationContext.Args.BindingContext;
var writer = CompilationContext.Args.EncodedWriter;
var value = pex.Argument != null
? Arg<object?>(FunctionBuilder.Reduce(pex.Argument, CompilationContext, out _))
: bindingContext.Property(o => o.Value);

var parentContext = bindingContext;
if (pex.Argument != null || partialBlockTemplate != null)
{
var value = pex.Argument != null
? Arg<object?>(FunctionBuilder.Reduce(pex.Argument, CompilationContext, out _))
: bindingContext.Property(o => o.Value);
var partialTemplate = Arg(partialBlockTemplate);
bindingContext = bindingContext.Call(o => o.CreateChildContext(value, partialTemplate));
}

var partialTemplate = Arg(partialBlockTemplate);
bindingContext = bindingContext.Call(o => o.CreateChildContext(value, partialTemplate));
}
var partialName = Cast<string>(pex.PartialName);
var configuration = Arg(CompilationContext.Configuration);
var isBlock = Arg(pex.IsBlock);
var indent = Arg(pex.Indent);

var partialName = Cast<string>(pex.PartialName);
var configuration = Arg(CompilationContext.Configuration);
var isBlock = Arg(pex.IsBlock);
var indent = Arg(pex.Indent);
if (decorators.Count > 0)
{
var templateDelegate = FunctionBuilder.Compile(
new []
{
Expand All @@ -70,24 +68,6 @@ out _
}
else
{
var bindingContext = CompilationContext.Args.BindingContext;
var writer = CompilationContext.Args.EncodedWriter;

if (pex.Argument != null || partialBlockTemplate != null)
{
var value = pex.Argument != null
? Arg<object?>(FunctionBuilder.Reduce(pex.Argument, CompilationContext, out _))
: bindingContext.Property(o => o.Value);

var partialTemplate = Arg(partialBlockTemplate);
bindingContext = bindingContext.Call(o => o.CreateChildContext(value, partialTemplate));
}

var partialName = Cast<string>(pex.PartialName);
var configuration = Arg(CompilationContext.Configuration);
var isBlock = Arg(pex.IsBlock);
var indent = Arg(pex.Indent);

return Call(() =>
InvokePartialWithFallback(partialName, bindingContext, writer, (ICompiledHandlebarsConfiguration) configuration, isBlock, indent)
);
Expand Down Expand Up @@ -142,7 +122,7 @@ private static void WriteWithIndent(EncodedTextWriter writer, string? content, s
}

var pos = 0;
while (pos < content.Length)
while (pos < content!.Length)
{
var newlinePos = content.IndexOf('\n', pos);
if (newlinePos < 0)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.Helpers;
using HandlebarsDotNet.PathStructure;
using HandlebarsDotNet.Runtime;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq.Expressions;
using Expressions.Shortcuts;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Linq.Expressions;
using Expressions.Shortcuts;
using HandlebarsDotNet.Helpers;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq.Expressions;
using Expressions.Shortcuts;
using static Expressions.Shortcuts.ExpressionShortcuts;
using static HandlebarsDotNet.ExpressionShortcuts.ExpressionShortcuts;

namespace HandlebarsDotNet.Compiler
{
Expand Down
74 changes: 74 additions & 0 deletions source/Handlebars/ExpressionShortcuts/BlockBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace HandlebarsDotNet.ExpressionShortcuts
{
/// <summary>
/// Shortcut for <see cref="BlockExpression"/>
/// </summary>
internal class BlockBuilder: ExpressionContainer
{
private readonly Type? _returnType;
private readonly List<Expression> _expressions;
private readonly HashSet<ParameterExpression> _parameters;

internal BlockBuilder(Type? returnType) : base(Expression.Empty())
{
_returnType = returnType;
_expressions = new List<Expression>();
_parameters = new HashSet<ParameterExpression>();
}

/// <inheritdoc />
public override Expression Expression =>
_returnType == null
? Expression.Block(_parameters, _expressions)
: Expression.Block(_returnType, _parameters, _expressions);

/// <summary>
/// Adds parameter to <see cref="BlockExpression"/>
/// </summary>
public BlockBuilder Parameter<T>(out ExpressionContainer<T> parameter)
{
var expression = Expression.Parameter(typeof(T));
parameter = ExpressionShortcuts.Arg<T>(expression);
return Parameter(expression);
}

/// <summary>
/// Adds parameter to <see cref="BlockExpression"/>
/// </summary>
public BlockBuilder Parameter(ParameterExpression e)
{
_parameters.Add(e);
return this;
}

/// <summary>
/// Adds new "line" to <see cref="BlockExpression"/>
/// </summary>
public BlockBuilder Line(Expression e)
{
_expressions.Add(e);
return this;
}

/// <summary>
/// Adds multiple new "lines" to <see cref="BlockExpression"/>
/// </summary>
public BlockBuilder Lines(IEnumerable<Expression> e)
{
_expressions.AddRange(e);
return this;
}

/// <summary>
/// Creates <see cref="InvocationExpression"/> out of current <see cref="BlockExpression"/>.
/// </summary>
public Expression<T> Lambda<T>(IEnumerable<ParameterExpression> parameters) where T : class
{
return Expression.Lambda<T>(Expression, parameters);
}
}
}
49 changes: 49 additions & 0 deletions source/Handlebars/ExpressionShortcuts/ExpressionContainer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System.Linq.Expressions;

namespace HandlebarsDotNet.ExpressionShortcuts
{
/// <summary>
/// Wrapper around of <see cref="System.Linq.Expressions.Expression"/> to provide addition functionality
/// </summary>
internal class ExpressionContainer
{
/// <summary>
/// This can be used to wrap a <see cref="Void"/>-Type expression or implement a derived type
/// </summary>
/// <param name="expression"></param>
public ExpressionContainer(Expression expression) => Expression = expression;

/// <summary>
/// Return the underling <see cref="System.Linq.Expressions.Expression"/>
/// </summary>
public virtual Expression Expression { get; }

/// <summary>
/// Convenience for unwrapping ExpressionContainer
/// </summary>
/// <param name="expressionContainer"></param>
/// <returns></returns>
public static implicit operator Expression(ExpressionContainer expressionContainer) => expressionContainer.Expression;
}

/// <summary>
/// Provides strongly typed container for <see cref="Expression"/>.
/// </summary>
/// <remarks>Used to trick C# compiler in cases like <see cref="ExpressionShortcuts.Call"/> in order to pass value to target method.</remarks>
/// <typeparam name="T">Type of expected <see cref="Expression"/> result value.</typeparam>
internal class ExpressionContainer<T> : ExpressionContainer
{
/// <summary>
/// Used to trick C# compiler - calls need to be removed through <see cref="UnpackExpressionContainerVisitor"/>
/// </summary>
public static implicit operator T(ExpressionContainer<T> _0) => default(T)!;

/// <summary>
/// Wrap an expression of Type <typeparamref name="T"/>
/// </summary>
/// <param name="expression"></param>
public ExpressionContainer(Expression expression) : base(expression)
{
}
}
}
Loading
Loading