Skip to content

Commit

Permalink
Add null suppression to component reference capture (#8320)
Browse files Browse the repository at this point in the history
* Add nullable enable option

* Add tests

* Add null suppression to component reference capture

* Update baselines
  • Loading branch information
jjonescz committed Mar 1, 2023
1 parent 25f991c commit 0591c70
Show file tree
Hide file tree
Showing 27 changed files with 503 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,7 @@ protected override void WriteReferenceCaptureInnards(CodeRenderingContext contex
var captureTypeName = node.IsComponentCapture
? TypeNameHelper.GetGloballyQualifiedNameIfNeeded(node.ComponentCaptureTypeName)
: ComponentsApi.ElementReference.FullTypeName;
var nullSuppression = !context.Options.SuppressNullabilityEnforcement ? "!" : string.Empty;
WriteCSharpCode(context, new CSharpCodeIntermediateNode
{
Source = node.Source,
Expand All @@ -1128,7 +1129,7 @@ protected override void WriteReferenceCaptureInnards(CodeRenderingContext contex
new IntermediateToken
{
Kind = TokenKind.CSharp,
Content = $" = default({captureTypeName});"
Content = $" = default({captureTypeName}){nullSuppression};"
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7628,6 +7628,58 @@ public class MyComponent : ComponentBase
CompileToAssembly(generated);
}

[Fact] // https://github.com/dotnet/razor/issues/8170
public void Component_WithRef_Nullable()
{
// Act
var generated = CompileToCSharp("""
<TestComponent @ref="myComponent" />

@code {
private TestComponent myComponent = null!;
public void Use() { System.GC.KeepAlive(myComponent); }
}
""",
nullableEnable: true);

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact] // https://github.com/dotnet/razor/issues/8170
public void Component_WithRef_Nullable_Generic()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse("""
using Microsoft.AspNetCore.Components;

namespace Test;

public class MyComponent<T> : ComponentBase
{
[Parameter] public T MyParameter { get; set; } = default!;
}
"""));

// Act
var generated = CompileToCSharp("""
<MyComponent @ref="myComponent" MyParameter="1" />

@code {
private MyComponent<int> myComponent = null!;
public void Use() { System.GC.KeepAlive(myComponent); }
}
""",
nullableEnable: true);

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}

[Fact]
public void Component_WithRef_WithChildContent()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
));
#nullable restore
#line 7 "x:\dir\subdir\Test\TestComponent.cshtml"
myComponentReference = default(global::Test.TemplatedComponent);
myComponentReference = default(global::Test.TemplatedComponent)!;

#line default
#line hidden
Expand All @@ -71,7 +71,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
#nullable disable
#nullable restore
#line 13 "x:\dir\subdir\Test\TestComponent.cshtml"
myElementReference = default(Microsoft.AspNetCore.Components.ElementReference);
myElementReference = default(Microsoft.AspNetCore.Components.ElementReference)!;

#line default
#line hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,32 @@ Source Location: (439:10,1 [38] x:\dir\subdir\Test\TestComponent.cshtml)
|if (DateTime.Now.Year > 1950)
{
|
Generated Location: (1965:64,1 [38] )
Generated Location: (1966:64,1 [38] )
|if (DateTime.Now.Year > 1950)
{
|

Source Location: (511:12,38 [18] x:\dir\subdir\Test\TestComponent.cshtml)
|myElementReference|
Generated Location: (2164:73,38 [18] )
Generated Location: (2165:73,38 [18] )
|myElementReference|

Source Location: (557:12,84 [6] x:\dir\subdir\Test\TestComponent.cshtml)
|
|
Generated Location: (2379:78,84 [6] )
Generated Location: (2381:78,84 [6] )
|
|

Source Location: (589:13,30 [10] x:\dir\subdir\Test\TestComponent.cshtml)
|myVariable|
Generated Location: (2574:83,30 [10] )
Generated Location: (2576:83,30 [10] )
|myVariable|

Source Location: (637:13,78 [3] x:\dir\subdir\Test\TestComponent.cshtml)
|
}|
Generated Location: (2947:92,78 [3] )
Generated Location: (2949:92,78 [3] )
|
}|

Expand All @@ -62,7 +62,7 @@ Source Location: (651:16,7 [245] x:\dir\subdir\Test\TestComponent.cshtml)
for (var i = 0; i < 10; i++)
{
|
Generated Location: (3129:102,7 [245] )
Generated Location: (3131:102,7 [245] )
|
ElementReference myElementReference;
TemplatedComponent myComponentReference;
Expand All @@ -76,12 +76,12 @@ Generated Location: (3129:102,7 [245] )

Source Location: (912:25,28 [1] x:\dir\subdir\Test\TestComponent.cshtml)
|i|
Generated Location: (3541:119,28 [1] )
Generated Location: (3543:119,28 [1] )
|i|

Source Location: (925:25,41 [1] x:\dir\subdir\Test\TestComponent.cshtml)
|i|
Generated Location: (3717:127,41 [1] )
Generated Location: (3719:127,41 [1] )
|i|

Source Location: (931:25,47 [166] x:\dir\subdir\Test\TestComponent.cshtml)
Expand All @@ -93,7 +93,7 @@ Source Location: (931:25,47 [166] x:\dir\subdir\Test\TestComponent.cshtml)
System.GC.KeepAlive(myVariable);
}
|
Generated Location: (3889:134,47 [166] )
Generated Location: (3891:134,47 [166] )
|
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
));
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
myInstance = default(global::Test.MyComponent);
myInstance = default(global::Test.MyComponent)!;

