Check for ITagHelper in tag helper feature provider (#2602)
Fixes #2482
This commit is contained in:
parent
fc86cc3ca1
commit
7144add4ad
|
|
@ -27,8 +27,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
|
||||||
return;
|
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 types = new List<INamedTypeSymbol>();
|
||||||
var visitor = ViewComponentTypeVisitor.Create(compilation, types);
|
var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);
|
||||||
|
|
||||||
// We always visit the global namespace.
|
// We always visit the global namespace.
|
||||||
visitor.Visit(compilation.Assembly.GlobalNamespace);
|
visitor.Visit(compilation.Assembly.GlobalNamespace);
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
|
||||||
private readonly INamedTypeSymbol _nonViewComponentAttribute;
|
private readonly INamedTypeSymbol _nonViewComponentAttribute;
|
||||||
private readonly List<INamedTypeSymbol> _results;
|
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(
|
public ViewComponentTypeVisitor(
|
||||||
INamedTypeSymbol viewComponentAttribute,
|
INamedTypeSymbol viewComponentAttribute,
|
||||||
INamedTypeSymbol nonViewComponentAttribute,
|
INamedTypeSymbol nonViewComponentAttribute,
|
||||||
|
|
@ -31,12 +24,8 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
|
||||||
_viewComponentAttribute = viewComponentAttribute;
|
_viewComponentAttribute = viewComponentAttribute;
|
||||||
_nonViewComponentAttribute = nonViewComponentAttribute;
|
_nonViewComponentAttribute = nonViewComponentAttribute;
|
||||||
_results = results;
|
_results = results;
|
||||||
|
|
||||||
Enabled = _viewComponentAttribute != null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Enabled { get; set; }
|
|
||||||
|
|
||||||
public override void VisitNamedType(INamedTypeSymbol symbol)
|
public override void VisitNamedType(INamedTypeSymbol symbol)
|
||||||
{
|
{
|
||||||
if (IsViewComponent(symbol))
|
if (IsViewComponent(symbol))
|
||||||
|
|
@ -65,11 +54,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X
|
||||||
|
|
||||||
internal bool IsViewComponent(INamedTypeSymbol symbol)
|
internal bool IsViewComponent(INamedTypeSymbol symbol)
|
||||||
{
|
{
|
||||||
if (!Enabled)
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (symbol.DeclaredAccessibility != Accessibility.Public ||
|
if (symbol.DeclaredAccessibility != Accessibility.Public ||
|
||||||
symbol.IsAbstract ||
|
symbol.IsAbstract ||
|
||||||
symbol.IsGenericType ||
|
symbol.IsGenericType ||
|
||||||
|
|
|
||||||
|
|
@ -27,8 +27,16 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
|
||||||
return;
|
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 types = new List<INamedTypeSymbol>();
|
||||||
var visitor = ViewComponentTypeVisitor.Create(compilation, types);
|
var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);
|
||||||
|
|
||||||
// We always visit the global namespace.
|
// We always visit the global namespace.
|
||||||
visitor.Visit(compilation.Assembly.GlobalNamespace);
|
visitor.Visit(compilation.Assembly.GlobalNamespace);
|
||||||
|
|
|
||||||
|
|
@ -13,13 +13,6 @@ namespace Microsoft.AspNetCore.Mvc.Razor.Extensions
|
||||||
private readonly INamedTypeSymbol _nonViewComponentAttribute;
|
private readonly INamedTypeSymbol _nonViewComponentAttribute;
|
||||||
private readonly List<INamedTypeSymbol> _results;
|
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(
|
public ViewComponentTypeVisitor(
|
||||||
INamedTypeSymbol viewComponentAttribute,
|
INamedTypeSymbol viewComponentAttribute,
|
||||||
INamedTypeSymbol nonViewComponentAttribute,
|
INamedTypeSymbol nonViewComponentAttribute,
|
||||||
|
|
|
||||||
|
|
@ -40,15 +40,11 @@ namespace Microsoft.CodeAnalysis.Razor
|
||||||
|
|
||||||
internal static bool IsValidCompilation(Compilation compilation)
|
internal static bool IsValidCompilation(Compilation compilation)
|
||||||
{
|
{
|
||||||
var iTagHelper = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper);
|
|
||||||
var @string = compilation.GetSpecialType(SpecialType.System_String);
|
var @string = compilation.GetSpecialType(SpecialType.System_String);
|
||||||
|
|
||||||
// Do some minimal tests to verify the compilation is valid. If symbols for ITagHelper or System.String
|
// Do some minimal tests to verify the compilation is valid. If symbols for System.String
|
||||||
// are missing or errored, the compilation may be missing references.
|
// is missing or errored, the compilation may be missing references.
|
||||||
return iTagHelper != null &&
|
return @string != null && @string.TypeKind != TypeKind.Error;
|
||||||
iTagHelper.TypeKind != TypeKind.Error &&
|
|
||||||
@string != null &&
|
|
||||||
@string.TypeKind != TypeKind.Error;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -31,8 +31,15 @@ namespace Microsoft.CodeAnalysis.Razor
|
||||||
return;
|
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 types = new List<INamedTypeSymbol>();
|
||||||
var visitor = TagHelperTypeVisitor.Create(compilation, types);
|
var visitor = new TagHelperTypeVisitor(iTagHelper, types);
|
||||||
|
|
||||||
// We always visit the global namespace.
|
// We always visit the global namespace.
|
||||||
visitor.Visit(compilation.Assembly.GlobalNamespace);
|
visitor.Visit(compilation.Assembly.GlobalNamespace);
|
||||||
|
|
|
||||||
|
|
@ -11,12 +11,6 @@ namespace Microsoft.CodeAnalysis.Razor
|
||||||
private INamedTypeSymbol _interface;
|
private INamedTypeSymbol _interface;
|
||||||
private List<INamedTypeSymbol> _results;
|
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)
|
public TagHelperTypeVisitor(INamedTypeSymbol @interface, List<INamedTypeSymbol> results)
|
||||||
{
|
{
|
||||||
_interface = @interface;
|
_interface = @interface;
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.Razor
|
||||||
public class CompilationTagHelperFeatureTest
|
public class CompilationTagHelperFeatureTest
|
||||||
{
|
{
|
||||||
[Fact]
|
[Fact]
|
||||||
public void IsValidCompilation_ReturnsFalseIfITagHelperInterfaceCannotBeFound()
|
public void IsValidCompilation_ReturnsTrueIfTagHelperInterfaceCannotBeFound()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var references = new[]
|
var references = new[]
|
||||||
|
|
@ -26,7 +26,7 @@ namespace Microsoft.CodeAnalysis.Razor
|
||||||
var result = CompilationTagHelperFeature.IsValidCompilation(compilation);
|
var result = CompilationTagHelperFeature.IsValidCompilation(compilation);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
Assert.False(result);
|
Assert.True(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue