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

Avoid using AddComponentParameter in design time #9265

Merged
merged 4 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -27,6 +27,9 @@ public ComponentDesignTimeNodeWriter(RazorLanguageVersion version) : base(versio
{
}

// Avoid using `AddComponentParameter` in design time where we currently don't detect its availability.
protected override bool CanUseAddComponentParameter(CodeRenderingContext context) => false;

public override void WriteMarkupBlock(CodeRenderingContext context, MarkupBlockIntermediateNode node)
{
if (context == null)
Expand Down Expand Up @@ -377,8 +380,8 @@ public override void WriteComponent(CodeRenderingContext context, ComponentInter
// Writes something like:
//
// __builder.OpenComponent<MyComponent>(0);
// __builder.AddComponentParameter(1, "Foo", ...);
// __builder.AddComponentParameter(2, "ChildContent", ...);
// __builder.AddAttribute(1, "Foo", ...);
// __builder.AddAttribute(2, "ChildContent", ...);
// __builder.SetKey(someValue);
// __builder.AddElementCapture(3, (__value) => _field = __value);
// __builder.CloseComponent();
Expand Down Expand Up @@ -993,9 +996,9 @@ public override void WriteComponentChildContent(CodeRenderingContext context, Co

// Writes something like:
//
// __builder.AddComponentParameter(1, "ChildContent", (RenderFragment)((__builder73) => { ... }));
// __builder.AddAttribute(1, "ChildContent", (RenderFragment)((__builder73) => { ... }));
// OR
// __builder.AddComponentParameter(1, "ChildContent", (RenderFragment<Person>)((person) => (__builder73) => { ... }));
// __builder.AddAttribute(1, "ChildContent", (RenderFragment<Person>)((person) => (__builder73) => { ... }));
BeginWriteAttribute(context, node.AttributeName);
context.CodeWriter.WriteParameterSeparator();
context.CodeWriter.Write("(");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected ComponentNodeWriter(RazorLanguageVersion version)
_version = version;
}

protected bool CanUseAddComponentParameter(CodeRenderingContext context)
protected virtual bool CanUseAddComponentParameter(CodeRenderingContext context)
{
return !context.Options.SuppressAddComponentParameter && _version.CompareTo(RazorLanguageVersion.Version_8_0) >= 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1642,6 +1642,35 @@ public class MyComponent<T> : ComponentBase
CompileToAssembly(generated);
}

[Fact, WorkItem("https://dev.azure.com/devdiv/DevDiv/_workitems/edit/1869483")]
public void AddComponentParameter()
{
var generated = CompileToCSharp("""
@typeparam T

<TestComponent Param="42" />

@code {
[Parameter]
public T Param { get; set; }
}
""");

CompileToAssembly(generated);

if (DesignTime)
{
// In design-time, AddComponentParameter shouldn't be used.
Assert.Contains("AddAttribute", generated.Code);
Assert.DoesNotContain("AddComponentParameter", generated.Code);
}
else
{
Assert.DoesNotContain("AddAttribute", generated.Code);
Assert.Contains("AddComponentParameter", generated.Code);
}
}

#endregion

#region Bind
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ 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, global::Microsoft.AspNetCore.Components.EventCallback<T> __arg1, int __seq2, global::System.Linq.Expressions.Expression<global::System.Func<T>> __arg2)
{
__builder.OpenComponent<global::Test.MyComponent<T>>(seq);
__builder.AddComponentParameter(__seq0, "SomeParam", __arg0);
__builder.AddComponentParameter(__seq1, "SomeParamChanged", __arg1);
__builder.AddComponentParameter(__seq2, "SomeParamExpression", __arg2);
__builder.AddAttribute(__seq0, "SomeParam", (object)__arg0);
__builder.AddAttribute(__seq1, "SomeParamChanged", (object)__arg1);
__builder.AddAttribute(__seq2, "SomeParamExpression", (object)__arg2);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ internal static class TypeInference
public static global::Test.MyComponent<T> CreateMyComponent_0<T>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Collections.Generic.IEnumerable<T> __arg0, int __seq1, global::Microsoft.AspNetCore.Components.EventCallback<global::System.Collections.Generic.IEnumerable<T>> __arg1, int __seq2, global::System.Linq.Expressions.Expression<global::System.Func<global::System.Collections.Generic.IEnumerable<T>>> __arg2)
{
__builder.OpenComponent<global::Test.MyComponent<T>>(seq);
__builder.AddComponentParameter(__seq0, "SomeParam", __arg0);
__builder.AddComponentParameter(__seq1, "SomeParamChanged", __arg1);
__builder.AddComponentParameter(__seq2, "SomeParamExpression", __arg2);
__builder.AddAttribute(__seq0, "SomeParam", (object)__arg0);
__builder.AddAttribute(__seq1, "SomeParamChanged", (object)__arg1);
__builder.AddAttribute(__seq2, "SomeParamExpression", (object)__arg2);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ 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, global::System.Action<T> __arg1, int __seq2, global::System.Linq.Expressions.Expression<global::System.Func<T>> __arg2)
{
__builder.OpenComponent<global::Test.MyComponent<T>>(seq);
__builder.AddComponentParameter(__seq0, "SomeParam", __arg0);
__builder.AddComponentParameter(__seq1, "SomeParamChanged", __arg1);
__builder.AddComponentParameter(__seq2, "SomeParamExpression", __arg2);
__builder.AddAttribute(__seq0, "SomeParam", (object)__arg0);
__builder.AddAttribute(__seq1, "SomeParamChanged", (object)__arg1);
__builder.AddAttribute(__seq2, "SomeParamExpression", (object)__arg2);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ internal static class TypeInference
public static global::Test.MyComponent<TValue> CreateMyComponent_0<TValue>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TValue __arg0, int __seq1, global::System.Action<TValue> __arg1)
{
__builder.OpenComponent<global::Test.MyComponent<TValue>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ValueChanged", __arg1);
__builder.AddAttribute(__seq0, "Value", (object)__arg0);
__builder.AddAttribute(__seq1, "ValueChanged", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ internal static class TypeInference
public static global::Test.MyComponent<TValue> CreateMyComponent_0<TValue>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TValue __arg0, int __seq1, global::Microsoft.AspNetCore.Components.EventCallback<TValue> __arg1)
{
__builder.OpenComponent<global::Test.MyComponent<TValue>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ValueChanged", __arg1);
__builder.AddAttribute(__seq0, "Value", (object)__arg0);
__builder.AddAttribute(__seq1, "ValueChanged", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ internal static class TypeInference
public static global::Test.MyComponent<TValue> CreateMyComponent_0<TValue>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TValue __arg0, int __seq1, global::Microsoft.AspNetCore.Components.EventCallback<TValue> __arg1)
{
__builder.OpenComponent<global::Test.MyComponent<TValue>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ValueChanged", __arg1);
__builder.AddAttribute(__seq0, "Value", (object)__arg0);
__builder.AddAttribute(__seq1, "ValueChanged", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ internal static class TypeInference
public static global::Test.MyComponent<TValue> CreateMyComponent_0<TValue>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TValue __arg0, int __seq1, global::Microsoft.AspNetCore.Components.EventCallback<TValue> __arg1)
{
__builder.OpenComponent<global::Test.MyComponent<TValue>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ValueChanged", __arg1);
__builder.AddAttribute(__seq0, "Value", (object)__arg0);
__builder.AddAttribute(__seq1, "ValueChanged", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ internal static class TypeInference
public static global::Test.ParentOne<TOne> CreateParentOne_0<TOne>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TOne __arg0, int __seq1, global::Microsoft.AspNetCore.Components.RenderFragment __arg1)
{
__builder.OpenComponent<global::Test.ParentOne<TOne>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ChildContent", __arg1);
__builder.AddAttribute(__seq0, "Value", (object)__arg0);
__builder.AddAttribute(__seq1, "ChildContent", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand All @@ -117,8 +117,8 @@ public static void CreateParentOne_0_CaptureParameters<TOne>(TOne __arg0, out TO
public static global::Test.ParentTwo<TTwo> CreateParentTwo_1<TTwo>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, TTwo __arg0, int __seq1, global::Microsoft.AspNetCore.Components.RenderFragment __arg1)
{
__builder.OpenComponent<global::Test.ParentTwo<TTwo>>(seq);
__builder.AddComponentParameter(__seq0, "Value", __arg0);
__builder.AddComponentParameter(__seq1, "ChildContent", __arg1);
__builder.AddAttribute(__seq0, "Value", (object)__arg0);
__builder.AddAttribute(__seq1, "ChildContent", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ internal static class TypeInference
public static global::Test.Grid<TItem> CreateGrid_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Collections.Generic.IEnumerable<TItem> __arg0, int __seq1, global::Microsoft.AspNetCore.Components.RenderFragment __arg1)
{
__builder.OpenComponent<global::Test.Grid<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Items", __arg0);
__builder.AddComponentParameter(__seq1, "ChildContent", __arg1);
__builder.AddAttribute(__seq0, "Items", (object)__arg0);
__builder.AddAttribute(__seq1, "ChildContent", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand All @@ -94,7 +94,7 @@ public static void CreateGrid_0_CaptureParameters<TItem>(global::System.Collecti
public static global::Test.Column<TItem> CreateColumn_1<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, global::System.Collections.Generic.IEnumerable<TItem> __syntheticArg0, int __seq0, global::Microsoft.AspNetCore.Components.RenderFragment<TItem> __arg0)
{
__builder.OpenComponent<global::Test.Column<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "ChildContent", __arg0);
__builder.AddAttribute(__seq0, "ChildContent", (object)__arg0);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ internal static class TypeInference
public static global::Test.Grid<TItem> CreateGrid_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Collections.Generic.IEnumerable<TItem> __arg0, int __seq1, global::Microsoft.AspNetCore.Components.RenderFragment __arg1)
{
__builder.OpenComponent<global::Test.Grid<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Items", __arg0);
__builder.AddComponentParameter(__seq1, "ChildContent", __arg1);
__builder.AddAttribute(__seq0, "Items", (object)__arg0);
__builder.AddAttribute(__seq1, "ChildContent", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand All @@ -104,7 +104,7 @@ public static void CreateGrid_0_CaptureParameters<TItem>(global::System.Collecti
public static global::Test.Column<TItem, TOutput> CreateColumn_1<TItem, TOutput>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, global::System.Collections.Generic.IEnumerable<TItem> __syntheticArg0, int __seq0, global::System.Func<TItem, TOutput> __arg0)
{
__builder.OpenComponent<global::Test.Column<TItem, TOutput>>(seq);
__builder.AddComponentParameter(__seq0, "SomeLambda", __arg0);
__builder.AddAttribute(__seq0, "SomeLambda", (object)__arg0);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ internal static class TypeInference
where TItem : class, new()
{
__builder.OpenComponent<global::Test.Column<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Title", __arg0);
__builder.AddComponentParameter(__seq1, "FieldName", __arg1);
__builder.AddComponentParameter(__seq2, "Format", __arg2);
__builder.AddComponentParameter(__seq3, "Width", __arg3);
__builder.AddAttribute(__seq0, "Title", (object)__arg0);
__builder.AddAttribute(__seq1, "FieldName", (object)__arg1);
__builder.AddAttribute(__seq2, "Format", (object)__arg2);
__builder.AddAttribute(__seq3, "Width", (object)__arg3);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ internal static class TypeInference
where TItem : global::Models.WeatherForecast, new()
{
__builder.OpenComponent<global::Test.Column<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Title", __arg0);
__builder.AddComponentParameter(__seq1, "FieldName", __arg1);
__builder.AddComponentParameter(__seq2, "Format", __arg2);
__builder.AddComponentParameter(__seq3, "Width", __arg3);
__builder.AddAttribute(__seq0, "Title", (object)__arg0);
__builder.AddAttribute(__seq1, "FieldName", (object)__arg1);
__builder.AddAttribute(__seq2, "Format", (object)__arg2);
__builder.AddAttribute(__seq3, "Width", (object)__arg3);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ internal static class TypeInference
where TItem : global::System.Collections.Generic.IEnumerable<TItem>
{
__builder.OpenComponent<global::Test.Column<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Title", __arg0);
__builder.AddComponentParameter(__seq1, "FieldName", __arg1);
__builder.AddComponentParameter(__seq2, "Format", __arg2);
__builder.AddComponentParameter(__seq3, "Width", __arg3);
__builder.AddAttribute(__seq0, "Title", (object)__arg0);
__builder.AddAttribute(__seq1, "FieldName", (object)__arg1);
__builder.AddAttribute(__seq2, "Format", (object)__arg2);
__builder.AddAttribute(__seq3, "Width", (object)__arg3);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ internal static class TypeInference
where TItem : class
{
__builder.OpenComponent<global::Test.Column<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Title", __arg0);
__builder.AddComponentParameter(__seq1, "FieldName", __arg1);
__builder.AddComponentParameter(__seq2, "Format", __arg2);
__builder.AddComponentParameter(__seq3, "Width", __arg3);
__builder.AddAttribute(__seq0, "Title", (object)__arg0);
__builder.AddAttribute(__seq1, "FieldName", (object)__arg1);
__builder.AddAttribute(__seq2, "Format", (object)__arg2);
__builder.AddAttribute(__seq3, "Width", (object)__arg3);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ internal static class TypeInference
public static global::Test.Ancestor<TItem> CreateAncestor_0<TItem>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Collections.Generic.IEnumerable<TItem> __arg0, int __seq1, global::Microsoft.AspNetCore.Components.RenderFragment __arg1)
{
__builder.OpenComponent<global::Test.Ancestor<TItem>>(seq);
__builder.AddComponentParameter(__seq0, "Items", __arg0);
__builder.AddComponentParameter(__seq1, "ChildContent", __arg1);
__builder.AddAttribute(__seq0, "Items", (object)__arg0);
__builder.AddAttribute(__seq1, "ChildContent", (object)__arg1);
__builder.CloseComponent();
return default;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ internal static class TypeInference
public static global::Test.Parent<TKey, TValue, TOther> CreateParent_0<TKey, TValue, TOther>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, int __seq0, global::System.Collections.Generic.Dictionary<TKey, TValue> __arg0, int __seq1, TOther __arg1, int __seq2, global::Microsoft.AspNetCore.Components.RenderFragment __arg2)
{
__builder.OpenComponent<global::Test.Parent<TKey, TValue, TOther>>(seq);
__builder.AddComponentParameter(__seq0, "Data", __arg0);
__builder.AddComponentParameter(__seq1, "Other", __arg1);
__builder.AddComponentParameter(__seq2, "ChildContent", __arg2);
__builder.AddAttribute(__seq0, "Data", (object)__arg0);
__builder.AddAttribute(__seq1, "Other", (object)__arg1);
__builder.AddAttribute(__seq2, "ChildContent", (object)__arg2);
__builder.CloseComponent();
return default;
}
Expand All @@ -123,7 +123,7 @@ public static void CreateParent_0_CaptureParameters<TKey, TValue, TOther>(global
public static global::Test.Child<TOther, TValue, TKey, TChildOnly> CreateChild_1<TOther, TValue, TKey, TChildOnly>(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder, int seq, TOther __syntheticArg0, global::System.Collections.Generic.Dictionary<TKey, TValue> __syntheticArg1, global::System.Collections.Generic.Dictionary<TKey, TValue> __syntheticArg2, int __seq0, global::System.Collections.Generic.ICollection<TChildOnly> __arg0)
{
__builder.OpenComponent<global::Test.Child<TOther, TValue, TKey, TChildOnly>>(seq);
__builder.AddComponentParameter(__seq0, "ChildOnlyItems", __arg0);
__builder.AddAttribute(__seq0, "ChildOnlyItems", (object)__arg0);
__builder.CloseComponent();
return default;
}
Expand Down
Loading