Check for ITagHelper in tag helper feature provider (#2602)

Fixes #2482
This commit is contained in:
Pranav K 2018-09-18 16:52:34 -07:00 committed by GitHub
parent fc86cc3ca1
commit 7144add4ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 31 additions and 41 deletions

View File

@ -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<INamedTypeSymbol>();
var visitor = ViewComponentTypeVisitor.Create(compilation, types);
var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);
// We always visit the global namespace.
visitor.Visit(compilation.Assembly.GlobalNamespace);

View File

@ -16,13 +16,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
private readonly INamedTypeSymbol _nonViewComponentAttribute;
private readonly List<INamedTypeSymbol> _results;
public static ViewComponentTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> 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 ||

View File

@ -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<INamedTypeSymbol>();
var visitor = ViewComponentTypeVisitor.Create(compilation, types);
var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);
// We always visit the global namespace.
visitor.Visit(compilation.Assembly.GlobalNamespace);

View File

@ -13,13 +13,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
private readonly INamedTypeSymbol _nonViewComponentAttribute;
private readonly List<INamedTypeSymbol> _results;
public static ViewComponentTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> 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,

View File

@ -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;
}
}
}

View File

@ -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<INamedTypeSymbol>();
var visitor = TagHelperTypeVisitor.Create(compilation, types);
var visitor = new TagHelperTypeVisitor(iTagHelper, types);
// We always visit the global namespace.
visitor.Visit(compilation.Assembly.GlobalNamespace);

View File

@ -11,12 +11,6 @@ namespace Microsoft.CodeAnalysis.Razor
private INamedTypeSymbol _interface;
private List<INamedTypeSymbol> _results;
public static TagHelperTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> results)
{
var @interface = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper);
return new TagHelperTypeVisitor(@interface, results);
}
public TagHelperTypeVisitor(INamedTypeSymbol @interface, List<INamedTypeSymbol> results)
{
_interface = @interface;

View File

@ -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]