Skip to content

Commit 23b7552

Browse files
committed
Match IComponent interface by symbol instead of name
1 parent 6e78f5a commit 23b7552

File tree

6 files changed

+40
-34
lines changed

6 files changed

+40
-34
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentsApi.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
namespace Microsoft.AspNetCore.Razor.Language.Components;
75

86
// Constants for method names used in code-generation
@@ -40,9 +38,8 @@ public static class InjectAttribute
4038

4139
public static class IComponent
4240
{
43-
public const string FullTypeName = "Microsoft.AspNetCore.Components.IComponent";
44-
45-
public const string MetadataName = FullTypeName;
41+
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsIComponent;
42+
public const string FullTypeName = MetadataName;
4643
}
4744

4845
public static class IDictionary
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
namespace Microsoft.CodeAnalysis.Razor;
4+
namespace Microsoft.AspNetCore.Razor.Language;
55

6-
internal static class WellKnownTypeNames
6+
public static class WellKnownTypeNames
77
{
8+
public const string MicrosoftAspNetCoreComponentsIComponent = "Microsoft.AspNetCore.Components.IComponent";
89
public const string SystemThreadingTasksTask1 = "System.Threading.Tasks.Task`1";
910
}
Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable enable
5-
64
using System;
7-
using System.Linq;
85

96
namespace Microsoft.CodeAnalysis.Razor;
107

@@ -25,24 +22,6 @@ public static bool IsComponent(INamedTypeSymbol symbol, INamedTypeSymbol icompon
2522
return
2623
symbol.DeclaredAccessibility == Accessibility.Public &&
2724
!symbol.IsAbstract &&
28-
symbol.AllInterfaces.Contains(icomponentSymbol);
29-
}
30-
31-
public static bool IsComponent(INamedTypeSymbol symbol, string icomponentSymbolName)
32-
{
33-
if (symbol is null)
34-
{
35-
throw new ArgumentNullException(nameof(symbol));
36-
}
37-
38-
if (icomponentSymbolName is null)
39-
{
40-
throw new ArgumentNullException(nameof(icomponentSymbolName));
41-
}
42-
43-
return
44-
symbol.DeclaredAccessibility == Accessibility.Public &&
45-
!symbol.IsAbstract &&
46-
symbol.AllInterfaces.Any(s => s.HasFullName(icomponentSymbolName));
25+
symbol.AllInterfaces.Contains(icomponentSymbol, SymbolEqualityComparer.Default);
4726
}
4827
}

src/Compiler/Microsoft.CodeAnalysis.Razor/src/ComponentTagHelperDescriptorProvider.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ public void Execute(TagHelperDescriptorProviderContext context)
3030
throw new ArgumentNullException(nameof(context));
3131
}
3232

33-
var compilation = context.GetCompilation();
34-
if (compilation == null)
33+
var typeProvider = context.GetTypeProvider();
34+
if (typeProvider == null
35+
|| !typeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftAspNetCoreComponentsIComponent, out var icomponentSymbol))
3536
{
3637
// No compilation, nothing to do.
3738
return;
3839
}
3940

4041
var types = new List<INamedTypeSymbol>();
41-
var visitor = new ComponentTypeVisitor(types);
42+
var visitor = new ComponentTypeVisitor(types, icomponentSymbol);
4243

4344
var targetSymbol = context.Items.GetTargetSymbol();
4445
if (targetSymbol is not null)
@@ -47,6 +48,7 @@ public void Execute(TagHelperDescriptorProviderContext context)
4748
}
4849
else
4950
{
51+
var compilation = typeProvider.Compilation;
5052
visitor.Visit(compilation.Assembly.GlobalNamespace);
5153
foreach (var reference in compilation.References)
5254
{
@@ -575,15 +577,17 @@ private enum PropertyKind
575577
private class ComponentTypeVisitor : SymbolVisitor
576578
{
577579
private readonly List<INamedTypeSymbol> _results;
580+
private readonly INamedTypeSymbol _icomponentSymbol;
578581

579-
public ComponentTypeVisitor(List<INamedTypeSymbol> results)
582+
public ComponentTypeVisitor(List<INamedTypeSymbol> results, INamedTypeSymbol icomponentSymbol)
580583
{
581584
_results = results;
585+
_icomponentSymbol = icomponentSymbol;
582586
}
583587

584588
public override void VisitNamedType(INamedTypeSymbol symbol)
585589
{
586-
if (ComponentDetectionConventions.IsComponent(symbol, ComponentsApi.IComponent.MetadataName))
590+
if (ComponentDetectionConventions.IsComponent(symbol, _icomponentSymbol))
587591
{
588592
_results.Add(symbol);
589593
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Collections.Generic;
5+
using System.Collections.Immutable;
6+
7+
namespace Microsoft.CodeAnalysis.Razor;
8+
9+
internal static class ImmutableArrayExtensions
10+
{
11+
public static bool Contains<T, TComparer>(this ImmutableArray<T> array, T item, TComparer comparer)
12+
where TComparer : IEqualityComparer<T>
13+
{
14+
foreach (var actual in array)
15+
{
16+
if (comparer.Equals(actual, item))
17+
{
18+
return true;
19+
}
20+
}
21+
22+
return false;
23+
}
24+
}

src/Compiler/Microsoft.CodeAnalysis.Razor/src/WellKnownTypeProvider.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Globalization;
1111
using System.Linq;
1212
using System.Threading;
13+
using Microsoft.AspNetCore.Razor.Language;
1314

1415
namespace Microsoft.CodeAnalysis.Razor;
1516

0 commit comments

Comments
 (0)