Exemplo n.º 1
0
        private static INamedTypeSymbol GetPageModel(RazorPageItem razorPage, Compilation compilation, Dictionary <string, List <string> > usingsByDirectory)
        {
            if (razorPage.Model == null)
            {
                return(null);
            }

            // First try to get the page model based on the usings in this file
            var explicitUsings = PagesFacts.ExtractUsings(razorPage);
            var pageModel      = FindPageModel(compilation, razorPage.Model, explicitUsings);

            if (pageModel != null)
            {
                return(pageModel);
            }

            // Walk up the path and try to get the page model based on usings in the implicitly included files
            foreach (var path in EnumerateUpPath(razorPage.AdditionalText.Path))
            {
                if (usingsByDirectory.TryGetValue(path, out var usings))
                {
                    pageModel = FindPageModel(compilation, razorPage.Model, usings);
                    if (pageModel != null)
                    {
                        return(pageModel);
                    }
                }
            }

            // Last try to get the page model based on what is in @model without a using
            return(FindPageModel(compilation, razorPage.Model, new List <string> {
                ""
            }));
        public void ExtractUsings_ReturnsEmpty_WhenNoUsings()
        {
            var file     = new InMemoryAdditionalText("", "");
            var pageData = new RazorPageItem(file);

            // Act
            var result = PagesFacts.ExtractUsings(pageData);

            // Assert
            result.Should().BeEmpty();
        }
        public void ExtractUsings_ReturnsUsings()
        {
            var file     = new InMemoryAdditionalText("", @"@using System
@using System.Text");
            var pageData = new RazorPageItem(file);

            // Act
            var result = PagesFacts.ExtractUsings(pageData);

            // Assert
            result.Should().BeEquivalentTo(new[]
            {
                "System",
                "System.Text",
            });
        }