Fix razor whitespace removal in methods (#23699)

This commit is contained in:
Steve Sanderson 2020-07-08 12:43:09 +01:00 committed by GitHub
parent c5ba43f011
commit 1bccebd22a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 337 additions and 24 deletions

View File

@ -45,14 +45,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
return;
}
var method = documentNode.FindPrimaryMethod();
if (method != null)
var @class = documentNode.FindPrimaryClass();
if (@class != null)
{
RemoveContiguousWhitespace(method.Children, TraversalDirection.Forwards);
RemoveContiguousWhitespace(method.Children, TraversalDirection.Backwards);
var visitor = new Visitor();
visitor.Visit(method);
visitor.Visit(@class);
}
}
@ -145,6 +142,13 @@ namespace Microsoft.AspNetCore.Razor.Language.Components
class Visitor : IntermediateNodeWalker
{
public override void VisitMethodDeclaration(MethodDeclarationIntermediateNode node)
{
RemoveContiguousWhitespace(node.Children, TraversalDirection.Forwards);
RemoveContiguousWhitespace(node.Children, TraversalDirection.Backwards);
VisitDefault(node);
}
public override void VisitMarkupElement(MarkupElementIntermediateNode node)
{
RemoveContiguousWhitespace(node.Children, TraversalDirection.Forwards);

View File

@ -4685,6 +4685,35 @@ namespace Test
CompileToAssembly(generated);
}
[Fact]
public void WhiteSpace_InMarkupInFunctionsBlock()
{
// Arrange
// Act
var generated = CompileToCSharp(@"
@using Microsoft.AspNetCore.Components.Rendering
@code {
void MyMethod(RenderTreeBuilder __builder)
{
<ul>
@for (var i = 0; i < 100; i++)
{
<li>
@i
</li>
}
</ul>
}
}
");
// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated);
}
[Fact]
public void WhiteSpace_WithPreserveWhitespace()
{

View File

@ -0,0 +1,76 @@
// <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;
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
using Microsoft.AspNetCore.Components.Rendering;
#line default
#line hidden
#nullable disable
public partial 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.Rendering.RenderTreeBuilder __builder)
{
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
void MyMethod(RenderTreeBuilder __builder)
{
#line default
#line hidden
#nullable disable
#nullable restore
#line 6 "x:\dir\subdir\Test\TestComponent.cshtml"
for (var i = 0; i < 100; i++)
{
#line default
#line hidden
#nullable disable
#nullable restore
#line 9 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = i;
#line default
#line hidden
#nullable disable
#nullable restore
#line 10 "x:\dir\subdir\Test\TestComponent.cshtml"
}
#line default
#line hidden
#nullable disable
#nullable restore
#line 12 "x:\dir\subdir\Test\TestComponent.cshtml"
}
#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,41 @@
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
UsingDirective - (1:0,1 [47] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components.Rendering
ClassDeclaration - - public partial - 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
HtmlContent - (48:0,48 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (48:0,48 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
HtmlContent - (294:13,1 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (294:13,1 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (57:1,7 [65] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (57:1,7 [65] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n void MyMethod(RenderTreeBuilder __builder)\n {\n
MarkupElement - (122:4,8 [162] x:\dir\subdir\Test\TestComponent.cshtml) - ul
HtmlContent - (126:4,12 [14] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (126:4,12 [14] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (141:5,13 [62] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (141:5,13 [62] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - for (var i = 0; i < 100; i++)\n {\n
MarkupElement - (203:7,16 [51] x:\dir\subdir\Test\TestComponent.cshtml) - li
HtmlContent - (207:7,20 [22] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (207:7,20 [22] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpExpression - (230:8,21 [1] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (230:8,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - i
HtmlContent - (231:8,22 [18] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (231:8,22 [18] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (254:9,21 [15] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (254:9,21 [15] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n }
HtmlContent - (269:10,13 [10] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (269:10,13 [10] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (284:11,13 [9] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (284:11,13 [9] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n }\n

View File

@ -0,0 +1,46 @@
Source Location: (1:0,1 [47] x:\dir\subdir\Test\TestComponent.cshtml)
|using Microsoft.AspNetCore.Components.Rendering|
Generated Location: (320:12,0 [47] )
|using Microsoft.AspNetCore.Components.Rendering|
Source Location: (57:1,7 [65] x:\dir\subdir\Test\TestComponent.cshtml)
|
void MyMethod(RenderTreeBuilder __builder)
{
|
Generated Location: (1078:33,7 [65] )
|
void MyMethod(RenderTreeBuilder __builder)
{
|
Source Location: (141:5,13 [62] x:\dir\subdir\Test\TestComponent.cshtml)
|for (var i = 0; i < 100; i++)
{
|
Generated Location: (1278:43,13 [62] )
|for (var i = 0; i < 100; i++)
{
|
Source Location: (230:8,21 [1] x:\dir\subdir\Test\TestComponent.cshtml)
|i|
Generated Location: (1483:52,21 [1] )
|i|
Source Location: (254:9,21 [15] x:\dir\subdir\Test\TestComponent.cshtml)
|
}|
Generated Location: (1629:59,21 [15] )
|
}|
Source Location: (284:11,13 [9] x:\dir\subdir\Test\TestComponent.cshtml)
|
}
|
Generated Location: (1780:67,13 [9] )
|
}
|

View File

@ -38,10 +38,8 @@ using Microsoft.AspNetCore.Components.Rendering;
#line default
#line hidden
#nullable disable
__builder.AddContent(0, " ");
__builder.OpenComponent<Test.MyComponent>(1);
__builder.OpenComponent<Test.MyComponent>(0);
__builder.CloseComponent();
__builder.AddMarkupContent(2, "\r\n");
#nullable restore
#line 9 "x:\dir\subdir\Test\TestComponent.cshtml"
}

View File

@ -12,10 +12,6 @@ Document -
LazyIntermediateToken - (55:2,2 [34] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - RenderChildComponent(__builder);
CSharpCode - (101:4,7 [69] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (101:4,7 [69] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n void RenderChildComponent(RenderTreeBuilder __builder)\n {\n
HtmlContent - (170:7,0 [8] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (170:7,0 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html -
Component - (178:7,8 [15] x:\dir\subdir\Test\TestComponent.cshtml) - MyComponent
HtmlContent - (193:7,23 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (193:7,23 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (195:8,0 [7] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (195:8,0 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - }\n

View File

@ -17,7 +17,7 @@ Generated Location: (968:33,7 [69] )
Source Location: (195:8,0 [7] x:\dir\subdir\Test\TestComponent.cshtml)
| }
|
Generated Location: (1343:46,0 [7] )
Generated Location: (1249:44,0 [7] )
| }
|

View File

@ -34,10 +34,9 @@ using Microsoft.AspNetCore.Components.Rendering;
#line default
#line hidden
#nullable disable
__builder.AddContent(0, " ");
__builder.OpenElement(1, "p");
__builder.AddContent(2, "Output: ");
__builder.AddContent(3,
__builder.OpenElement(0, "p");
__builder.AddContent(1, "Output: ");
__builder.AddContent(2,
#nullable restore
#line 9 "x:\dir\subdir\Test\TestComponent.cshtml"
output
@ -47,7 +46,6 @@ using Microsoft.AspNetCore.Components.Rendering;
#nullable disable
);
__builder.CloseElement();
__builder.AddMarkupContent(4, "\r\n");
#nullable restore
#line 10 "x:\dir\subdir\Test\TestComponent.cshtml"
}

View File

@ -10,14 +10,10 @@ Document -
MethodDeclaration - - protected override - void - BuildRenderTree
CSharpCode - (60:2,7 [213] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (60:2,7 [213] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n void RenderChildComponent(RenderTreeBuilder __builder)\n {\n var output = string.Empty;\n if (__builder == null) output = "Builder is null!";\n else output = "Builder is not null!";\n
HtmlContent - (273:8,0 [8] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (273:8,0 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html -
MarkupElement - (281:8,8 [22] x:\dir\subdir\Test\TestComponent.cshtml) - p
HtmlContent - (284:8,11 [8] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (284:8,11 [8] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Output:
CSharpExpression - (293:8,20 [6] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (293:8,20 [6] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - output
HtmlContent - (303:8,30 [2] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (303:8,30 [2] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n
CSharpCode - (305:9,0 [7] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (305:9,0 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - }\n

View File

@ -18,7 +18,7 @@ Generated Location: (810:26,7 [213] )
Source Location: (305:9,0 [7] x:\dir\subdir\Test\TestComponent.cshtml)
| }
|
Generated Location: (1553:52,0 [7] )
Generated Location: (1459:50,0 [7] )
| }
|

View File

@ -0,0 +1,71 @@
// <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;
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
using Microsoft.AspNetCore.Components.Rendering;
#line default
#line hidden
#nullable disable
public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBase
{
#pragma warning disable 1998
protected override void BuildRenderTree(Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
}
#pragma warning restore 1998
#nullable restore
#line 2 "x:\dir\subdir\Test\TestComponent.cshtml"
void MyMethod(RenderTreeBuilder __builder)
{
#line default
#line hidden
#nullable disable
__builder.OpenElement(0, "ul");
#nullable restore
#line 6 "x:\dir\subdir\Test\TestComponent.cshtml"
for (var i = 0; i < 100; i++)
{
#line default
#line hidden
#nullable disable
__builder.OpenElement(1, "li");
__builder.AddContent(2,
#nullable restore
#line 9 "x:\dir\subdir\Test\TestComponent.cshtml"
i
#line default
#line hidden
#nullable disable
);
__builder.CloseElement();
#nullable restore
#line 11 "x:\dir\subdir\Test\TestComponent.cshtml"
}
#line default
#line hidden
#nullable disable
__builder.CloseElement();
#nullable restore
#line 13 "x:\dir\subdir\Test\TestComponent.cshtml"
}
#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591

View File

@ -0,0 +1,24 @@
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
UsingDirective - (1:0,1 [49] x:\dir\subdir\Test\TestComponent.cshtml) - Microsoft.AspNetCore.Components.Rendering
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
CSharpCode - (57:1,7 [57] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (57:1,7 [57] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n void MyMethod(RenderTreeBuilder __builder)\n {\n
MarkupElement - (122:4,8 [162] x:\dir\subdir\Test\TestComponent.cshtml) - ul
CSharpCode - (128:5,0 [12] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (128:5,0 [12] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp -
CSharpCode - (141:5,13 [46] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (141:5,13 [46] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - for (var i = 0; i < 100; i++)\n {\n
MarkupElement - (203:7,16 [51] x:\dir\subdir\Test\TestComponent.cshtml) - li
CSharpExpression - (230:8,21 [1] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (230:8,21 [1] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - i
CSharpCode - (256:10,0 [15] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (256:10,0 [15] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - }\n
CSharpCode - (286:12,0 [7] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (286:12,0 [7] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - }\n

View File

@ -0,0 +1,34 @@
Source Location: (57:1,7 [57] x:\dir\subdir\Test\TestComponent.cshtml)
|
void MyMethod(RenderTreeBuilder __builder)
{
|
Generated Location: (810:26,7 [57] )
|
void MyMethod(RenderTreeBuilder __builder)
{
|
Source Location: (141:5,13 [46] x:\dir\subdir\Test\TestComponent.cshtml)
|for (var i = 0; i < 100; i++)
{
|
Generated Location: (1041:36,13 [46] )
|for (var i = 0; i < 100; i++)
{
|
Source Location: (256:10,0 [15] x:\dir\subdir\Test\TestComponent.cshtml)
| }
|
Generated Location: (1474:55,0 [15] )
| }
|
Source Location: (286:12,0 [7] x:\dir\subdir\Test\TestComponent.cshtml)
| }
|
Generated Location: (1645:63,0 [7] )
| }
|