Add codegen for default format

\n\nCommit migrated from e5be8d4673
This commit is contained in:
Ryan Nowak 2019-06-30 15:14:17 -07:00 committed by Ryan Nowak
parent 3c84c57c61
commit 66cb243807
14 changed files with 407 additions and 2 deletions

View File

@ -290,13 +290,22 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
return new[] { node };
}
// Look for a matching format node. If we find one then we need to pass the format into the
// Look for a format. If we find one then we need to pass the format into the
// two nodes we generate.
IntermediateToken format = null;
if (bindEntry.BindFormatNode != null)
{
format = GetAttributeContent(bindEntry.BindFormatNode);
}
else if (node.TagHelper?.GetFormat() != null)
{
// We may have a default format if one is associated with the field type.
format = new IntermediateToken()
{
Kind = TokenKind.CSharp,
Content = "\"" + node.TagHelper.GetFormat() + "\"",
};
}
if (TryGetFormatNode(
parent,

View File

@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Razor.Language.IntegrationTests
internal override bool UseTwoPhaseCompilation => true;
protected ComponentCodeGenerationTestBase()
: base(generateBaselines: null)
: base(generateBaselines: false)
{
}
@ -1162,6 +1162,64 @@ namespace Test
CompileToAssembly(generated);
}
[Fact]
public void BuiltIn_BindToInputWithDefaultFormat()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse(@"
using System;
using Microsoft.AspNetCore.Components;
namespace Test
{
[BindInputElement(""custom"", null, ""value"", ""onchange"", isInvariantCulture: false, format: ""MM/dd"")]
public static class BindAttributes
{
}
}"));
// Act
var generated = CompileToCSharp(@"
<input type=""custom"" @bind=""@CurrentDate"" />
@code {
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
}");
// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}
[Fact]
public void BuiltIn_BindToInputWithDefaultFormat_Override()
{
// Arrange
AdditionalSyntaxTrees.Add(Parse(@"
using System;
using Microsoft.AspNetCore.Components;
namespace Test
{
[BindInputElement(""custom"", null, ""value"", ""onchange"", isInvariantCulture: false, format: ""MM/dd"")]
public static class BindAttributes
{
}
}"));
// Act
var generated = CompileToCSharp(@"
<input type=""custom"" @bind=""@CurrentDate"" @bind:format=""MM/dd/yyyy""/>
@code {
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
}");
// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}
#endregion
#region Child Content

View File

@ -0,0 +1,45 @@
// <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 class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder)
{
__o = Microsoft.AspNetCore.Components.BindMethods.GetValue(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
CurrentDate
#line default
#line hidden
#nullable disable
, "MM/dd");
__o = Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd");
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,34 @@
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 - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [44] x:\dir\subdir\Test\TestComponent.cshtml) - input
HtmlAttribute - - type=" - "
HtmlAttributeValue - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) - Html - custom
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - value=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.BindMethods.GetValue(
IntermediateToken - (29:0,29 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - CurrentDate
IntermediateToken - - CSharp - ,
IntermediateToken - - CSharp - "MM/dd"
IntermediateToken - - CSharp - )
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - onchange=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd")
HtmlContent - (44:0,44 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (44:0,44 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (53:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (53:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);\n

View File

@ -0,0 +1,14 @@
Source Location: (29:0,29 [11] x:\dir\subdir\Test\TestComponent.cshtml)
|CurrentDate|
Generated Location: (945:25,29 [11] )
|CurrentDate|
Source Location: (53:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|
Generated Location: (1308:36,7 [77] )
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|

View File

@ -0,0 +1,45 @@
// <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 class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static System.Object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder)
{
__o = Microsoft.AspNetCore.Components.BindMethods.GetValue(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
CurrentDate
#line default
#line hidden
#nullable disable
, "MM/dd/yyyy");
__o = Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd/yyyy");
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,34 @@
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 - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static System.Object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [69] x:\dir\subdir\Test\TestComponent.cshtml) - input
HtmlAttribute - - type=" - "
HtmlAttributeValue - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) - Html - custom
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - value=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.BindMethods.GetValue(
IntermediateToken - (29:0,29 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - CurrentDate
IntermediateToken - - CSharp - ,
IntermediateToken - - CSharp - "MM/dd/yyyy"
IntermediateToken - - CSharp - )
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - onchange=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd/yyyy")
HtmlContent - (69:0,69 [2] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (69:0,69 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (78:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (78:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);\n

View File

@ -0,0 +1,14 @@
Source Location: (29:0,29 [11] x:\dir\subdir\Test\TestComponent.cshtml)
|CurrentDate|
Generated Location: (945:25,29 [11] )
|CurrentDate|
Source Location: (78:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|
Generated Location: (1318:36,7 [77] )
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|

View File

@ -0,0 +1,42 @@
// <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 class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder)
{
builder.OpenElement(0, "input");
builder.AddAttribute(1, "type", "custom");
builder.AddAttribute(2, "value", Microsoft.AspNetCore.Components.BindMethods.GetValue(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
CurrentDate
#line default
#line hidden
#nullable disable
, "MM/dd"));
builder.AddAttribute(3, "onchange", Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd"));
builder.SetUpdatesAttributeName("value");
builder.CloseElement();
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,25 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [14] ) - System
UsingDirective - (18:2,1 [34] ) - System.Collections.Generic
UsingDirective - (53:3,1 [19] ) - System.Linq
UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [44] x:\dir\subdir\Test\TestComponent.cshtml) - input
HtmlAttribute - - type=" - "
HtmlAttributeValue - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) - Html - custom
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - value=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.BindMethods.GetValue(
IntermediateToken - (29:0,29 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - CurrentDate
IntermediateToken - - CSharp - ,
IntermediateToken - - CSharp - "MM/dd"
IntermediateToken - - CSharp - )
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - onchange=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd")
CSharpCode - (53:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (53:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);\n

View File

@ -0,0 +1,9 @@
Source Location: (53:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|
Generated Location: (1293:33,7 [77] )
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|

View File

@ -0,0 +1,42 @@
// <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 class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.RenderTree.RenderTreeBuilder builder)
{
builder.OpenElement(0, "input");
builder.AddAttribute(1, "type", "custom");
builder.AddAttribute(2, "value", Microsoft.AspNetCore.Components.BindMethods.GetValue(
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
CurrentDate
#line default
#line hidden
#nullable disable
, "MM/dd/yyyy"));
builder.AddAttribute(3, "onchange", Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd/yyyy"));
builder.SetUpdatesAttributeName("value");
builder.CloseElement();
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,25 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [14] ) - System
UsingDirective - (18:2,1 [34] ) - System.Collections.Generic
UsingDirective - (53:3,1 [19] ) - System.Linq
UsingDirective - (73:4,1 [30] ) - System.Threading.Tasks
UsingDirective - (104:5,1 [39] ) - Microsoft.AspNetCore.Components
ClassDeclaration - - public - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
MarkupElement - (0:0,0 [69] x:\dir\subdir\Test\TestComponent.cshtml) - input
HtmlAttribute - - type=" - "
HtmlAttributeValue - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) -
IntermediateToken - (13:0,13 [6] x:\dir\subdir\Test\TestComponent.cshtml) - Html - custom
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - value=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.BindMethods.GetValue(
IntermediateToken - (29:0,29 [11] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - CurrentDate
IntermediateToken - - CSharp - ,
IntermediateToken - - CSharp - "MM/dd/yyyy"
IntermediateToken - - CSharp - )
HtmlAttribute - (28:0,28 [12] x:\dir\subdir\Test\TestComponent.cshtml) - onchange=" - "
CSharpExpressionAttributeValue - -
IntermediateToken - - CSharp - Microsoft.AspNetCore.Components.EventCallback.Factory.CreateBinder(this, __value => CurrentDate = __value, CurrentDate, "MM/dd/yyyy")
CSharpCode - (78:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
IntermediateToken - (78:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);\n

View File

@ -0,0 +1,9 @@
Source Location: (78:1,7 [77] x:\dir\subdir\Test\TestComponent.cshtml)
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|
Generated Location: (1303:33,7 [77] )
|
public DateTime CurrentDate { get; set; } = new DateTime(2018, 1, 1);
|