Esempio n. 1
0
        /// <summary>
        /// Instantiates a new <see cref="ParserResults"/> instance.
        /// </summary>
        /// <param name="success"><c>true</c> if parsing was successful, <c>false</c> otherwise.</param>
        /// <param name="document">The <see cref="Block"/> for the syntax tree.</param>
        /// <param name="tagHelperDescriptors">
        /// The <see cref="TagHelperDescriptor"/>s that apply to the current Razor document.
        /// </param>
        /// <param name="errorSink">
        /// The <see cref="ErrorSink"/> used to collect <see cref="RazorError"/>s encountered when parsing the
        /// current Razor document.
        /// </param>
        protected ParserResults(bool success,
                                Block document,
                                IEnumerable<TagHelperDescriptor> tagHelperDescriptors,
                                ErrorSink errorSink)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

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

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

            Success = success;
            Document = document;
            TagHelperDescriptors = tagHelperDescriptors;
            ErrorSink = errorSink;
            ParserErrors = errorSink.Errors;
            Prefix = tagHelperDescriptors.FirstOrDefault()?.Prefix;
        }
Esempio n. 2
0
        /// <inheritdoc />
        protected override IEnumerable<TagHelperDescriptor> GetTagHelperDescriptors(
            Block documentRoot,
            ErrorSink errorSink)
        {
            if (documentRoot == null)
            {
                throw new ArgumentNullException(nameof(documentRoot));
            }

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

            var visitor = new ViewImportsTagHelperDirectiveSpanVisitor(
                TagHelperDescriptorResolver,
                _viewImportsDirectiveDescriptors,
                errorSink);

            var descriptors = visitor.GetDescriptors(documentRoot);
            foreach (var descriptor in descriptors)
            {
                foreach (var attributeDescriptor in descriptor.Attributes)
                {
                    if (attributeDescriptor.IsIndexer &&
                        string.Equals(
                            attributeDescriptor.TypeName,
                            _modelExpressionTypeName,
                            StringComparison.Ordinal))
                    {
                        errorSink.OnError(
                            SourceLocation.Undefined,
                            Resources.FormatMvcRazorParser_InvalidPropertyType(
                                descriptor.TypeName,
                                attributeDescriptor.Name,
                                _modelExpressionTypeName),
                            length: 0);
                    }
                }
            }

            return descriptors;
        }
