Skip to content

Use IAssemblySymbol for tag helper discovery #30560

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

Merged
merged 1 commit into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -38,13 +38,10 @@ public void Execute(TagHelperDescriptorProviderContext context)
var types = new List<INamedTypeSymbol>();
var visitor = new ViewComponentTypeVisitor(vcAttribute, nonVCAttribute, types);

var targetReference = context.Items.GetTargetMetadataReference();
if (targetReference is not null)
var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null)
{
if (compilation.GetAssemblyOrModuleSymbol(targetReference) is IAssemblySymbol targetAssembly && IsTagHelperAssembly(targetAssembly))
{
visitor.Visit(targetAssembly.GlobalNamespace);
}
visitor.Visit(targetAssembly.GlobalNamespace);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public void Execute(TagHelperDescriptorProviderContext context)
return;
}

var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null && !SymbolEqualityComparer.Default.Equals(targetAssembly, bindMethods.ContainingAssembly))
{
return;
}

// Tag Helper defintion for case #1. This is the most general case.
context.Results.Add(CreateFallbackBindTagHelper());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,10 @@ public void Execute(TagHelperDescriptorProviderContext context)
var types = new List<INamedTypeSymbol>();
var visitor = new ComponentTypeVisitor(symbols, types);

var targetReference = context.Items.GetTargetMetadataReference();
if (targetReference is not null)
var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null)
{
if (compilation.GetAssemblyOrModuleSymbol(targetReference) is IAssemblySymbol targetAssembly)
{
visitor.Visit(targetAssembly.GlobalNamespace);
}

visitor.Visit(targetAssembly.GlobalNamespace);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,10 @@ public void Execute(TagHelperDescriptorProviderContext context)
var types = new List<INamedTypeSymbol>();
var visitor = new TagHelperTypeVisitor(iTagHelper, types);

var targetReference = context.Items.GetTargetMetadataReference();
if (targetReference is not null)
var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null)
{
if (compilation.GetAssemblyOrModuleSymbol(targetReference) is IAssemblySymbol targetAssembly && IsTagHelperAssembly(targetAssembly))
{
visitor.Visit(targetAssembly.GlobalNamespace);
}
visitor.Visit(targetAssembly.GlobalNamespace);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,10 @@ private List<EventHandlerData> GetEventHandlerData(TagHelperDescriptorProviderCo
var types = new List<INamedTypeSymbol>();
var visitor = new EventHandlerDataVisitor(types);


var targetReference = context.Items.GetTargetMetadataReference();
if (targetReference is not null)
var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null)
{
if (compilation.GetAssemblyOrModuleSymbol(targetReference) is IAssemblySymbol targetAssembly)
{
visitor.Visit(targetAssembly.GlobalNamespace);
}

visitor.Visit(targetAssembly.GlobalNamespace);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void Execute(TagHelperDescriptorProviderContext context)
return;
}

var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null && !SymbolEqualityComparer.Default.Equals(targetAssembly, renderTreeBuilderType.ContainingAssembly))
{
return;
}

context.Results.Add(CreateKeyTagHelper());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void Execute(TagHelperDescriptorProviderContext context)
return;
}

var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null && !SymbolEqualityComparer.Default.Equals(targetAssembly, elementReference.ContainingAssembly))
{
return;
}

context.Results.Add(CreateRefTagHelper());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ public void Execute(TagHelperDescriptorProviderContext context)
return;
}

var targetAssembly = context.Items.GetTargetAssembly();
if (targetAssembly is not null && !SymbolEqualityComparer.Default.Equals(targetAssembly, renderTreeBuilder.ContainingAssembly))
{
return;
}

context.Results.Add(CreateSplatTagHelper());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@

namespace Microsoft.CodeAnalysis.Razor
{
internal static class TagHelperTargetReferenceExtensions
internal static class TagHelperTargetAssemblyExtensions
{
private static readonly object TargetAssemblyKey = new object();

public static MetadataReference? GetTargetMetadataReference(this ItemCollection items)
public static IAssemblySymbol? GetTargetAssembly(this ItemCollection items)
{
if (items.Count == 0 || items[TargetAssemblyKey] is not MetadataReference reference)
if (items.Count == 0 || items[TargetAssemblyKey] is not IAssemblySymbol symbol)
{
return null;
}

return reference;
return symbol;
}

public static void SetTargetMetadataReference(this ItemCollection items, MetadataReference reference)
public static void SetTargetAssembly(this ItemCollection items, IAssemblySymbol symbol)
{
items[TargetAssemblyKey] = reference;
items[TargetAssemblyKey] = symbol;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1617,7 +1617,7 @@ public Task SetParametersAsync(ParameterView parameters)

var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(compilation);
context.Items.SetTargetMetadataReference(compilation.References.Single(r => r.Display.Contains("Microsoft.CodeAnalysis.Razor.Test.dll")));
context.Items.SetTargetAssembly((IAssemblySymbol) compilation.GetAssemblyOrModuleSymbol(compilation.References.First(r => r.Display.Contains("Microsoft.CodeAnalysis.Razor.Test.dll"))));
var provider = new ComponentTagHelperDescriptorProvider();

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output) {
}

[Fact]
public void Execute_WithTargetMetadataReference_Works()
public void Execute_WithTargetAssembly_Works()
{
// Arrange
var testTagHelper = "TestAssembly.TestTagHelper";
Expand All @@ -104,7 +104,7 @@ public override void Process(TagHelperContext context, TagHelperOutput output) {

var context = TagHelperDescriptorProviderContext.Create();
context.SetCompilation(compilation);
context.Items.SetTargetMetadataReference(compilation.References.First(r => r.Display.Contains("Microsoft.CodeAnalysis.Razor.Test.dll")));
context.Items.SetTargetAssembly((IAssemblySymbol) compilation.GetAssemblyOrModuleSymbol(compilation.References.First(r => r.Display.Contains("Microsoft.CodeAnalysis.Razor.Test.dll"))));

// Act
descriptorProvider.Execute(context);
Expand Down