public void WriteInjectProperty(CodeRenderingContext context, InjectIntermediateNode node)
            {
                if (context == null)
                {
                    throw new ArgumentNullException(nameof(context));
                }

                if (node == null)
                {
                    throw new ArgumentNullException(nameof(node));
                }

                var property = $"public {node.TypeName} {node.MemberName} {{ get; private set; }}";

                if (node.Source.HasValue)
                {
                    using (context.CodeWriter.BuildLinePragma(node.Source.Value))
                    {
                        context.CodeWriter
                        .WriteLine(RazorInjectAttribute)
                        .WriteLine(property);
                    }
                }
                else
                {
                    context.CodeWriter
                    .WriteLine(RazorInjectAttribute)
                    .WriteLine(property);
                }
            }
    public void InjectDirectiveTargetExtension_WritesPropertyWithLinePragma_WhenSourceIsSet()
    {
        // Arrange
        var context = TestCodeRenderingContext.CreateRuntime();
        var target  = new InjectTargetExtension();
        var node    = new InjectIntermediateNode()
        {
            TypeName   = "PropertyType<ModelType>",
            MemberName = "PropertyName",
            Source     = new SourceSpan(
                filePath: "test-path",
                absoluteIndex: 0,
                lineIndex: 1,
                characterIndex: 1,
                length: 10)
        };

        // Act
        target.WriteInjectProperty(context, node);

        // Assert
        Assert.Equal(Environment.NewLine +
                     "#nullable restore" + Environment.NewLine +
                     "#line 2 \"test-path\"" + Environment.NewLine +
                     "[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]" + Environment.NewLine +
                     "public PropertyType<ModelType> PropertyName { get; private set; }" + Environment.NewLine + Environment.NewLine +
                     "#line default" + Environment.NewLine +
                     "#line hidden" + Environment.NewLine +
                     "#nullable disable" + Environment.NewLine,
                     context.CodeWriter.GenerateCode());
    }
Esempio n. 3
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            if (documentNode.DocumentKind != RazorPageDocumentClassifierPass.RazorPageDocumentKind &&
                documentNode.DocumentKind != MvcViewDocumentClassifierPass.MvcViewDocumentKind)
            {
                // Not a MVC file. Skip.
                return;
            }

            var visitor = new Visitor();

            visitor.Visit(documentNode);
            var modelType = ModelDirective.GetModelType(documentNode);

            var properties = new HashSet <string>(StringComparer.Ordinal);

            for (var i = visitor.Directives.Count - 1; i >= 0; i--)
            {
                var directive = visitor.Directives[i];
                var tokens    = directive.Tokens.ToArray();
                if (tokens.Length < 2)
                {
                    continue;
                }

                var typeName   = tokens[0].Content;
                var memberName = tokens[1].Content;

                if (!properties.Add(memberName))
                {
                    continue;
                }

                typeName = typeName.Replace("<TModel>", "<" + modelType + ">");

                var injectNode = new InjectIntermediateNode()
                {
                    TypeName   = typeName,
                    MemberName = memberName,
                };

                visitor.Class.Children.Add(injectNode);
            }
        }
Esempio n. 4
0
            public void WriteInjectProperty(CodeRenderingContext context, InjectIntermediateNode node)
            {
                TkDebug.AssertArgumentNull(context, nameof(context), this);
                TkDebug.AssertArgumentNull(node, nameof(node), this);

                var property = $"public {node.TypeName} {node.MemberName} {{ get; private set; }}";

                if (node.Source.HasValue)
                {
                    using (context.CodeWriter.BuildLinePragma(node.Source.Value))
                    {
                        context.CodeWriter.WriteLine(RazorInjectAttribute).WriteLine(property);
                    }
                }
                else
                {
                    context.CodeWriter.WriteLine(RazorInjectAttribute).WriteLine(property);
                }
            }
    public void InjectDirectiveTargetExtension_WritesProperty()
    {
        // Arrange
        var context = TestCodeRenderingContext.CreateRuntime();
        var target  = new InjectTargetExtension();
        var node    = new InjectIntermediateNode()
        {
            TypeName   = "PropertyType",
            MemberName = "PropertyName",
        };

        // Act
        target.WriteInjectProperty(context, node);

        // Assert
        Assert.Equal(
            "[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]" + Environment.NewLine +
            "public PropertyType PropertyName { get; private set; }" + Environment.NewLine,
            context.CodeWriter.GenerateCode());
    }
Esempio n. 6
0
            protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
            {
                var visitor = new Visitor();

                visitor.Visit(documentNode);
                var modelType = Instrumentation.ModelDirective.GetModelType(documentNode);

                var properties = new HashSet <string>(StringComparer.Ordinal);

                for (var i = visitor.Directives.Count - 1; i >= 0; i--)
                {
                    var directive = visitor.Directives[i];
                    var tokens    = directive.Tokens.ToArray();
                    if (tokens.Length < 2)
                    {
                        continue;
                    }

                    var typeName   = tokens[0].Content;
                    var memberName = tokens[1].Content;

                    if (!properties.Add(memberName))
                    {
                        continue;
                    }

                    typeName = typeName.Replace("<TModel>", "<" + modelType + ">");

                    var injectNode = new InjectIntermediateNode
                    {
                        TypeName   = typeName,
                        MemberName = memberName,
                    };

                    visitor.Class.Children.Add(injectNode);
                }
            }