protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            var configuration = Engine.GetFeature <DefaultDocumentClassifierPassFeature>();

            if (configuration != null)
            {
                for (var i = 0; i < configuration.ConfigureClass.Count; i++)
                {
                    var configureClass = configuration.ConfigureClass[i];
                    configureClass(codeDocument, @class);
                }

                for (var i = 0; i < configuration.ConfigureNamespace.Count; i++)
                {
                    var configureNamespace = configuration.ConfigureNamespace[i];
                    configureNamespace(codeDocument, @namespace);
                }

                for (var i = 0; i < configuration.ConfigureMethod.Count; i++)
                {
                    var configureMethod = configuration.ConfigureMethod[i];
                    configureMethod(codeDocument, @method);
                }
            }
        }
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

            @namespace.Content = "AspNetCore";

            var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath;

            if (string.IsNullOrEmpty(filePath))
            {
                // It's possible for a Razor document to not have a file path.
                // Eg. When we try to generate code for an in memory document like default imports.
                var checksum = BytesToString(codeDocument.Source.GetChecksum());
                @class.ClassName = $"AspNetCore_{checksum}";
            }
            else
            {
                @class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);
            }

            @class.BaseType = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>";
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            method.MethodName = "ExecuteAsync";
            method.Modifiers.Clear();
            method.Modifiers.Add("public");
            method.Modifiers.Add("async");
            method.Modifiers.Add("override");
            method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}";
        }
Exemplo n.º 3
0
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

            @namespace.Content = "AspNetCore";

            @class.BaseType = "global::Microsoft.AspNetCore.Mvc.RazorPages.Page";

            var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath;

            @class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);

            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            method.MethodName = "ExecuteAsync";
            method.Modifiers.Clear();
            method.Modifiers.Add("public");
            method.Modifiers.Add("async");
            method.Modifiers.Add("override");
            method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}";

            var document = codeDocument.GetDocumentIntermediateNode();

            PageDirective.TryGetPageDirective(document, out var pageDirective);

            EnsureValidPageDirective(codeDocument, pageDirective);

            AddRouteTemplateMetadataAttribute(@namespace, @class, pageDirective);
        }
Exemplo n.º 4
0
 protected virtual void OnDocumentStructureCreated(
     RazorCodeDocument codeDocument,
     NamespaceDeclarationIntermediateNode @namespace,
     ClassDeclarationIntermediateNode @class,
     MethodDeclarationIntermediateNode @method)
 {
     // Intentionally empty.
 }
 protected override void OnDocumentStructureCreated(
     RazorCodeDocument codeDocument,
     NamespaceDeclarationIntermediateNode @namespace,
     ClassDeclarationIntermediateNode @class,
     MethodDeclarationIntermediateNode method)
 {
     @namespace.Content = Namespace;
     @class.ClassName   = Class;
     @method.MethodName = Method;
 }
        /// <inheritdoc />
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            if (!TryComputeNamespaceAndClass(
                    codeDocument.Source.FilePath,
                    codeDocument.Source.RelativePath,
                    out var computedNamespace,
                    out var computedClass))
            {
                // If we can't compute a nice namespace (no relative path) then just generate something
                // mangled.
                computedNamespace = BaseNamespace;
                computedClass     = CSharpIdentifier.GetClassNameFromPath(codeDocument.Source.FilePath) ?? "__BlazorComponent";
            }

            if (MangleClassNames)
            {
                computedClass = "__" + computedClass;
            }

            @namespace.Content = computedNamespace;

            @class.BaseType  = BlazorApi.BlazorComponent.FullTypeName;
            @class.ClassName = computedClass;
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            method.ReturnType = "void";
            method.MethodName = BlazorApi.BlazorComponent.BuildRenderTree;
            method.Modifiers.Clear();
            method.Modifiers.Add("protected");
            method.Modifiers.Add("override");

            method.Parameters.Clear();
            method.Parameters.Add(new MethodParameter()
            {
                ParameterName = "builder",
                TypeName      = BlazorApi.RenderTreeBuilder.FullTypeName,
            });

            // We need to call the 'base' method as the first statement.
            var callBase = new CSharpCodeIntermediateNode();

            callBase.Annotations.Add(BuildRenderTreeBaseCallAnnotation, true);
            callBase.Children.Add(new IntermediateToken
            {
                Kind    = TokenKind.CSharp,
                Content = $"base.{BlazorApi.BlazorComponent.BuildRenderTree}(builder);"
            });
            method.Children.Insert(0, callBase);
        }