Esempio n. 3
0
        /// <summary>
        /// Instantiates a new <see cref="ParserResults"/> instance.
        /// </summary>
        /// <param name="document">The <see cref="Block"/> for the syntax tree.</param>
        /// <param name="tagHelperDescriptors">
        /// The <see cref="TagHelperDescriptor"/>s that apply to the current Razor document.
        /// </param>
        /// <param name="errorSink">
        /// The <see cref="ErrorSink"/> used to collect <see cref="RazorError"/>s encountered when parsing the
        /// current Razor document.
        /// </param>
        public ParserResults(Block document,
                             IEnumerable<TagHelperDescriptor> tagHelperDescriptors,
                             ErrorSink errorSink)
            : this(!errorSink.Errors.Any(), document, tagHelperDescriptors, errorSink)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

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

            if (errorSink == null)
            {
                throw new ArgumentNullException(nameof(errorSink));
            }
        }
        public void RenderAttributeValue_RendersModelExpressionsCorrectly(
            string modelExpressionType,
            string propertyType,
            string expectedValue)
        {
            // Arrange
            var renderer = new MvcTagHelperAttributeValueCodeRenderer(
                new GeneratedTagHelperAttributeContext
                {
                    ModelExpressionTypeName = modelExpressionType,
                    CreateModelExpressionMethodName = "SomeMethod"
                });
            var attributeDescriptor = new TagHelperAttributeDescriptor
            {
                Name = "MyAttribute",
                PropertyName = "SomeProperty",
                TypeName = propertyType,
            };
            var writer = new CSharpCodeWriter();
            var generatorContext = new ChunkGeneratorContext(
                host: null,
                className: string.Empty,
                rootNamespace: string.Empty,
                sourceFile: string.Empty,
                shouldGenerateLinePragmas: true);
            var errorSink = new ErrorSink();
            var context = new CodeGeneratorContext(generatorContext, errorSink);

            // Act
            renderer.RenderAttributeValue(attributeDescriptor, writer, context,
            (codeWriter) =>
            {
                codeWriter.Write("MyValue");
            },
            complexValue: false);

            // Assert
            Assert.Equal(expectedValue, writer.GenerateCode());
        }
        public void Compile_ReturnsFailedResultIfParseFails()
        {
            // Arrange
            var errorSink = new ErrorSink();
            errorSink.OnError(new RazorError("some message", 1, 1, 1, 1));
            var generatorResult = new GeneratorResults(
                    new Block(new BlockBuilder { Type = BlockType.Comment }),
                    Enumerable.Empty<TagHelperDescriptor>(),
                    errorSink,
                    new CodeGeneratorResult("", new LineMapping[0]),
                    new ChunkTree());
            var host = new Mock<IMvcRazorHost>();
            host.Setup(h => h.GenerateCode(It.IsAny<string>(), It.IsAny<Stream>()))
                .Returns(generatorResult)
                .Verifiable();

            var fileInfo = new Mock<IFileInfo>();
            fileInfo.Setup(f => f.CreateReadStream())
                    .Returns(Stream.Null);

            var compiler = new Mock<ICompilationService>(MockBehavior.Strict);
            var relativeFileInfo = new RelativeFileInfo(fileInfo.Object, @"Views\index\home.cshtml");
            var razorService = new RazorCompilationService(compiler.Object, host.Object, GetOptions());

            // Act
            var result = razorService.Compile(relativeFileInfo);

            // Assert
            Assert.NotNull(result.CompilationFailures);
            Assert.Collection(result.CompilationFailures,
                failure =>
                {
                    var message = Assert.Single(failure.Messages);
                    Assert.Equal("some message", message.Message);
                });
            host.Verify();
        }
Esempio n. 6
0
 /// <summary>
 /// Instantiates a new <see cref="ParserResults"/> instance.
 /// </summary>
 /// <param name="document">The <see cref="Block"/> for the syntax tree.</param>
 /// <param name="tagHelperDescriptors">
 /// The <see cref="TagHelperDescriptor"/>s that apply to the current Razor document.
 /// </param>
 /// <param name="errorSink">
 /// The <see cref="ErrorSink"/> used to collect <see cref="RazorError"/>s encountered when parsing the
 /// current Razor document.
 /// </param>
 public ParserResults([NotNull] Block document,
                      [NotNull] IEnumerable <TagHelperDescriptor> tagHelperDescriptors,
                      [NotNull] ErrorSink errorSink)
     : this(!errorSink.Errors.Any(), document, tagHelperDescriptors, errorSink)
 {
 }
Esempio n. 7
0
 public IEnumerable<TagHelperDescriptor> GetTagHelperDescriptorsPublic(
     Block documentRoot,
     ErrorSink errorSink)
 {
     return GetTagHelperDescriptors(documentRoot, errorSink);
 }
Esempio n. 8
0
            protected override TagHelperDescriptorResolutionContext GetTagHelperDescriptorResolutionContext(
                IEnumerable<TagHelperDirectiveDescriptor> descriptors,
                ErrorSink errorSink)
            {
                var directivesToImport = MergeDirectiveDescriptors(descriptors, _viewImportsDirectiveDescriptors);

                return base.GetTagHelperDescriptorResolutionContext(directivesToImport, errorSink);
            }
Esempio n. 9
0
 public ViewImportsTagHelperDirectiveSpanVisitor(
     ITagHelperDescriptorResolver descriptorResolver,
     IEnumerable<TagHelperDirectiveDescriptor> viewImportsDirectiveDescriptors,
     ErrorSink errorSink)
     : base(descriptorResolver, errorSink)
 {
     _viewImportsDirectiveDescriptors = viewImportsDirectiveDescriptors;
 }