Пример #1
0
        public override void VisitHelperExpression(HelperExpressionNode helperExpressionNode)
        {
            if (helperExpressionNode.Method == "Partial")
            {
                var    partialViewNameArg = helperExpressionNode.Arguments[0].Expression;
                var    modelArg           = helperExpressionNode.Arguments[1]?.Expression;
                var    viewDataArg        = helperExpressionNode.Arguments[2]?.Expression;
                var    viewData           = new Dictionary <string, object>();
                string partialViewName    = null;
                var    baseObject         = this.BaseObject;
                var    modelObjectGraph   = new List <IBase>()
                {
                    baseObject
                };

                SwitchExtensions.Switch(partialViewNameArg, () => partialViewNameArg.GetType().Name,

                                        SwitchExtensions.Case <LiteralExpressionSyntax>("LiteralExpressionSyntax", (literalExpression) =>
                {
                    partialViewName = literalExpression.Token.Text.RemoveQuotes();

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.CaseElse(() =>
                {
                    var a = partialViewNameArg;
                    var t = partialViewNameArg.GetType().Name;

                    // implementation here or throw error

                    DebugUtils.Break();
                })
                                        );

                if (modelArg != null)
                {
                    SwitchExtensions.Switch(modelArg, () => modelArg.GetType().Name,

                                            SwitchExtensions.Case <MemberAccessExpressionSyntax>("MemberAccessExpressionSyntax", (memberAccessExpression) =>
                    {
                        var identifiers = memberAccessExpression.GetMemberIdentifiers();
                        var joined      = string.Empty;

                        foreach (var identifier in identifiers)
                        {
                            if (identifier is IdentifierNameSyntax)
                            {
                                var identifierName = (IdentifierNameSyntax)identifier;
                                var identifierText = identifierName.Identifier.Text;
                                joined             = joined.Append(identifierText.PrependIf(".", !joined.IsNullOrEmpty()));

                                if (joined.StartsWith("Model.Entity."))
                                {
                                    var parentBase    = (IParentBase)baseObject;
                                    IBase childObject = null;

                                    if (parentBase is Entity_Set)
                                    {
                                        baseObject = parentBase.ChildElements.Single();
                                        parentBase = (IParentBase)baseObject;

                                        modelObjectGraph.Add(parentBase);
                                    }

                                    childObject = parentBase.ChildNodes.Single(b => b.Name == identifierText);
                                    modelObjectGraph.Add(childObject);
                                }
                            }
                            else
                            {
                                DebugUtils.Break();
                            }
                        }

                        DebugUtils.NoOp();
                    }),
                                            SwitchExtensions.CaseElse(() =>
                    {
                        var a = modelArg;
                        var t = modelArg.GetType().Name;

                        // implementation here or throw error

                        DebugUtils.Break();
                    })
                                            );
                }

                if (viewDataArg != null)
                {
                    SwitchExtensions.Switch(viewDataArg, () => viewDataArg.GetType().Name,

                                            SwitchExtensions.Case <ObjectCreationExpressionSyntax>("ObjectCreationExpressionSyntax", (objectCreationExpression) =>
                    {
                        var typeName    = objectCreationExpression.Type.GetName();
                        var argList     = objectCreationExpression.ArgumentList.Arguments;
                        var initializer = objectCreationExpression.Initializer;

                        if (typeName == "ViewDataDictionary")
                        {
                            var includeOurViewData = false;

                            if (argList.Count > 0)
                            {
                                var sourceArg           = argList.Single();
                                var sourceArgExpression = sourceArg.Expression;

                                SwitchExtensions.Switch(sourceArgExpression, () => sourceArgExpression.GetType().Name,

                                                        SwitchExtensions.Case <MemberAccessExpressionSyntax>("MemberAccessExpressionSyntax", (memberAccessExpression) =>
                                {
                                    if (memberAccessExpression.HasThisExpression())
                                    {
                                        var identifiers = memberAccessExpression.GetMemberIdentifiers(true);

                                        if (identifiers.Count() == 1)
                                        {
                                            var identifier = identifiers.Single();

                                            if (identifier is IdentifierNameSyntax)
                                            {
                                                var identifierName = (IdentifierNameSyntax)identifier;
                                                var identifierText = identifierName.Identifier.Text;

                                                if (identifierText == "ViewData")
                                                {
                                                    includeOurViewData = true;
                                                }
                                                else
                                                {
                                                    DebugUtils.Break();
                                                }
                                            }
                                            else
                                            {
                                                DebugUtils.Break();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        DebugUtils.Break();
                                    }

                                    DebugUtils.NoOp();
                                }),
                                                        SwitchExtensions.CaseElse(() =>
                                {
                                    var a = sourceArgExpression;
                                    var t = sourceArgExpression.GetType().Name;

                                    // implementation here or throw error

                                    DebugUtils.Break();
                                })
                                                        );

                                if (initializer != null)
                                {
                                    var kind = initializer.Kind();

                                    if (kind == SyntaxKind.CollectionInitializerExpression)
                                    {
                                        foreach (var expression in initializer.Expressions.Cast <InitializerExpressionSyntax>())
                                        {
                                            var expressions = expression.Expressions.ToList();
                                            var key         = ((LiteralExpressionSyntax)expressions[0]).Token.Text.RemoveQuotes();
                                            var value       = ((LiteralExpressionSyntax)expressions[1]).Token.Value;

                                            viewData.Add(key, value);
                                        }
                                    }
                                    else
                                    {
                                        DebugUtils.Break();
                                    }
                                }

                                if (includeOurViewData)
                                {
                                    viewData = viewData.Concat(this.ViewData).ToDictionary(k => k.Key, k => k.Value);
                                }
                            }
                        }
                        else
                        {
                            DebugUtils.Break();
                        }

                        DebugUtils.NoOp();
                    }),
                                            SwitchExtensions.CaseElse(() =>
                    {
                        var a = viewDataArg;
                        var t = viewDataArg.GetType().Name;

                        // implementation here or throw error

                        DebugUtils.Break();
                    })
                                            );
                }

                var args = new ChildViewHandlerEventArgs(partialViewName, modelObjectGraph, viewData);

                OnChildView(this, args);

                /// kn - you are here, raise OnChildView, handled by GeneratorConfiguration.HandleViews, remove from sortedList
                /// 1. pass args, i.e. Model, ViewDataDictionary
                /// 2. retrieve PartialView component name back
            }
            else
            {
                DebugUtils.Break();
            }

            base.VisitHelperExpression(helperExpressionNode);
        }
Пример #2
0
        private void VisitChildren(BaseNode node)
        {
            foreach (var child in node.ChildNodes)
            {
                VisitNode(child);

                SwitchExtensions.Switch(child, () => child.GetType().Name,

                                        SwitchExtensions.Case <DirectiveNode>("DirectiveNode", (directiveNode) =>
                {
                    VisitDirective(directiveNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <MarkupNode> ("MarkupNode", (markupNode) =>
                {
                    VisitMarkup(markupNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <ModelNode>("ModelNode", (modelNode) =>
                {
                    VisitModel(modelNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <RenderNode>("RenderNode", (renderNode) =>
                {
                    VisitRender(renderNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <CodeNode>("CodeNode", (codeNode) =>
                {
                    VisitCode(codeNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <ModelInvocationNode>("ModelInvocationNode", (modelInvocationNode) =>
                {
                    VisitModelInvocation(modelInvocationNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <ViewDataNode>("ViewDataNode", (viewDataNode) =>
                {
                    VisitViewData(viewDataNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <ViewBagNode>("ViewBagNode", (viewBagNode) =>
                {
                    VisitViewBag(viewBagNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <PropertyNode>("PropertyNode", (propertyNode) =>
                {
                    VisitProperty(propertyNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <VariableNode>("VariableNode", (variableNode) =>
                {
                    VisitVariable(variableNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <SectionNode>("SectionNode", (sectionNode) =>
                {
                    VisitSection(sectionNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <MarkupBlockNode>("MarkupBlockNode", (markupBlockNode) =>
                {
                    VisitMarkupBlock(markupBlockNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <CommentNode>("CommentNode", (commentNode) =>
                {
                    VisitComment(commentNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <CodeExpressionNode>("CodeExpressionNode", (codeExpressionNode) =>
                {
                    VisitCodeExpression(codeExpressionNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <HelperExpressionNode>("HelperExpressionNode", (helperExpressionNode) =>
                {
                    VisitHelperExpression(helperExpressionNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <CustomScriptsSectionNode>("CustomScriptsSectionNode", (customScriptsSectionNode) =>
                {
                    VisitCustomScriptsSection(customScriptsSectionNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.Case <ScriptExpressionNode>("ScriptExpressionNode", (scriptExpressionNode) =>
                {
                    VisitScriptExpression(scriptExpressionNode);

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.CaseElse(() =>
                {
                    var a = child;
                    var t = child.GetType().Name;

                    // implementation here or throw error

                    DebugUtils.Break();
                })
                                        );
            }
        }
Пример #3
0
        private void ParseStatements(SyntaxTree syntaxTree, CompilationUnitSyntax syntaxRoot, SemanticModel model)
        {
            var renderMethod = syntaxRoot.GetRenderMethod();
            var statements   = renderMethod.Body.Statements.ToList();

            foreach (var statement in statements)
            {
                SwitchExtensions.Switch(statement, () => statement.GetType().Name,

                                        SwitchExtensions.Case <ExpressionStatementSyntax>("ExpressionStatementSyntax", (expressionStatement) =>
                {
                    var expression = expressionStatement.Expression;

                    SwitchExtensions.Switch(expression, () => expression.GetType().Name,

                                            SwitchExtensions.Case <AssignmentExpressionSyntax>("AssignmentExpressionSyntax", (assignmentExpression) =>
                    {
                        var left  = assignmentExpression.Left;
                        var right = assignmentExpression.Right;
                        AssignmentNode assignmentNode = null;

                        SwitchExtensions.Switch(left, () => left.GetType().Name,

                                                SwitchExtensions.Case <ElementAccessExpressionSyntax>("ElementAccessExpressionSyntax", (elementAccessExpression) =>
                        {
                            var name    = ((IdentifierNameSyntax)elementAccessExpression.Expression).Identifier.Text;
                            var arg     = elementAccessExpression.ArgumentList.Arguments.Single();
                            var literal = (LiteralExpressionSyntax)arg.Expression;
                            var key     = (string)literal.Token.Value;

                            if (name == "ViewData")
                            {
                                var viewDataNode = new ViewDataNode(null, key, assignmentExpression.ToFullString());

                                viewDataNode.Left = key;

                                assignmentNode = viewDataNode;
                            }
                            else
                            {
                                DebugUtils.Break();
                            }

                            DebugUtils.NoOp();
                        }),
                                                SwitchExtensions.Case <MemberAccessExpressionSyntax>("MemberAccessExpressionSyntax", (memberAccessExpressionSyntax) =>
                        {
                            var name     = ((IdentifierNameSyntax)memberAccessExpressionSyntax.Expression).Identifier.Text;
                            var property = memberAccessExpressionSyntax.Name.Identifier.Text;

                            if (name == "ViewBag")
                            {
                                var viewBagNode = new ViewBagNode(null, property, assignmentExpression.ToFullString());

                                viewBagNode.Left = name;

                                assignmentNode = viewBagNode;
                            }
                            else
                            {
                                DebugUtils.Break();
                            }

                            DebugUtils.NoOp();
                        }),
                                                SwitchExtensions.Case <IdentifierNameSyntax>("IdentifierNameSyntax", (identifierName) =>
                        {
                            var name         = identifierName.Identifier.Text;
                            var propertyNode = new PropertyNode(null, assignmentExpression.ToFullString());

                            propertyNode.Left = name;

                            assignmentNode = propertyNode;

                            DebugUtils.NoOp();
                        }),
                                                SwitchExtensions.CaseElse(() =>
                        {
                            var a = left;
                            var t = left.GetType().Name;

                            // implementation here or throw error

                            DebugUtils.Break();
                        })
                                                );

                        SwitchExtensions.Switch(right, () => right.GetType().Name,

                                                SwitchExtensions.Case <LiteralExpressionSyntax>("LiteralExpressionSyntax", (literalExpression) =>
                        {
                            assignmentNode.Right = literalExpression.Token.Value;
                        }),
                                                SwitchExtensions.CaseElse(() =>
                        {
                            var a = right;
                            var t = right.GetType().Name;

                            // implementation here or throw error

                            DebugUtils.Break();
                        })
                                                );

                        assignmentNode.Right = right;

                        this.AddChild(assignmentNode);
                    }),
                                            SwitchExtensions.CaseElse(() =>
                    {
                        var a = expression;
                        var t = expression.GetType().Name;

                        // implementation here or throw error

                        DebugUtils.Break();
                    })
                                            );
                }),
                                        SwitchExtensions.Case <LocalDeclarationStatementSyntax>("LocalDeclarationStatementSyntax", (localDeclarationStatement) =>
                {
                    var variable     = localDeclarationStatement.Declaration.Variables.Single();
                    var name         = variable.Identifier.Text;
                    var initializer  = variable.Initializer;
                    var variableNode = new VariableNode(name, null, localDeclarationStatement.ToFullString());
                    ModelInvocationNode modelInvocationNode = null;

                    SwitchExtensions.Switch(initializer, () => initializer.GetType().Name,

                                            SwitchExtensions.Case <EqualsValueClauseSyntax>("EqualsValueClauseSyntax", (equalsValueClause) =>
                    {
                        var value = equalsValueClause.Value;

                        if (value is InvocationExpressionSyntax)
                        {
                            var invocationExpression = (InvocationExpressionSyntax)value;

                            if (invocationExpression.Expression is MemberAccessExpressionSyntax)
                            {
                                var memberAccessExpression = (MemberAccessExpressionSyntax)invocationExpression.Expression;

                                if (memberAccessExpression.Expression is IdentifierNameSyntax && ((IdentifierNameSyntax)memberAccessExpression.Expression).Identifier.Text == "Model")
                                {
                                    if (memberAccessExpression.Name is GenericNameSyntax)
                                    {
                                        var identifierName = (GenericNameSyntax)memberAccessExpression.Name;
                                        var methodName     = identifierName.Identifier.Text;
                                        var typeArguments  = identifierName.TypeArgumentList.Arguments;

                                        modelInvocationNode = new ModelInvocationNode(null, invocationExpression.ToFullString(), variableNode, methodName, typeArguments);
                                    }
                                    else
                                    {
                                        DebugUtils.Break();
                                    }
                                }
                                else
                                {
                                    DebugUtils.Break();
                                }
                            }
                            else
                            {
                                DebugUtils.Break();
                            }
                        }
                        else
                        {
                            DebugUtils.Break();
                        }

                        variableNode.Right = value;
                    }),
                                            SwitchExtensions.CaseElse(() =>
                    {
                        var a = initializer;
                        var t = initializer.GetType().Name;

                        // implementation here or throw error

                        DebugUtils.Break();
                    })
                                            );

                    this.AddChild(variableNode);

                    if (modelInvocationNode != null)
                    {
                        variableNode.AddChild(modelInvocationNode);
                    }

                    DebugUtils.NoOp();
                }),
                                        SwitchExtensions.CaseElse(() =>
                {
                    var a = statement;
                    var t = statement.GetType().Name;

                    // implementation here or throw error

                    DebugUtils.Break();
                })
                                        );
            }
        }