Exemplo n.º 7
0
    protected override void OnDocumentStructureCreated(
        RazorCodeDocument codeDocument,
        NamespaceDeclarationIntermediateNode @namespace,
        ClassDeclarationIntermediateNode @class,
        MethodDeclarationIntermediateNode method)
    {
        base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

        if (!codeDocument.TryComputeNamespace(fallbackToRootNamespace: false, out var namespaceName))
        {
            @namespace.Content = _useConsolidatedMvcViews ? "AspNetCoreGeneratedDocument" : "AspNetCore";
        }
Exemplo n.º 8
0
 public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node)
 {
     Context.CodeWriter.WriteLine("#pragma warning disable 1998");
     for (int index = 0; index < node.Modifiers.Count; ++index)
     {
         Context.CodeWriter.Write(node.Modifiers[index]);
         Context.CodeWriter.Write(" ");
     }
     Context.CodeWriter.Write(node.ReturnType).Write(" ").Write(node.MethodName).WriteLine("()");
     using (Context.CodeWriter.BuildScope())
         VisitDefault(node);
     Context.CodeWriter.WriteLine("#pragma warning restore 1998");
 }
Exemplo n.º 9
0
            public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node)
            {
                Context.CodeWriter.WriteLine("#pragma warning disable 1998");

                for (var i = 0; i < node.Modifiers.Count; i++)
                {
                    Context.CodeWriter.Write(node.Modifiers[i]);
                    Context.CodeWriter.Write(" ");
                }

                Context.CodeWriter.Write(node.ReturnType);
                Context.CodeWriter.Write(" ");

                Context.CodeWriter.Write(node.MethodName);
                Context.CodeWriter.Write("(");

                for (var i = 0; i < node.Parameters.Count; i++)
                {
                    var parameter = node.Parameters[i];

                    for (var j = 0; j < parameter.Modifiers.Count; j++)
                    {
                        Context.CodeWriter.Write(parameter.Modifiers[j]);
                        Context.CodeWriter.Write(" ");
                    }

                    Context.CodeWriter.Write(parameter.TypeName);
                    Context.CodeWriter.Write(" ");

                    Context.CodeWriter.Write(parameter.ParameterName);

                    if (i < node.Parameters.Count - 1)
                    {
                        Context.CodeWriter.Write(", ");
                    }
                }

                Context.CodeWriter.Write(")");
                Context.CodeWriter.WriteLine();

                using (Context.CodeWriter.BuildScope())
                {
                    VisitDefault(node);
                }

                Context.CodeWriter.WriteLine("#pragma warning restore 1998");
            }
