Skip to content

Commit 7144add

Browse files
authored
Check for ITagHelper in tag helper feature provider (#2602)
Fixes #2482
1 parent fc86cc3 commit 7144add

File tree

8 files changed

+31
-41
lines changed

8 files changed

+31
-41
lines changed

Diff for: src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTagHelperDescriptorProvider.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ public void Execute(TagHelperDescriptorProviderContext context)
2727
return;
2828
}
2929

30+
var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute);
31+
var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute);
32+
if (vcAttribute == null || vcAttribute.TypeKind == TypeKind.Error)
33+
{
34+
// Could not find attributes we care about in the compilation. Nothing to do.
35+
return;
36+
}
37+
3038
var types = new List<INamedTypeSymbol>();
31-
var visitor = ViewComponentTypeVisitor.Create(compilation, types);
39+
var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);
3240

3341
// We always visit the global namespace.
3442
visitor.Visit(compilation.Assembly.GlobalNamespace);

Diff for: src/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/ViewComponentTypeVisitor.cs

-16
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@ internal class ViewComponentTypeVisitor : SymbolVisitor
1616
private readonly INamedTypeSymbol _nonViewComponentAttribute;
1717
private readonly List<INamedTypeSymbol> _results;
1818

19-
public static ViewComponentTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> results)
20-
{
21-
var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute);
22-
var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute);
23-
return new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, results);
24-
}
25-
2619
public ViewComponentTypeVisitor(
2720
INamedTypeSymbol viewComponentAttribute,
2821
INamedTypeSymbol nonViewComponentAttribute,
@@ -31,12 +24,8 @@ public ViewComponentTypeVisitor(
3124
_viewComponentAttribute = viewComponentAttribute;
3225
_nonViewComponentAttribute = nonViewComponentAttribute;
3326
_results = results;
34-
35-
Enabled = _viewComponentAttribute != null;
3627
}
3728

38-
public bool Enabled { get; set; }
39-
4029
public override void VisitNamedType(INamedTypeSymbol symbol)
4130
{
4231
if (IsViewComponent(symbol))
@@ -65,11 +54,6 @@ public override void VisitNamespace(INamespaceSymbol symbol)
6554

6655
internal bool IsViewComponent(INamedTypeSymbol symbol)
6756
{
68-
if (!Enabled)
69-
{
70-
return false;
71-
}
72-
7357
if (symbol.DeclaredAccessibility != Accessibility.Public ||
7458
symbol.IsAbstract ||
7559
symbol.IsGenericType ||

Diff for: src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTagHelperDescriptorProvider.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ public void Execute(TagHelperDescriptorProviderContext context)
2727
return;
2828
}
2929

30+
var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute);
31+
var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute);
32+
if (vcAttribute == null || vcAttribute.TypeKind == TypeKind.Error)
33+
{
34+
// Could not find attributes we care about in the compilation. Nothing to do.
35+
return;
36+
}
37+
3038
var types = new List<INamedTypeSymbol>();
31-
var visitor = ViewComponentTypeVisitor.Create(compilation, types);
39+
var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);
3240

3341
// We always visit the global namespace.
3442
visitor.Visit(compilation.Assembly.GlobalNamespace);

Diff for: src/Microsoft.AspNetCore.Mvc.Razor.Extensions/ViewComponentTypeVisitor.cs

-7
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@ internal class ViewComponentTypeVisitor : SymbolVisitor
1313
private readonly INamedTypeSymbol _nonViewComponentAttribute;
1414
private readonly List<INamedTypeSymbol> _results;
1515

16-
public static ViewComponentTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> results)
17-
{
18-
var vcAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.ViewComponentAttribute);
19-
var nonVCAttribute = compilation.GetTypeByMetadataName(ViewComponentTypes.NonViewComponentAttribute);
20-
return new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, results);
21-
}
22-
2316
public ViewComponentTypeVisitor(
2417
INamedTypeSymbol viewComponentAttribute,
2518
INamedTypeSymbol nonViewComponentAttribute,

Diff for: src/Microsoft.CodeAnalysis.Razor/CompilationTagHelperFeature.cs

+3-7
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ protected override void OnInitialized()
4040

4141
internal static bool IsValidCompilation(Compilation compilation)
4242
{
43-
var iTagHelper = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper);
4443
var @string = compilation.GetSpecialType(SpecialType.System_String);
4544

46-
// Do some minimal tests to verify the compilation is valid. If symbols for ITagHelper or System.String
47-
// are missing or errored, the compilation may be missing references.
48-
return iTagHelper != null &&
49-
iTagHelper.TypeKind != TypeKind.Error &&
50-
@string != null &&
51-
@string.TypeKind != TypeKind.Error;
45+
// Do some minimal tests to verify the compilation is valid. If symbols for System.String
46+
// is missing or errored, the compilation may be missing references.
47+
return @string != null && @string.TypeKind != TypeKind.Error;
5248
}
5349
}
5450
}

Diff for: src/Microsoft.CodeAnalysis.Razor/DefaultTagHelperDescriptorProvider.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,15 @@ public void Execute(TagHelperDescriptorProviderContext context)
3131
return;
3232
}
3333

34+
var iTagHelper = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper);
35+
if (iTagHelper == null || iTagHelper.TypeKind == TypeKind.Error)
36+
{
37+
// Could not find attributes we care about in the compilation. Nothing to do.
38+
return;
39+
}
40+
3441
var types = new List<INamedTypeSymbol>();
35-
var visitor = TagHelperTypeVisitor.Create(compilation, types);
42+
var visitor = new TagHelperTypeVisitor(iTagHelper, types);
3643

3744
// We always visit the global namespace.
3845
visitor.Visit(compilation.Assembly.GlobalNamespace);

Diff for: src/Microsoft.CodeAnalysis.Razor/TagHelperTypeVisitor.cs

-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ internal class TagHelperTypeVisitor : SymbolVisitor
1111
private INamedTypeSymbol _interface;
1212
private List<INamedTypeSymbol> _results;
1313

14-
public static TagHelperTypeVisitor Create(Compilation compilation, List<INamedTypeSymbol> results)
15-
{
16-
var @interface = compilation.GetTypeByMetadataName(TagHelperTypes.ITagHelper);
17-
return new TagHelperTypeVisitor(@interface, results);
18-
}
19-
2014
public TagHelperTypeVisitor(INamedTypeSymbol @interface, List<INamedTypeSymbol> results)
2115
{
2216
_interface = @interface;

Diff for: test/Microsoft.CodeAnalysis.Razor.Test/CompilationTagHelperFeatureTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.CodeAnalysis.Razor
1313
public class CompilationTagHelperFeatureTest
1414
{
1515
[Fact]
16-
public void IsValidCompilation_ReturnsFalseIfITagHelperInterfaceCannotBeFound()
16+
public void IsValidCompilation_ReturnsTrueIfTagHelperInterfaceCannotBeFound()
1717
{
1818
// Arrange
1919
var references = new[]
@@ -26,7 +26,7 @@ public void IsValidCompilation_ReturnsFalseIfITagHelperInterfaceCannotBeFound()
2626
var result = CompilationTagHelperFeature.IsValidCompilation(compilation);
2727

2828
// Assert
29-
Assert.False(result);
29+
Assert.True(result);
3030
}
3131

3232
[Fact]

0 commit comments

Comments
 (0)