-
Notifications
You must be signed in to change notification settings - Fork 237
Expand file tree
/
Copy pathPartialBlockAccumulatorContext.cs
More file actions
89 lines (77 loc) · 3.52 KB
/
PartialBlockAccumulatorContext.cs
File metadata and controls
89 lines (77 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
namespace HandlebarsDotNet.Compiler
{
internal class PartialBlockAccumulatorContext : BlockAccumulatorContext
{
private readonly PartialExpression _startingNode;
private readonly List<Expression> _body = new List<Expression>();
public sealed override string BlockName { get; protected set; }
public PartialBlockAccumulatorContext(Expression startingNode)
: base(startingNode)
{
_startingNode = ConvertToPartialExpression(UnwrapStatement(startingNode));
}
public override void HandleElement(Expression item)
{
_body.Add(item);
}
public override Expression GetAccumulatedBlock()
{
return HandlebarsExpression.Partial(
_startingNode.PartialName,
_startingNode.Argument,
_body.Count switch
{
0 => Expression.Empty(),
1 => _body[0],
_ => Expression.Block(_body)
});
}
public override bool IsClosingElement(Expression item)
{
item = UnwrapStatement(item);
return IsClosingNode(item);
}
private bool IsClosingNode(Expression item)
{
return item is PathExpression && ((PathExpression)item).Path == "/" + BlockName;
}
private PartialExpression ConvertToPartialExpression(Expression expression)
{
if (expression is PathExpression pathExpression)
{
BlockName = pathExpression.Path.Replace("#>", "");
return HandlebarsExpression.Partial(Expression.Constant(BlockName));
}
if (!(expression is HelperExpression helperExpression))
throw new HandlebarsCompilerException($"Cannot convert '{expression}' to a partial expression");
BlockName = helperExpression.HelperName.Replace("#>", "");
var argumentsCount = helperExpression.Arguments.Count();
if (string.IsNullOrEmpty(BlockName))
{
switch (argumentsCount)
{
case 0:
throw new HandlebarsCompilerException("Partial expression misses the name", helperExpression.Context);
case 1:
BlockName = helperExpression.Arguments.First().As<PathExpression>().Path;
return HandlebarsExpression.Partial(Expression.Constant(BlockName));
case 2:
BlockName = helperExpression.Arguments.First().As<PathExpression>().Path;
return HandlebarsExpression.Partial(Expression.Constant(BlockName), helperExpression.Arguments.Last());
default:
throw new HandlebarsCompilerException("Cannot convert a multi-argument helper expression to a partial expression", helperExpression.Context);
}
}
return argumentsCount switch
{
0 => HandlebarsExpression.Partial(Expression.Constant(BlockName)),
1 => HandlebarsExpression.Partial(Expression.Constant(BlockName),
helperExpression.Arguments.First()),
_ => throw new HandlebarsCompilerException("Cannot convert a multi-argument helper expression to a partial expression", helperExpression.Context)
};
}
}
}