Exemplo n.º 10
0
    private void Rewrite(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
    {
        // Rewrite the document from a flat structure to use a sensible default structure,
        // a namespace and class declaration with a single 'razor' method.
        var children = new List <IntermediateNode>(documentNode.Children);

        documentNode.Children.Clear();

        var @namespace = new NamespaceDeclarationIntermediateNode();

        @namespace.Annotations[CommonAnnotations.PrimaryNamespace] = CommonAnnotations.PrimaryNamespace;

        var @class = new ClassDeclarationIntermediateNode();

        @class.Annotations[CommonAnnotations.PrimaryClass] = CommonAnnotations.PrimaryClass;

        var method = new MethodDeclarationIntermediateNode();

        method.Annotations[CommonAnnotations.PrimaryMethod] = CommonAnnotations.PrimaryMethod;

        var documentBuilder = IntermediateNodeBuilder.Create(documentNode);

        var namespaceBuilder = IntermediateNodeBuilder.Create(documentBuilder.Current);

        namespaceBuilder.Push(@namespace);

        var classBuilder = IntermediateNodeBuilder.Create(namespaceBuilder.Current);

        classBuilder.Push(@class);

        var methodBuilder = IntermediateNodeBuilder.Create(classBuilder.Current);

        methodBuilder.Push(method);

        var visitor = new Visitor(documentBuilder, namespaceBuilder, classBuilder, methodBuilder);

        for (var i = 0; i < children.Count; i++)
        {
            visitor.Visit(children[i]);
        }

        // Note that this is called at the *end* of rewriting so that user code can see the tree
        // and look at its content to make a decision.
        OnDocumentStructureCreated(codeDocument, @namespace, @class, method);
    }
    public void FindPrimaryMethod_FindsMethodWithAnnotation()
    {
        // Arrange
        var document = new DocumentIntermediateNode();
        var method   = new MethodDeclarationIntermediateNode();

        method.Annotations[CommonAnnotations.PrimaryMethod] = CommonAnnotations.PrimaryMethod;

        var builder = IntermediateNodeBuilder.Create(document);

        builder.Add(method);

        // Act
        var result = document.FindPrimaryMethod();

        // Assert
        Assert.Same(method, result);
    }
Exemplo n.º 12
0
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            var documentNode = codeDocument.GetDocumentIntermediateNode();

            foreach (var @using in @namespace.Children.OfType <UsingDirectiveIntermediateNode>())
            {
                documentNode.Children.Add(@using);
            }
            foreach (var child in method.Children)
            {
                documentNode.Children.Add(child);
            }

            documentNode.Children.Remove(@namespace);

            return;
        }
Exemplo n.º 13
0
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

            @namespace.Content = "Razor.Orm.Template";

            @class.ClassName = $"{codeDocument.Source.FilePath}_GeneratedTemplate";
            @class.BaseType  = "global::Razor.Orm.Template.SqlTemplate<TModel>";
            @class.Modifiers.Clear();
            @class.Modifiers.Add("internal");

            method.MethodName = "Execute";
            method.Modifiers.Clear();
            method.Modifiers.Add("protected");
            method.Modifiers.Add("override");
            method.ReturnType = "void";
        }
        protected override void OnDocumentStructureCreated(RazorCodeDocument codeDocument,
                                                           NamespaceDeclarationIntermediateNode @namespace, ClassDeclarationIntermediateNode @class,
                                                           MethodDeclarationIntermediateNode method)
        {
            string templateKey = codeDocument.Source.FilePath ?? codeDocument.Source.FilePath;

            base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

            @namespace.Content = "YJC.Toolkit.Razor.CompiledTemplates";

            @class.ClassName = "GeneratedTemplate"; //CSharpIdentifier.GetClassNameFromPath(templateKey);
            @class.BaseType  = fBaseType;
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            method.MethodName = "ExecuteAsync";
            method.Modifiers.Clear();
            method.Modifiers.Add("public");
            method.Modifiers.Add("async");
            method.Modifiers.Add("override");
            method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}";
        }
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            var relative = codeDocument.Items["relative-path"] as string;
            var filePath = relative ?? codeDocument.Source.FilePath;

            base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);
            @namespace.Content = runtimeTemplatesNamespace;
            @class.ClassName   = CSharpIdentifier.GetClassNameFromPath(filePath);
            @class.BaseType    = baseTypeName;
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            method.MethodName = "ExecuteAsync";
            method.Modifiers.Clear();
            method.Modifiers.Add("public");
            method.Modifiers.Add("async");
            method.Modifiers.Add("override");
            method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}";
        }
