public ConditionalBlockAccumulatorContext(Expression startingNode) : base(startingNode) { startingNode = UnwrapStatement(startingNode); var helperExpression = (HelperExpression)startingNode; var testType = helperExpression.HelperName[0] == '#' ? TestType.Direct : TestType.Reverse; BlockName = helperExpression.HelperName.Substring(1, helperExpression.HelperName.Length - 1); if (!ValidHelperNames.Contains(BlockName)) { throw new HandlebarsCompilerException($"Tried to convert {BlockName} expression to conditional block", helperExpression.Context); } var argument = HandlebarsExpression.Boolish(helperExpression.Arguments.Single()); _currentCondition = BlockName switch { "if" when testType == TestType.Direct => argument, "if" when testType == TestType.Reverse => Expression.Not(argument), "unless" when testType == TestType.Direct => Expression.Not(argument), "unless" when testType == TestType.Reverse => argument, _ => throw new HandlebarsCompilerException($"Tried to convert {BlockName} expression to conditional block", helperExpression.Context) }; }
protected virtual Expression VisitBoolishExpression(BoolishExpression bex) { Expression condition = Visit(bex.Condition); if (condition != bex.Condition) { return(HandlebarsExpression.Boolish(condition)); } return(bex); }
public ConditionalBlockAccumulatorContext(Expression startingNode) : base(startingNode) { startingNode = UnwrapStatement(startingNode); _blockName = ((HelperExpression)startingNode).HelperName.Replace("#", ""); if (new [] { "if", "unless" }.Contains(_blockName) == false) { throw new HandlebarsCompilerException(string.Format( "Tried to convert {0} expression to conditional block", _blockName)); } _testType = _blockName == "if"; var argument = HandlebarsExpression.Boolish(((HelperExpression)startingNode).Arguments.Single()); _currentCondition = _testType ? (Expression)argument : Expression.Not(argument); }
public ConditionalBlockAccumulatorContext(Expression startingNode) : base(startingNode) { startingNode = UnwrapStatement(startingNode); var helperExpression = (HelperExpression)startingNode; BlockName = helperExpression.HelperName.Replace("#", ""); if (!ValidHelperNames.Contains(BlockName)) { throw new HandlebarsCompilerException($"Tried to convert {BlockName} expression to conditional block", helperExpression.Context); } var testType = BlockName == "if"; var argument = HandlebarsExpression.Boolish(helperExpression.Arguments.Single()); _currentCondition = testType ? (Expression)argument : Expression.Not(argument); }
private static Expression CreateIfBlock(HelperExpression startingNode, IEnumerable <Expression> body) { var condition = HandlebarsExpression.Boolish(startingNode.Arguments.Single()); body = UnwrapBlockExpression(body); body = body.Concat(new Expression[] { Expression.Empty() }); if (startingNode.HelperName == "#if") { return(Expression.IfThen(condition, Expression.Block(body))); } else if (startingNode.HelperName == "#unless") { return(Expression.IfThen(Expression.Not(condition), Expression.Block(body))); } else { throw new HandlebarsCompilerException(string.Format( "Tried to create a conditional expression for '{0}'", startingNode.HelperName)); } }
private Expression GetElseIfTestExpression(Expression item) { item = UnwrapStatement(item); return(HandlebarsExpression.Boolish(((HelperExpression)item).Arguments.Skip(1).Single())); }
protected override Expression VisitBoolishExpression(BoolishExpression bex) { return(HandlebarsExpression.Boolish(Visit(bex.Condition))); }