Skip to content

Commit

Permalink
Match IComponent interface by symbol instead of name
Browse files Browse the repository at this point in the history
  • Loading branch information
sharwell committed Feb 22, 2023
1 parent 6e78f5a commit 7b77a7c
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable
using Microsoft.CodeAnalysis.Razor;

namespace Microsoft.AspNetCore.Razor.Language.Components;

Expand Down Expand Up @@ -40,9 +40,8 @@ public static class InjectAttribute

public static class IComponent
{
public const string FullTypeName = "Microsoft.AspNetCore.Components.IComponent";

public const string MetadataName = FullTypeName;
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsIComponent;
public const string FullTypeName = MetadataName;
}

public static class IDictionary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System;
using System.Linq;

namespace Microsoft.CodeAnalysis.Razor;

Expand All @@ -25,24 +22,6 @@ public static bool IsComponent(INamedTypeSymbol symbol, INamedTypeSymbol icompon
return
symbol.DeclaredAccessibility == Accessibility.Public &&
!symbol.IsAbstract &&
symbol.AllInterfaces.Contains(icomponentSymbol);
}

public static bool IsComponent(INamedTypeSymbol symbol, string icomponentSymbolName)
{
if (symbol is null)
{
throw new ArgumentNullException(nameof(symbol));
}

if (icomponentSymbolName is null)
{
throw new ArgumentNullException(nameof(icomponentSymbolName));
}

return
symbol.DeclaredAccessibility == Accessibility.Public &&
!symbol.IsAbstract &&
symbol.AllInterfaces.Any(s => s.HasFullName(icomponentSymbolName));
symbol.AllInterfaces.Contains(icomponentSymbol, SymbolEqualityComparer.Default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,16 @@ public void Execute(TagHelperDescriptorProviderContext context)
throw new ArgumentNullException(nameof(context));
}

var compilation = context.GetCompilation();
if (compilation == null)
var typeProvider = context.GetTypeProvider();
if (typeProvider == null
|| !typeProvider.TryGetOrCreateTypeByMetadataName(WellKnownTypeNames.MicrosoftAspNetCoreComponentsIComponent, out var icomponentSymbol))
{
// No compilation, nothing to do.
return;
}

var types = new List<INamedTypeSymbol>();
var visitor = new ComponentTypeVisitor(types);
var visitor = new ComponentTypeVisitor(types, icomponentSymbol);

var targetSymbol = context.Items.GetTargetSymbol();
if (targetSymbol is not null)
Expand All @@ -47,6 +48,7 @@ public void Execute(TagHelperDescriptorProviderContext context)
}
else
{
var compilation = typeProvider.Compilation;
visitor.Visit(compilation.Assembly.GlobalNamespace);
foreach (var reference in compilation.References)
{
Expand Down Expand Up @@ -575,15 +577,17 @@ private enum PropertyKind
private class ComponentTypeVisitor : SymbolVisitor
{
private readonly List<INamedTypeSymbol> _results;
private readonly INamedTypeSymbol _icomponentSymbol;

public ComponentTypeVisitor(List<INamedTypeSymbol> results)
public ComponentTypeVisitor(List<INamedTypeSymbol> results, INamedTypeSymbol icomponentSymbol)
{
_results = results;
_icomponentSymbol = icomponentSymbol;
}

public override void VisitNamedType(INamedTypeSymbol symbol)
{
if (ComponentDetectionConventions.IsComponent(symbol, ComponentsApi.IComponent.MetadataName))
if (ComponentDetectionConventions.IsComponent(symbol, _icomponentSymbol))
{
_results.Add(symbol);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace Microsoft.CodeAnalysis.Razor;

internal static class ImmutableArrayExtensions
{
public static bool Contains<T, TComparer>(this ImmutableArray<T> array, T item, TComparer comparer)
where TComparer : IEqualityComparer<T>
{
foreach (var actual in array)
{
if (comparer.Equals(actual, item))
{
return true;
}
}

return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

namespace Microsoft.CodeAnalysis.Razor;

internal static class WellKnownTypeNames
public static class WellKnownTypeNames
{
public const string MicrosoftAspNetCoreComponentsIComponent = "Microsoft.AspNetCore.Components.IComponent";
public const string SystemThreadingTasksTask1 = "System.Threading.Tasks.Task`1";
}

0 comments on commit 7b77a7c

Please sign in to comment.