Exemplo n.º 16
0
            protected override void OnDocumentStructureCreated(
                RazorCodeDocument codeDocument,
                NamespaceDeclarationIntermediateNode @namespace,
                ClassDeclarationIntermediateNode @class,
                MethodDeclarationIntermediateNode method)
            {
                string templateKey = codeDocument.Source.FilePath ?? codeDocument.Source.FilePath;

                base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

                @namespace.Content = "Scriban.Benchmarks";

                @class.ClassName = "GeneratedTemplate"; //CSharpIdentifier.GetClassNameFromPath(templateKey);
                @class.BaseType  = "global::Scriban.Benchmarks.RazorTemplatePage";
                @class.Modifiers.Clear();
                @class.Modifiers.Add("public");

                method.MethodName = "Execute";
                method.Modifiers.Clear();
                method.Modifiers.Add("public");
                method.Modifiers.Add("override");
                method.ReturnType = $"void";
            }
Exemplo n.º 17
0
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

            @namespace.Content = "AspNetCore";

            var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath;

            @class.ClassName = CSharpIdentifier.GetClassNameFromPath(filePath);
            @class.BaseType  = "global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<TModel>";
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            method.MethodName = "ExecuteAsync";
            method.Modifiers.Clear();
            method.Modifiers.Add("public");
            method.Modifiers.Add("async");
            method.Modifiers.Add("override");
            method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}";
        }
Exemplo n.º 18
0
            public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode methodNode)
            {
                var topLevelExpressions = methodNode.Children.OfType <CSharpExpressionIntermediateNode>();

                foreach (var csharpExpression in topLevelExpressions)
                {
                    if (csharpExpression.Children.Count == 1)
                    {
                        var child = csharpExpression.Children[0];
                        if (child is IntermediateToken intermediateToken)
                        {
                            if (TryGetLayoutType(intermediateToken.Content, out string layoutType))
                            {
                                DidFindLayoutDeclaration = true;
                                MethodNode = methodNode;
                                LayoutNode = csharpExpression;
                                LayoutType = layoutType;
                            }
                        }
                    }
                }

                base.VisitMethodDeclaration(methodNode);
            }
            public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode methodNode)
            {
                MethodNode = methodNode;

                var topLevelExpressions = methodNode.Children.OfType <CSharpExpressionIntermediateNode>();

                foreach (var csharpExpression in topLevelExpressions)
                {
                    if (csharpExpression.Children.Count == 1)
                    {
                        var child = csharpExpression.Children[0];
                        if (child is IntermediateToken intermediateToken)
                        {
                            if (TryGetImplementsType(intermediateToken.Content, out string implementsType))
                            {
                                ImplementsNodes.Add(csharpExpression);
                                ImplementsTypes.Add(implementsType);
                            }
                        }
                    }
                }

                base.VisitMethodDeclaration(methodNode);
            }
 public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node)
 {
     Method = node;
 }
Exemplo n.º 21
0
        protected override void OnDocumentStructureCreated(RazorCodeDocument codeDocument, NamespaceDeclarationIntermediateNode @namespace, ClassDeclarationIntermediateNode @class, MethodDeclarationIntermediateNode method)
        {
            var compilationData = codeDocument.Items["ViewCompilationData"] as RazorViewCompilationData;

            @namespace.Content = compilationData.Namespace;
            @class.ClassName   = compilationData.ClassName;

            @class.BaseType = "global::MustardBlack.ViewEngines.Razor.RazorViewPage";
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            method.MethodName = "ExecuteAsync";
            method.Modifiers.Clear();
            method.Modifiers.Add("public");
            method.Modifiers.Add("async");
            method.Modifiers.Add("override");
            method.ReturnType = $"global::{typeof(System.Threading.Tasks.Task).FullName}";
        }
