Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adopt WellKnownTypeProvider for type lookup by name #8310

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class StringParameterViewComponent
var compilation = MvcShim.BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));

var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(compilation);
context.SetTypeProvider(new WellKnownTypeProvider(compilation));

var provider = new ViewComponentTagHelperDescriptorProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class StringParameterViewComponent
var compilation = MvcShim.BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));

var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(compilation);
context.SetTypeProvider(new WellKnownTypeProvider(compilation));

var provider = new ViewComponentTagHelperDescriptorProvider()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class StringParameterViewComponent
var compilation = MvcShim.BaseCompilation.AddSyntaxTrees(CSharpSyntaxTree.ParseText(code));

var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(compilation);
context.SetTypeProvider(new WellKnownTypeProvider(compilation));

var provider = new ViewComponentTagHelperDescriptorProvider()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

namespace Microsoft.AspNetCore.Razor.Language.Components;

// Constants for method names used in code-generation
Expand All @@ -16,16 +14,16 @@ internal static class ComponentsApi
public static class ComponentBase
{
public const string Namespace = "Microsoft.AspNetCore.Components";
public const string FullTypeName = Namespace + ".ComponentBase";
public const string MetadataName = FullTypeName;
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsComponentBase;
public const string FullTypeName = MetadataName;

public const string BuildRenderTree = nameof(BuildRenderTree);
}

public static class ParameterAttribute
{
public const string FullTypeName = "Microsoft.AspNetCore.Components.ParameterAttribute";
public const string MetadataName = FullTypeName;
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsParameterAttribute;
public const string FullTypeName = MetadataName;
}

public static class LayoutAttribute
Expand All @@ -40,9 +38,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 All @@ -53,15 +50,15 @@ public static class IDictionary
public static class RenderFragment
{
public const string Namespace = "Microsoft.AspNetCore.Components";
public const string FullTypeName = Namespace + ".RenderFragment";
public const string MetadataName = FullTypeName;
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsRenderFragment;
public const string FullTypeName = MetadataName;
}

public static class RenderFragmentOfT
{
public const string Namespace = "Microsoft.AspNetCore.Components";
public const string FullTypeName = Namespace + ".RenderFragment<>";
public const string MetadataName = Namespace + ".RenderFragment`1";
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsRenderFragment1;
public const string DisplayName = Namespace + ".RenderFragment<TValue>";
}

Expand Down Expand Up @@ -141,15 +138,15 @@ public static class ElementReference

public static class EventCallback
{
public const string FullTypeName = "Microsoft.AspNetCore.Components.EventCallback";
public const string MetadataName = FullTypeName;
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsEventCallback;
public const string FullTypeName = MetadataName;

public const string FactoryAccessor = FullTypeName + ".Factory";
}

public static class EventCallbackOfT
{
public const string MetadataName = "Microsoft.AspNetCore.Components.EventCallback`1";
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsEventCallback1;
public const string DisplayName = "Microsoft.AspNetCore.Components.EventCallback<TValue>";
}

Expand All @@ -167,6 +164,6 @@ public static class BindConverter

public static class CascadingTypeParameterAttribute
{
public const string MetadataName = "Microsoft.AspNetCore.Components.CascadingTypeParameterAttribute";
public const string MetadataName = WellKnownTypeNames.MicrosoftAspNetCoreComponentsCascadingTypeParameterAttribute;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.ComponentModel;

namespace Microsoft.AspNetCore.Razor.Language;

public static class WellKnownTypeNames
{
public const string MicrosoftAspNetCoreComponentsCascadingTypeParameterAttribute = "Microsoft.AspNetCore.Components.CascadingTypeParameterAttribute";
public const string MicrosoftAspNetCoreComponentsComponentBase = "Microsoft.AspNetCore.Components.ComponentBase";
public const string MicrosoftAspNetCoreComponentsEditorRequiredAttribute = "Microsoft.AspNetCore.Components.EditorRequiredAttribute";
public const string MicrosoftAspNetCoreComponentsEventCallback = "Microsoft.AspNetCore.Components.EventCallback";
public const string MicrosoftAspNetCoreComponentsEventCallback1 = "Microsoft.AspNetCore.Components.EventCallback`1";
public const string MicrosoftAspNetCoreComponentsIComponent = "Microsoft.AspNetCore.Components.IComponent";
public const string MicrosoftAspNetCoreComponentsParameterAttribute = "Microsoft.AspNetCore.Components.ParameterAttribute";
public const string MicrosoftAspNetCoreComponentsRenderFragment = "Microsoft.AspNetCore.Components.RenderFragment";
public const string MicrosoftAspNetCoreComponentsRenderFragment1 = "Microsoft.AspNetCore.Components.RenderFragment`1";
public const string MicrosoftAspNetCoreRazorTagHelpersHtmlAttributeNameAttribute = "Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeNameAttribute";
public const string MicrosoftAspNetCoreRazorTagHelpersHtmlAttributeNotBoundAttribute = "Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeNotBoundAttribute";
public const string MicrosoftAspNetCoreRazorTagHelpersHtmlTargetElementAttribute = "Microsoft.AspNetCore.Razor.TagHelpers.HtmlTargetElementAttribute";
public const string MicrosoftAspNetCoreRazorTagHelpersITagHelper = "Microsoft.AspNetCore.Razor.TagHelpers.ITagHelper";
public const string MicrosoftAspNetCoreRazorTagHelpersOutputElementHintAttribute = "Microsoft.AspNetCore.Razor.TagHelpers.OutputElementHintAttribute";
public const string MicrosoftAspNetCoreRazorTagHelpersRestrictChildrenAttribute = "Microsoft.AspNetCore.Razor.TagHelpers.RestrictChildrenAttribute";
public const string SystemCollectionsGenericIDictionary2 = "System.Collections.Generic.IDictionary`2";
public const string SystemComponentModelEditorBrowsableAttribute = $"{nameof(System)}.{nameof(System.ComponentModel)}.{nameof(EditorBrowsableAttribute)}";
public const string SystemThreadingTasksTask1 = "System.Threading.Tasks.Task`1";
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#nullable disable

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.CodeAnalysis.CSharp;
Expand All @@ -23,7 +24,7 @@ public IReadOnlyList<TagHelperDescriptor> GetDescriptors()
var compilation = CSharpCompilation.Create("__TagHelpers", references: _referenceFeature.References);
if (IsValidCompilation(compilation))
{
context.SetCompilation(compilation);
context.SetTypeProvider(new WellKnownTypeProvider(compilation));
}

for (var i = 0; i < _providers.Length; i++)
Expand All @@ -40,7 +41,7 @@ protected override void OnInitialized()
_providers = Engine.Features.OfType<ITagHelperDescriptorProvider>().OrderBy(f => f.Order).ToArray();
}

internal static bool IsValidCompilation(Compilation compilation)
internal static bool IsValidCompilation([NotNullWhen(true)] Compilation compilation)
{
var @string = compilation.GetSpecialType(SpecialType.System_String);

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);
}
}
Loading