diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs index af79139c69..318994ec0e 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs @@ -27,8 +27,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X return; } + var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute); + var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute); + if (vcAttribute == null || vcAttribute.TypeKind == TypeKind.Error) + { + // Could not find attributes we care about in the compilation. Nothing to do. + return; + } + var types = new List(); - var visitor = ViewComponentTypeVisitor.Create(compilation, types); + var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types); // We always visit the global namespace. visitor.Visit(compilation.Assembly.GlobalNamespace); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs index be987cbdd5..db0912087a 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs @@ -16,13 +16,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X private readonly INamedTypeSymbol _nonViewComponentAttribute; private readonly List _results; - public static ViewComponentTypeVisitor Create(Compilation compilation, List results) - { - var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute); - var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute); - return new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, results); - } - public ViewComponentTypeVisitor( INamedTypeSymbol viewComponentAttribute, INamedTypeSymbol nonViewComponentAttribute, @@ -31,12 +24,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X _viewComponentAttribute = viewComponentAttribute; _nonViewComponentAttribute = nonViewComponentAttribute; _results = results; - - Enabled = _viewComponentAttribute != null; } - public bool Enabled { get; set; } - public override void VisitNamedType(INamedTypeSymbol symbol) { if (IsViewComponent(symbol)) @@ -65,11 +54,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X internal bool IsViewComponent(INamedTypeSymbol symbol) { - if (!Enabled) - { - return false; - } - if (symbol.DeclaredAccessibility != Accessibility.Public || symbol.IsAbstract || symbol.IsGenericType || diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs index a5c05dc585..d0d9805f6b 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs @@ -27,8 +27,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions return; } + var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute); + var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute); + if (vcAttribute == null || vcAttribute.TypeKind == TypeKind.Error) + { + // Could not find attributes we care about in the compilation. Nothing to do. + return; + } + var types = new List(); - var visitor = ViewComponentTypeVisitor.Create(compilation, types); + var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types); // We always visit the global namespace. visitor.Visit(compilation.Assembly.GlobalNamespace); diff --git a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs index 2fd215d99d..9e1f936060 100644 --- a/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs +++ b/src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs @@ -13,13 +13,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions private readonly INamedTypeSymbol _nonViewComponentAttribute; private readonly List _results; - public static ViewComponentTypeVisitor Create(Compilation compilation, List results) - { - var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute); - var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute); - return new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, results); - } - public ViewComponentTypeVisitor( INamedTypeSymbol viewComponentAttribute, INamedTypeSymbol nonViewComponentAttribute, diff --git a/src/Microsoft.CodeAnalysis.Razor/CompilationTagHelperFeature.cs b/src/Microsoft.CodeAnalysis.Razor/CompilationTagHelperFeature.cs index 2b343f74e2..2041cd9eb5 100644 --- a/src/Microsoft.CodeAnalysis.Razor/CompilationTagHelperFeature.cs +++ b/src/Microsoft.CodeAnalysis.Razor/CompilationTagHelperFeature.cs @@ -40,15 +40,11 @@ namespace Microsoft.CodeAnalysis.Razor internal static bool IsValidCompilation(Compilation compilation) { - var iTagHelper = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper); var @string = compilation.GetSpecialType(SpecialType.System_String); - // Do some minimal tests to verify the compilation is valid. If symbols for ITagHelper or System.String - // are missing or errored, the compilation may be missing references. - return iTagHelper != null && - iTagHelper.TypeKind != TypeKind.Error && - @string != null && - @string.TypeKind != TypeKind.Error; + // Do some minimal tests to verify the compilation is valid. If symbols for System.String + // is missing or errored, the compilation may be missing references. + return @string != null && @string.TypeKind != TypeKind.Error; } } } diff --git a/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs b/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs index b0d07db459..ebb649da5b 100644 --- a/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs +++ b/src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs @@ -31,8 +31,15 @@ namespace Microsoft.CodeAnalysis.Razor return; } + var iTagHelper = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper); + if (iTagHelper == null || iTagHelper.TypeKind == TypeKind.Error) + { + // Could not find attributes we care about in the compilation. Nothing to do. + return; + } + var types = new List(); - var visitor = TagHelperTypeVisitor.Create(compilation, types); + var visitor = new TagHelperTypeVisitor(iTagHelper, types); // We always visit the global namespace. visitor.Visit(compilation.Assembly.GlobalNamespace); diff --git a/src/Microsoft.CodeAnalysis.Razor/TagHelperTypeVisitor.cs b/src/Microsoft.CodeAnalysis.Razor/TagHelperTypeVisitor.cs index aa240a1eac..3387cfd1e8 100644 --- a/src/Microsoft.CodeAnalysis.Razor/TagHelperTypeVisitor.cs +++ b/src/Microsoft.CodeAnalysis.Razor/TagHelperTypeVisitor.cs @@ -11,12 +11,6 @@ namespace Microsoft.CodeAnalysis.Razor private INamedTypeSymbol _interface; private List _results; - public static TagHelperTypeVisitor Create(Compilation compilation, List results) - { - var @interface = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper); - return new TagHelperTypeVisitor(@interface, results); - } - public TagHelperTypeVisitor(INamedTypeSymbol @interface, List results) { _interface = @interface; diff --git a/test/Microsoft.CodeAnalysis.Razor.Test/CompilationTagHelperFeatureTest.cs b/test/Microsoft.CodeAnalysis.Razor.Test/CompilationTagHelperFeatureTest.cs index e545d671e2..53aeb98031 100644 --- a/test/Microsoft.CodeAnalysis.Razor.Test/CompilationTagHelperFeatureTest.cs +++ b/test/Microsoft.CodeAnalysis.Razor.Test/CompilationTagHelperFeatureTest.cs @@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.Razor public class CompilationTagHelperFeatureTest { [Fact] - public void IsValidCompilation_ReturnsFalseIfITagHelperInterfaceCannotBeFound() + public void IsValidCompilation_ReturnsTrueIfTagHelperInterfaceCannotBeFound() { // Arrange var references = new[] @@ -26,7 +26,7 @@ namespace Microsoft.CodeAnalysis.Razor var result = CompilationTagHelperFeature.IsValidCompilation(compilation); // Assert - Assert.False(result); + Assert.True(result); } [Fact]