Exemplo n.º 22
0
 public virtual void VisitMethodDeclaration(MethodDeclarationIntermediateNode node)
 {
     VisitDefault(node);
 }
        /// <inheritdoc />
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            if (!codeDocument.TryComputeNamespace(fallbackToRootNamespace: true, out var computedNamespace) ||
                !TryComputeClassName(codeDocument, out var computedClass))
            {
                // If we can't compute a nice namespace (no relative path) then just generate something
                // mangled.
                computedNamespace = FallbackRootNamespace;
                var checksum = Checksum.BytesToString(codeDocument.Source.GetChecksum());
                computedClass = $"AspNetCore_{checksum}";
            }

            var documentNode = codeDocument.GetDocumentIntermediateNode();

            if (char.IsLower(computedClass, 0))
            {
                // We don't allow component names to start with a lowercase character.
                documentNode.Diagnostics.Add(
                    ComponentDiagnosticFactory.Create_ComponentNamesCannotStartWithLowerCase(computedClass, documentNode.Source));
            }

            if (MangleClassNames)
            {
                computedClass = ComponentMetadata.MangleClassName(computedClass);
            }

            @namespace.Content = computedNamespace;
            @class.ClassName   = computedClass;
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");
            @class.Modifiers.Add("partial");

            if (FileKinds.IsComponentImport(codeDocument.GetFileKind()))
            {
                // We don't want component imports to be considered as real component.
                // But we still want to generate code for it so we can get diagnostics.
                @class.BaseType = typeof(object).FullName;

                method.ReturnType = "void";
                method.MethodName = "Execute";
                method.Modifiers.Clear();
                method.Modifiers.Add("protected");

                method.Parameters.Clear();
            }
            else
            {
                @class.BaseType = ComponentsApi.ComponentBase.FullTypeName;

                var typeParamReferences = documentNode.FindDirectiveReferences(ComponentTypeParamDirective.Directive);
                for (var i = 0; i < typeParamReferences.Count; i++)
                {
                    var typeParamNode = (DirectiveIntermediateNode)typeParamReferences[i].Node;
                    if (typeParamNode.HasDiagnostics)
                    {
                        continue;
                    }

                    @class.TypeParameters.Add(new TypeParameter()
                    {
                        ParameterName = typeParamNode.Tokens.First().Content,
                        Constraints   = typeParamNode.Tokens.Skip(1).FirstOrDefault()?.Content
                    });
                }

                method.ReturnType = "void";
                method.MethodName = ComponentsApi.ComponentBase.BuildRenderTree;
                method.Modifiers.Clear();
                method.Modifiers.Add("protected");
                method.Modifiers.Add("override");

                method.Parameters.Clear();
                method.Parameters.Add(new MethodParameter()
                {
                    ParameterName = ComponentsApi.RenderTreeBuilder.BuilderParameter,
                    TypeName      = ComponentsApi.RenderTreeBuilder.FullTypeName,
                });
            }
        }
Exemplo n.º 24
0
        /// <inheritdoc />
        protected override void OnDocumentStructureCreated(
            RazorCodeDocument codeDocument,
            NamespaceDeclarationIntermediateNode @namespace,
            ClassDeclarationIntermediateNode @class,
            MethodDeclarationIntermediateNode method)
        {
            if (!TryComputeNamespaceAndClass(
                    codeDocument.Source.FilePath,
                    codeDocument.Source.RelativePath,
                    out var computedNamespace,
                    out var computedClass))
            {
                // If we can't compute a nice namespace (no relative path) then just generate something
                // mangled.
                computedNamespace = BaseNamespace;
                computedClass     = CSharpIdentifier.GetClassNameFromPath(codeDocument.Source.FilePath) ?? "__BlazorComponent";
            }

            if (MangleClassNames)
            {
                computedClass = "__" + computedClass;
            }

            @namespace.Content = computedNamespace;

            @class.BaseType  = ComponentsApi.ComponentBase.FullTypeName;
            @class.ClassName = computedClass;
            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");

            var documentNode        = codeDocument.GetDocumentIntermediateNode();
            var typeParamReferences = documentNode.FindDirectiveReferences(TypeParamDirective.Directive);

            for (var i = 0; i < typeParamReferences.Count; i++)
            {
                var typeParamNode = (DirectiveIntermediateNode)typeParamReferences[i].Node;
                if (typeParamNode.HasDiagnostics)
                {
                    continue;
                }

                @class.TypeParameters.Add(new TypeParameter()
                {
                    ParameterName = typeParamNode.Tokens.First().Content,
                });
            }

            method.ReturnType = "void";
            method.MethodName = ComponentsApi.ComponentBase.BuildRenderTree;
            method.Modifiers.Clear();
            method.Modifiers.Add("protected");
            method.Modifiers.Add("override");

            method.Parameters.Clear();
            method.Parameters.Add(new MethodParameter()
            {
                ParameterName = "builder",
                TypeName      = ComponentsApi.RenderTreeBuilder.FullTypeName,
            });

            // We need to call the 'base' method as the first statement.
            var callBase = new CSharpCodeIntermediateNode();

            callBase.Annotations.Add(BuildRenderTreeBaseCallAnnotation, true);
            callBase.Children.Add(new IntermediateToken
            {
                Kind    = TokenKind.CSharp,
                Content = $"base.{ComponentsApi.ComponentBase.BuildRenderTree}(builder);"
            });
            method.Children.Insert(0, callBase);
        }
