Skip to content

Commit 1f691cc

Browse files
authored
Fuse/typeparamintellisense (#11795)
Track tokens in the TypeNameRewriter so we can emit source information for where the supplied type param comes from. Fixes #11552
1 parent edfd8fd commit 1f691cc

File tree

125 files changed

+2487
-365
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+2487
-365
lines changed

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9339,6 +9339,150 @@ public class MyComponent<T> : ComponentBase where T : IMyInterface
93399339
CompileToAssembly(generated);
93409340
}
93419341

9342+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11552")]
9343+
public void GenericComponentTypeUsage()
9344+
{
9345+
// Act
9346+
var generated = CompileToCSharp("""
9347+
@typeparam TItem
9348+
@code {
9349+
[Parameter]
9350+
public TItem MyItem { get; set; }
9351+
}
9352+
9353+
<TestComponent TItem="string" />
9354+
""");
9355+
9356+
// Assert
9357+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9358+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9359+
CompileToAssembly(generated);
9360+
}
9361+
9362+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11552")]
9363+
public void GenericComponentTypeUsageWithInference()
9364+
{
9365+
// Act
9366+
var generated = CompileToCSharp("""
9367+
@typeparam TItem
9368+
@code {
9369+
[Parameter]
9370+
public TItem MyItem { get; set; }
9371+
}
9372+
9373+
<TestComponent MyItem="1" />
9374+
""");
9375+
9376+
// Assert
9377+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9378+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9379+
CompileToAssembly(generated);
9380+
}
9381+
9382+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11552")]
9383+
public void GenericComponentMultipleTypeParamUsage()
9384+
{
9385+
// Act
9386+
var generated = CompileToCSharp("""
9387+
@typeparam TItem
9388+
@typeparam TItem2
9389+
@code {
9390+
[Parameter]
9391+
public TItem MyItem { get; set; }
9392+
9393+
[Parameter]
9394+
public TItem2 MyItem2 { get; set; }
9395+
}
9396+
9397+
<TestComponent TItem2="int" TItem="string" />
9398+
""");
9399+
9400+
// Assert
9401+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9402+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9403+
CompileToAssembly(generated);
9404+
}
9405+
9406+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11552")]
9407+
public void GenericComponentTypeParamUsageWithImplicitExpression()
9408+
{
9409+
// Act
9410+
var generated = CompileToCSharp("""
9411+
@typeparam TItem
9412+
@code {
9413+
[Parameter]
9414+
public TItem MyItem { get; set; }
9415+
}
9416+
9417+
<TestComponent TItem="@string" />
9418+
""");
9419+
9420+
// Assert
9421+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9422+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9423+
CompileToAssembly(generated);
9424+
}
9425+
9426+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11552")]
9427+
public void GenericComponentTypeParamUsageWithImplicitExpression2()
9428+
{
9429+
// Act
9430+
var generated = CompileToCSharp("""
9431+
@typeparam TItem
9432+
@code {
9433+
[Parameter]
9434+
public TItem MyItem { get; set; }
9435+
}
9436+
9437+
<TestComponent TItem="@(string)" />
9438+
""");
9439+
9440+
// Assert
9441+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9442+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9443+
CompileToAssembly(generated);
9444+
}
9445+
9446+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11552")]
9447+
public void GenericComponentTypeUsageWhitespace()
9448+
{
9449+
// Act
9450+
var generated = CompileToCSharp("""
9451+
@typeparam TItem
9452+
@code {
9453+
[Parameter]
9454+
public TItem MyItem { get; set; }
9455+
}
9456+
9457+
<TestComponent TItem=" string " />
9458+
""");
9459+
9460+
// Assert
9461+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9462+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9463+
CompileToAssembly(generated);
9464+
}
9465+
9466+
[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/11552")]
9467+
public void GenericComponentTypeUsageWithGenericType()
9468+
{
9469+
// Act
9470+
var generated = CompileToCSharp("""
9471+
@typeparam TItem
9472+
@code {
9473+
[Parameter]
9474+
public TItem MyItem { get; set; }
9475+
}
9476+
9477+
<TestComponent TItem="TestComponent<string>" />
9478+
""");
9479+
9480+
// Assert
9481+
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
9482+
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
9483+
CompileToAssembly(generated);
9484+
}
9485+
93429486
#endregion
93439487

93449488
#region Key

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/ChildComponent_AtSpecifiedInRazorFileForTypeParameter/TestComponent.ir.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
MethodDeclaration - - protected override - void - BuildRenderTree
1717
Component - (0:0,0 [26] x:\dir\subdir\Test\TestComponent.cshtml) - C
1818
ComponentTypeArgument - (6:0,6 [7] x:\dir\subdir\Test\TestComponent.cshtml) - T
19-
CSharpExpression - (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml)
20-
LazyIntermediateToken - (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - string
19+
LazyIntermediateToken - (7:0,7 [6] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - string
2120
ComponentAttribute - (21:0,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - Item - Item - AttributeStructure.DoubleQuotes
2221
LazyIntermediateToken - (21:0,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line default
6+
using global::System;
7+
using global::System.Collections.Generic;
8+
using global::System.Linq;
9+
using global::System.Threading.Tasks;
10+
using global::Microsoft.AspNetCore.Components;
11+
#line default
12+
#line hidden
13+
#nullable restore
14+
public partial class TestComponent<
15+
#nullable restore
16+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
17+
TItem
18+
19+
#line default
20+
#line hidden
21+
#nullable disable
22+
,
23+
#nullable restore
24+
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
25+
TItem2
26+
27+
#line default
28+
#line hidden
29+
#nullable disable
30+
> : global::Microsoft.AspNetCore.Components.ComponentBase
31+
#nullable disable
32+
{
33+
#pragma warning disable 219
34+
private void __RazorDirectiveTokenHelpers__() {
35+
((global::System.Action)(() => {
36+
}
37+
))();
38+
((global::System.Action)(() => {
39+
}
40+
))();
41+
}
42+
#pragma warning restore 219
43+
#pragma warning disable 0414
44+
private static object __o = null;
45+
#pragma warning restore 0414
46+
#pragma warning disable 1998
47+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
48+
{
49+
__o = typeof(
50+
#nullable restore
51+
#line 11 "x:\dir\subdir\Test\TestComponent.cshtml"
52+
int
53+
54+
#line default
55+
#line hidden
56+
#nullable disable
57+
);
58+
__o = typeof(
59+
#nullable restore
60+
#line 11 "x:\dir\subdir\Test\TestComponent.cshtml"
61+
string
62+
63+
#line default
64+
#line hidden
65+
#nullable disable
66+
);
67+
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
68+
}
69+
));
70+
#nullable restore
71+
#line 11 "x:\dir\subdir\Test\TestComponent.cshtml"
72+
__o = typeof(global::Test.TestComponent<,>);
73+
74+
#line default
75+
#line hidden
76+
#nullable disable
77+
}
78+
#pragma warning restore 1998
79+
#nullable restore
80+
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"
81+
82+
[Parameter]
83+
public TItem MyItem { get; set; }
84+
85+
[Parameter]
86+
public TItem2 MyItem2 { get; set; }
87+
88+
#line default
89+
#line hidden
90+
#nullable disable
91+
}
92+
}
93+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
Document -
2+
NamespaceDeclaration - - Test
3+
UsingDirective - (3:1,1 [20] ) - global::System
4+
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
5+
UsingDirective - (69:3,1 [25] ) - global::System.Linq
6+
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
7+
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
8+
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase - - TItem, TItem2
9+
DesignTimeDirective -
10+
DirectiveToken - (11:0,11 [5] x:\dir\subdir\Test\TestComponent.cshtml) - TItem
11+
DirectiveToken - (29:1,11 [6] x:\dir\subdir\Test\TestComponent.cshtml) - TItem2
12+
CSharpCode -
13+
IntermediateToken - - CSharp - #pragma warning disable 0414
14+
CSharpCode -
15+
IntermediateToken - - CSharp - private static object __o = null;
16+
CSharpCode -
17+
IntermediateToken - - CSharp - #pragma warning restore 0414
18+
MethodDeclaration - - protected override - void - BuildRenderTree
19+
HtmlContent - (163:8,1 [4] x:\dir\subdir\Test\TestComponent.cshtml)
20+
LazyIntermediateToken - (163:8,1 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
21+
Component - (167:10,0 [45] x:\dir\subdir\Test\TestComponent.cshtml) - TestComponent
22+
ComponentTypeArgument - (190:10,23 [3] x:\dir\subdir\Test\TestComponent.cshtml) - TItem2
23+
LazyIntermediateToken - (190:10,23 [3] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - int
24+
ComponentTypeArgument - (202:10,35 [6] x:\dir\subdir\Test\TestComponent.cshtml) - TItem
25+
LazyIntermediateToken - (202:10,35 [6] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - string
26+
CSharpCode - (44:2,7 [118] x:\dir\subdir\Test\TestComponent.cshtml)
27+
LazyIntermediateToken - (44:2,7 [118] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n [Parameter]\n public TItem MyItem { get; set; }\n\n [Parameter]\n public TItem2 MyItem2 { get; set; }\n
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Source Location: (11:0,11 [5] x:\dir\subdir\Test\TestComponent.cshtml)
2+
|TItem|
3+
Generated Location: (462:16,0 [5] )
4+
|TItem|
5+
6+
Source Location: (29:1,11 [6] x:\dir\subdir\Test\TestComponent.cshtml)
7+
|TItem2|
8+
Generated Location: (596:24,0 [6] )
9+
|TItem2|
10+
11+
Source Location: (190:10,23 [3] x:\dir\subdir\Test\TestComponent.cshtml)
12+
|int|
13+
Generated Location: (1442:51,23 [3] )
14+
|int|
15+
16+
Source Location: (202:10,35 [6] x:\dir\subdir\Test\TestComponent.cshtml)
17+
|string|
18+
Generated Location: (1646:60,35 [6] )
19+
|string|
20+
21+
Source Location: (44:2,7 [118] x:\dir\subdir\Test\TestComponent.cshtml)
22+
|
23+
[Parameter]
24+
public TItem MyItem { get; set; }
25+
26+
[Parameter]
27+
public TItem2 MyItem2 { get; set; }
28+
|
29+
Generated Location: (2176:80,7 [118] )
30+
|
31+
[Parameter]
32+
public TItem MyItem { get; set; }
33+
34+
[Parameter]
35+
public TItem2 MyItem2 { get; set; }
36+
|
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
namespace Test
4+
{
5+
#line default
6+
using global::System;
7+
using global::System.Collections.Generic;
8+
using global::System.Linq;
9+
using global::System.Threading.Tasks;
10+
using global::Microsoft.AspNetCore.Components;
11+
#line default
12+
#line hidden
13+
#nullable restore
14+
public partial class TestComponent<
15+
#nullable restore
16+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
17+
TItem
18+
19+
#line default
20+
#line hidden
21+
#nullable disable
22+
> : global::Microsoft.AspNetCore.Components.ComponentBase
23+
#nullable disable
24+
{
25+
#pragma warning disable 219
26+
private void __RazorDirectiveTokenHelpers__() {
27+
((global::System.Action)(() => {
28+
}
29+
))();
30+
}
31+
#pragma warning restore 219
32+
#pragma warning disable 0414
33+
private static object __o = null;
34+
#pragma warning restore 0414
35+
#pragma warning disable 1998
36+
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
37+
{
38+
__o = typeof(
39+
#nullable restore
40+
#line 7 "x:\dir\subdir\Test\TestComponent.cshtml"
41+
string
42+
43+
#line default
44+
#line hidden
45+
#nullable disable
46+
);
47+
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
48+
}
49+
));
50+
#nullable restore
51+
#line 7 "x:\dir\subdir\Test\TestComponent.cshtml"
52+
__o = typeof(global::Test.TestComponent<>);
53+
54+
#line default
55+
#line hidden
56+
#nullable disable
57+
}
58+
#pragma warning restore 1998
59+
#nullable restore
60+
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
61+
62+
[Parameter]
63+
public TItem MyItem { get; set; }
64+
65+
#line default
66+
#line hidden
67+
#nullable disable
68+
}
69+
}
70+
#pragma warning restore 1591

0 commit comments

Comments
 (0)