-
Notifications
You must be signed in to change notification settings - Fork 229
Merge duplicate tag helper descriptors #8730
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/Compiler/Microsoft.AspNetCore.Razor.Language/src/TagHelperDescriptorSimpleComparer.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // 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 Microsoft.AspNetCore.Razor.Language.Components; | ||
| using Microsoft.Extensions.Internal; | ||
|
|
||
| namespace Microsoft.AspNetCore.Razor.Language; | ||
|
|
||
| /// <summary> | ||
| /// Considers two descriptors equal if they are definitions of the same class. | ||
| /// </summary> | ||
| internal sealed class TagHelperDescriptorSimpleComparer : IEqualityComparer<TagHelperDescriptor> | ||
| { | ||
| public static readonly TagHelperDescriptorSimpleComparer Default = new(); | ||
|
|
||
| private TagHelperDescriptorSimpleComparer() { } | ||
|
|
||
| public bool Equals(TagHelperDescriptor? x, TagHelperDescriptor? y) | ||
| { | ||
| if (ReferenceEquals(x, y)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| if (x is null || y is null) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| return x.Kind == y.Kind && | ||
| x.AssemblyName == y.AssemblyName && | ||
| x.Name == y.Name && | ||
| x.IsComponentFullyQualifiedNameMatch() == y.IsComponentFullyQualifiedNameMatch(); | ||
| } | ||
|
|
||
| public int GetHashCode(TagHelperDescriptor obj) | ||
| { | ||
| var hash = HashCodeCombiner.Start(); | ||
| hash.Add(obj.Kind, StringComparer.Ordinal); | ||
| hash.Add(obj.AssemblyName, StringComparer.Ordinal); | ||
| hash.Add(obj.Name, StringComparer.Ordinal); | ||
| return hash.CombinedHash; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorComponentTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Razor.Test.Common; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.NET.Sdk.Razor.SourceGenerators; | ||
|
|
||
| public sealed class RazorSourceGeneratorComponentTests : RazorSourceGeneratorTestsBase | ||
| { | ||
| [Fact, WorkItem("https://github.com/dotnet/razor/issues/8718")] | ||
| public async Task PartialClass() | ||
| { | ||
| // Arrange | ||
| var project = CreateTestProject(new() | ||
| { | ||
| ["Views/Home/Index.cshtml"] = """ | ||
| @(await Html.RenderComponentAsync<MyApp.Shared.Component1>(RenderMode.Static)) | ||
| """, | ||
| ["Shared/Component1.razor"] = """ | ||
| <Component2 Param="42" /> | ||
| """, | ||
| ["Shared/Component2.razor"] = """ | ||
| @inherits ComponentBase | ||
|
|
||
| Value: @(Param + 1) | ||
|
|
||
| @code { | ||
| [Parameter] | ||
| public int Param { get; set; } | ||
| } | ||
| """ | ||
| }, new() | ||
| { | ||
| ["Component2.razor.cs"] = """ | ||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace MyApp.Shared; | ||
|
|
||
| public partial class Component2 : ComponentBase { } | ||
| """ | ||
| }); | ||
| var compilation = await project.GetCompilationAsync(); | ||
| var driver = await GetDriverAsync(project, options => | ||
| { | ||
| options.TestGlobalOptions["build_property.RazorLangVersion"] = "7.0"; | ||
| }); | ||
|
|
||
| // Act | ||
| var result = RunGenerator(compilation!, ref driver, out compilation); | ||
|
|
||
| // Assert | ||
| Assert.Empty(result.Diagnostics); | ||
| Assert.Equal(3, result.GeneratedSources.Length); | ||
| await VerifyRazorPageMatchesBaselineAsync(compilation, "Views_Home_Index"); | ||
| } | ||
|
|
||
| [Fact, WorkItem("https://github.com/dotnet/razor/issues/8718")] | ||
| public async Task PartialClass_NoBaseInCSharp() | ||
| { | ||
| // Arrange | ||
| var project = CreateTestProject(new() | ||
| { | ||
| ["Views/Home/Index.cshtml"] = """ | ||
| @(await Html.RenderComponentAsync<MyApp.Shared.Component1>(RenderMode.Static)) | ||
| """, | ||
| ["Shared/Component1.razor"] = """ | ||
| <Component2 Param="42" /> | ||
| """, | ||
| ["Shared/Component2.razor"] = """ | ||
| @inherits ComponentBase | ||
|
|
||
| Value: @(Param + 1) | ||
|
|
||
| @code { | ||
| [Parameter] | ||
| public int Param { get; set; } | ||
| } | ||
| """ | ||
| }, new() | ||
| { | ||
| ["Component2.razor.cs"] = """ | ||
| using Microsoft.AspNetCore.Components; | ||
|
|
||
| namespace MyApp.Shared; | ||
|
|
||
| public partial class Component2 { } | ||
| """ | ||
| }); | ||
| var compilation = await project.GetCompilationAsync(); | ||
| var driver = await GetDriverAsync(project, options => | ||
| { | ||
| options.TestGlobalOptions["build_property.RazorLangVersion"] = "7.0"; | ||
| }); | ||
|
|
||
| // Act | ||
| var result = RunGenerator(compilation!, ref driver, out compilation); | ||
|
|
||
| // Assert | ||
| Assert.Empty(result.Diagnostics); | ||
| Assert.Equal(3, result.GeneratedSources.Length); | ||
| await VerifyRazorPageMatchesBaselineAsync(compilation, "Views_Home_Index"); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,7 @@ | |
| using System.Threading.Tasks; | ||
| using Microsoft.AspNetCore.Builder; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Http.Features; | ||
| using Microsoft.AspNetCore.Mvc; | ||
| using Microsoft.AspNetCore.Mvc.Abstractions; | ||
| using Microsoft.AspNetCore.Mvc.ApplicationParts; | ||
|
|
@@ -154,6 +155,14 @@ protected static async Task<string> RenderRazorPageAsync(Compilation compilation | |
| { | ||
| RequestServices = app.Services | ||
| }; | ||
| var requestFeature = new HttpRequestFeature | ||
| { | ||
| Method = HttpMethods.Get, | ||
| Protocol = HttpProtocol.Http2, | ||
| Scheme = "http" | ||
| }; | ||
| requestFeature.Headers.Host = "localhost"; | ||
| httpContext.Features.Set<IHttpRequestFeature>(requestFeature); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Small improvement of the test infra to allow rendering also Blazor components. |
||
| var actionContext = new ActionContext( | ||
| httpContext, | ||
| new AspNetCore.Routing.RouteData(), | ||
|
|
||
2 changes: 2 additions & 0 deletions
2
...ors.Tests/TestFiles/RazorSourceGeneratorComponentTests/PartialClass/Views_Home_Index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| | ||
| Value: 43 |
2 changes: 2 additions & 0 deletions
2
...iles/RazorSourceGeneratorComponentTests/PartialClass_NoBaseInCSharp/Views_Home_Index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| | ||
| Value: 43 |
2 changes: 2 additions & 0 deletions
2
.../TestFiles/RazorSourceGeneratorTagHelperTests/ComponentAndTagHelper/Views_Home_Index.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| | ||
| <a href="mailto:example">custom tag helper</a> |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Broken assert - a component does not have to inherit from
IComponent, nothing stops users from declaring component likeThis manifested in the new test
ComponentAndTagHelperwhere a component inherits fromTagHelper.