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);
            }
        }
예제 #2
0
        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());
        }
예제 #3
0
        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());
        }
예제 #4
0
            protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
            {
                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);
                }
            }