Exemplo n.º 25
0
 public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node)
 {
     RemoveContiguousWhitespace(node.Children, TraversalDirection.Forwards);
     RemoveContiguousWhitespace(node.Children, TraversalDirection.Backwards);
     VisitDefault(node);
 }
Exemplo n.º 26
0
 public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node)
 {
     WriteContentNode(node, string.Join(" ", node.Modifiers), node.ReturnType, node.MethodName);
 }
        protected override void OnDocumentStructureCreated(RazorCodeDocument codeDocument, NamespaceDeclarationIntermediateNode @namespace, ClassDeclarationIntermediateNode @class, MethodDeclarationIntermediateNode method)
        {
            base.OnDocumentStructureCreated(codeDocument, @namespace, @class, method);

            if (!TryComputeNamespaceAndClass(
                    codeDocument.Source.FilePath,
                    codeDocument.Source.RelativePath,
                    out var computedNamespace,
                    out var computedClass))
            {
                // If we can't compute a nice namespace (no relative path) then just generate something
                // mangled.
                computedNamespace = "AspNetCore";
                var checksum = Checksum.BytesToString(codeDocument.Source.GetChecksum());
                computedClass = $"AspNetCore_{checksum}";
            }

            @namespace.Content = computedNamespace;

            @class.ClassName = computedClass;
            @class.BaseType  = $"global::{CodeGenerationConstants.RazorComponent.FullTypeName}";
            var filePath = codeDocument.Source.RelativePath ?? codeDocument.Source.FilePath;

            if (string.IsNullOrEmpty(filePath))
            {
                // It's possible for a Razor document to not have a file path.
                // Eg. When we try to generate code for an in memory document like default imports.
                var checksum = Checksum.BytesToString(codeDocument.Source.GetChecksum());
                @class.ClassName = $"AspNetCore_{checksum}";
            }
            else
            {
                @class.ClassName = CSharpIdentifier.SanitizeIdentifier(Path.GetFileNameWithoutExtension(filePath));
            }

            @class.Modifiers.Clear();
            @class.Modifiers.Add("public");
            @class.Modifiers.Add("sealed");

            method.MethodName = CodeGenerationConstants.RazorComponent.BuildRenderTree;
            method.ReturnType = "void";
            method.Modifiers.Clear();
            method.Modifiers.Add("public");
            method.Modifiers.Add("override");

            method.Parameters.Clear();
            method.Parameters.Add(new MethodParameter()
            {
                TypeName      = CodeGenerationConstants.RenderTreeBuilder.FullTypeName,
                ParameterName = CodeGenerationConstants.RazorComponent.BuildRenderTreeParameter,
            });

            // We need to call the 'base' method as the first statement.
            var callBase = new CSharpCodeIntermediateNode();

            callBase.Annotations.Add(BuildRenderTreeBaseCallAnnotation, true);
            callBase.Children.Add(new IntermediateToken
            {
                Kind    = TokenKind.CSharp,
                Content = $"base.{CodeGenerationConstants.RazorComponent.BuildRenderTree}({CodeGenerationConstants.RazorComponent.BuildRenderTreeParameter});"
            });
            method.Children.Insert(0, callBase);
        }