#line default
#line hidden
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Source Location: (84:2,7 [104] x:\dir\subdir\Test\TestComponent.cshtml)
private Test.MyComponent myInstance;
public void Foo() { System.GC.KeepAlive(myInstance); }
|
Generated Location: (1496:45,7 [104] )
Generated Location: (1497:45,7 [104] )
|
private Test.MyComponent myInstance;
public void Foo() { System.GC.KeepAlive(myInstance); }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
myComponent = default(global::Test.TestComponent)!;

#line default
#line hidden
#nullable disable
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Test.TestComponent);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"

private TestComponent myComponent = null!;
public void Use() { System.GC.KeepAlive(myComponent); }

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [36] x:\dir\subdir\Test\TestComponent.cshtml) - TestComponent
ReferenceCapture - (21:0,21 [11] x:\dir\subdir\Test\TestComponent.cshtml) - myComponent
HtmlContent - (36:0,36 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (36:0,36 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private TestComponent myComponent = null!;\n public void Use() { System.GC.KeepAlive(myComponent); }\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Source Location: (21:0,21 [11] x:\dir\subdir\Test\TestComponent.cshtml)
|myComponent|
Generated Location: (1045:27,21 [11] )
|myComponent|

Source Location: (47:2,7 [111] x:\dir\subdir\Test\TestComponent.cshtml)
|
private TestComponent myComponent = null!;
public void Use() { System.GC.KeepAlive(myComponent); }
|
Generated Location: (1437:43,7 [111] )
|
private TestComponent myComponent = null!;
public void Use() { System.GC.KeepAlive(myComponent); }
|

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line hidden
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
var __typeInference_CreateMyComponent_0 = global::__Blazor.Test.TestComponent.TypeInference.CreateMyComponent_0(__builder, -1, -1,
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
1

#line default
#line hidden
#nullable disable
, -1, (__value) => {
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
myComponent = __value;
#line default
#line hidden
#nullable disable
}
);
__o = __typeInference_CreateMyComponent_0.
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
MyParameter

#line default
#line hidden
#nullable disable
;
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Test.MyComponent<>);

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"

private MyComponent<int> myComponent = null!;
public void Use() { System.GC.KeepAlive(myComponent); }

#line default
#line hidden
#nullable disable
}
}
namespace __Blazor.Test.TestComponent
{
#line hidden
internal static class TypeInference
{
public static global::Test.MyComponent<T> CreateMyComponent_0<T>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, T __arg0, int __seq1, System.Action<global::Test.MyComponent<T>> __arg1)
{
__builder.OpenComponent<global::Test.MyComponent<T>>(seq);
__builder.AddAttribute(__seq0, "MyParameter", __arg0);
__builder.AddComponentReferenceCapture(__seq1, (__value) => { __arg1((global::Test.MyComponent<T>)__value); });
__builder.CloseComponent();
return default;
}
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [12] ) - System
UsingDirective - (18:2,1 [32] ) - System.Collections.Generic
UsingDirective - (53:3,1 [17] ) - System.Linq
UsingDirective - (73:4,1 [28] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [50] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
ReferenceCapture - (19:0,19 [11] x:\dir\subdir\Test\TestComponent.cshtml) - myComponent
ComponentAttribute - (45:0,45 [1] x:\dir\subdir\Test\TestComponent.cshtml) - MyParameter - MyParameter - AttributeStructure.DoubleQuotes
LazyIntermediateToken - (45:0,45 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - 1
HtmlContent - (50:0,50 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (50:0,50 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (61:2,7 [114] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n private MyComponent<int> myComponent = null!;\n public void Use() { System.GC.KeepAlive(myComponent); }\n
NamespaceDeclaration - - __Blazor.Test.TestComponent
ClassDeclaration - - internal static - TypeInference - -
ComponentTypeInferenceMethod - - __Blazor.Test.TestComponent.TypeInference - CreateMyComponent_0

0 comments on commit 0591c70

Please sign in to comment.