[Fixes #878] Removed marker nodes from IR

- Also fixed SourceMappings content length in Markup spans
This commit is contained in:
Ajay Bhargav Baaskaran 2016-12-21 15:58:56 -08:00
parent c6150ba287
commit af499794c5
17 changed files with 82 additions and 62 deletions

View File

@ -126,7 +126,26 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public override void VisitEndTemplateBlock(TemplateBlockChunkGenerator chunkGenerator, Block block)
{
Builder.Pop();
var templateNode = Builder.Pop();
if (templateNode.Children.Count > 0)
{
var sourceRangeStart = templateNode
.Children
.FirstOrDefault(child => child.SourceRange != null)
?.SourceRange;
if (sourceRangeStart != null)
{
var contentLength = templateNode.Children.Sum(child => child.SourceRange?.ContentLength ?? 0);
templateNode.SourceRange = new MappingLocation(
sourceRangeStart.AbsoluteIndex,
sourceRangeStart.LineIndex,
sourceRangeStart.CharacterIndex,
contentLength,
sourceRangeStart.FilePath ?? _codeDocument.Source.Filename);
}
}
}
// CSharp expressions are broken up into blocks and spans because Razor allows Razor comments
@ -150,19 +169,35 @@ namespace Microsoft.AspNetCore.Razor.Evolution
.Children
.FirstOrDefault(child => child.SourceRange != null)
?.SourceRange;
var contentLength = expressionNode.Children.Sum(child => child.SourceRange?.ContentLength ?? 0);
expressionNode.SourceRange = new MappingLocation(
sourceRangeStart.AbsoluteIndex,
sourceRangeStart.LineIndex,
sourceRangeStart.CharacterIndex,
contentLength,
sourceRangeStart.FilePath ?? _codeDocument.Source.Filename);
if (sourceRangeStart != null)
{
var contentLength = expressionNode.Children.Sum(child => child.SourceRange?.ContentLength ?? 0);
expressionNode.SourceRange = new MappingLocation(
sourceRangeStart.AbsoluteIndex,
sourceRangeStart.LineIndex,
sourceRangeStart.CharacterIndex,
contentLength,
sourceRangeStart.FilePath ?? _codeDocument.Source.Filename);
}
}
}
public override void VisitExpressionSpan(ExpressionChunkGenerator chunkGenerator, Span span)
{
if (span.Symbols.Count == 1)
{
var symbol = span.Symbols[0] as CSharpSymbol;
if (symbol != null &&
symbol.Type == CSharpSymbolType.Unknown &&
symbol.Content.Length == 0)
{
// We don't want to create IR nodes for marker symbols.
return;
}
}
Builder.Add(new CSharpTokenIRNode()
{
Content = span.Content,
@ -181,11 +216,29 @@ namespace Microsoft.AspNetCore.Razor.Evolution
public override void VisitMarkupSpan(MarkupChunkGenerator chunkGenerator, Span span)
{
if (span.Symbols.Count == 1)
{
var symbol = span.Symbols[0] as HtmlSymbol;
if (symbol != null &&
symbol.Type == HtmlSymbolType.Unknown &&
symbol.Content.Length == 0)
{
// We don't want to create IR nodes for marker symbols.
return;
}
}
var currentChildren = Builder.Current.Children;
if (currentChildren.Count > 0 && currentChildren[currentChildren.Count - 1] is HtmlContentIRNode)
{
var existingHtmlContent = (HtmlContentIRNode)currentChildren[currentChildren.Count - 1];
existingHtmlContent.Content = string.Concat(existingHtmlContent.Content, span.Content);
existingHtmlContent.SourceRange = new MappingLocation(
existingHtmlContent.SourceRange.AbsoluteIndex,
existingHtmlContent.SourceRange.LineIndex,
existingHtmlContent.SourceRange.CharacterIndex,
existingHtmlContent.SourceRange.ContentLength + span.Content.Length,
existingHtmlContent.SourceRange.FilePath);
}
else
{

View File

@ -316,12 +316,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution
if (node.SourceRange != null)
{
linePragmaScope = new LinePragmaWriter(Context.Writer, node.SourceRange);
var padding = BuildOffsetPadding(Context.RenderingConventions.StartWriteMethod.Length, node.SourceRange);
Context.Writer.Write(padding);
}
var padding = BuildOffsetPadding(Context.RenderingConventions.StartWriteMethod.Length, node.SourceRange);
Context.Writer
.Write(padding)
.Write(Context.RenderingConventions.StartWriteMethod);
Context.Writer.Write(Context.RenderingConventions.StartWriteMethod);
VisitDefault(node);

View File

@ -95,9 +95,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
node => Assert.IsType<RazorMethodDeclarationIRNode>(node),
node => CSharpStatement(" var value = true; ", node));
var method = (RazorMethodDeclarationIRNode)@class.Children[0];
Children(method,
node => Html(string.Empty, node),
node => Html(string.Empty, node));
Assert.Empty(method.Children);
}
[Fact]
@ -129,11 +127,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var @class = @namespace.Children[2];
var method = SingleChild<RazorMethodDeclarationIRNode>(@class);
Children(method,
node => Html(string.Empty, node),
node => CSharpStatement("DefineSection(\"Header\", async () => {", node),
node => Html(" <p>Hello World</p> ", node),
node => CSharpStatement("});", node),
node => Html(string.Empty, node));
node => CSharpStatement("});", node));
}
[Fact]
@ -166,11 +162,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution
var @class = @namespace.Children[2];
var method = SingleChild<RazorMethodDeclarationIRNode>(@class);
Children(method,
node => Html(string.Empty, node),
node => CSharpStatement("DefineSection(\"Header\", async (__razor_section_writer) => {", node),
node => Html(" <p>Hello World</p> ", node),
node => CSharpStatement("});", node),
node => Html(string.Empty, node));
node => CSharpStatement("});", node));
}
private static DocumentIRNode Lower(RazorCodeDocument codeDocument)

View File

@ -33,9 +33,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
n => Assert.IsType<ClassDeclarationIRNode>(n));
var @class = @namespace.Children[2];
var method = SingleChild<RazorMethodDeclarationIRNode>(@class);
var html = SingleChild<HtmlContentIRNode>(method);
Assert.Equal(string.Empty, html.Content);
Assert.Empty(method.Children);
}
[Fact]

View File

@ -5,6 +5,4 @@ Document -
UsingStatement - - System.Threading.Tasks
ClassDeclaration - - - - -
RazorMethodDeclaration - - - - -
HtmlContent - (0:0,0 [0] CustomDirective.cshtml) -
Directive - - test_directive
HtmlContent - (15:0,15 [0] CustomDirective.cshtml) -

View File

@ -5,4 +5,3 @@ Document -
UsingStatement - - System.Threading.Tasks
ClassDeclaration - - - - -
RazorMethodDeclaration - - - - -
HtmlContent - (0:0,0 [0] Empty.cshtml) -

View File

@ -5,9 +5,9 @@ Document -
UsingStatement - - System.Threading.Tasks
ClassDeclaration - - - - -
RazorMethodDeclaration - - - - -
HtmlContent - (0:0,0 [6] HtmlWithConditionalAttribute.cshtml) - <html>\n<body>\n <span
HtmlContent - (0:0,0 [25] HtmlWithConditionalAttribute.cshtml) - <html>\n<body>\n <span
HtmlAttribute - (25:2,9 [13] HtmlWithConditionalAttribute.cshtml) - val=" - "
CSharpAttributeValue - (31:2,15 [6] HtmlWithConditionalAttribute.cshtml) -
CSharpExpression - (32:2,16 [5] HtmlWithConditionalAttribute.cshtml)
CSharpToken - (32:2,16 [5] HtmlWithConditionalAttribute.cshtml) - Hello
HtmlContent - (38:2,22 [3] HtmlWithConditionalAttribute.cshtml) - />\n</body>\n</html>"
HtmlContent - (38:2,22 [22] HtmlWithConditionalAttribute.cshtml) - />\n</body>\n</html>"

View File

@ -5,7 +5,7 @@ Document -
UsingStatement - - System.Threading.Tasks
ClassDeclaration - - - - -
RazorMethodDeclaration - - - - -
HtmlContent - (0:0,0 [6] HtmlWithDataDashAttribute.cshtml) - <html>\n<body>\n <span data-val="
HtmlContent - (0:0,0 [36] HtmlWithDataDashAttribute.cshtml) - <html>\n<body>\n <span data-val="
CSharpExpression - (37:2,21 [5] HtmlWithDataDashAttribute.cshtml)
CSharpToken - (37:2,21 [5] HtmlWithDataDashAttribute.cshtml) - Hello
HtmlContent - (42:2,26 [1] HtmlWithDataDashAttribute.cshtml) - " />\n</body>\n</html>"
HtmlContent - (42:2,26 [23] HtmlWithDataDashAttribute.cshtml) - " />\n</body>\n</html>"

View File

@ -10,11 +10,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests.TestFiles
public async System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("This is markup\r\n\r\n");
#line 3 "TestFiles/IntegrationTests/RuntimeCodeGenerationIntegrationTest/EmptyExplicitExpression.cshtml"
Write();
#line default
#line hidden
Write();
}
#pragma warning restore 1998
}

View File

@ -10,11 +10,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests.TestFiles
public async System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("This is markup\r\n\r\n");
#line 3 "TestFiles/IntegrationTests/RuntimeCodeGenerationIntegrationTest/EmptyImplicitExpression.cshtml"
Write();
#line default
#line hidden
Write();
WriteLiteral("!");
}
#pragma warning restore 1998

View File

@ -9,11 +9,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests.TestFiles
#pragma warning disable 1998
public async System.Threading.Tasks.Task ExecuteAsync()
{
#line 2 "TestFiles/IntegrationTests/RuntimeCodeGenerationIntegrationTest/EmptyImplicitExpressionInCode.cshtml"
Write();
#line default
#line hidden
Write();
}
#pragma warning restore 1998
}

View File

@ -10,11 +10,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests.TestFiles
public async System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("This is markup\r\n\r\n");
#line 3 "TestFiles/IntegrationTests/RuntimeCodeGenerationIntegrationTest/ExplicitExpressionAtEOF.cshtml"
Write();
#line default
#line hidden
Write();
}
#pragma warning restore 1998
}

View File

@ -11,7 +11,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests.TestFiles
{
WriteLiteral("<div>");
#line 1 "TestFiles/IntegrationTests/RuntimeCodeGenerationIntegrationTest/ExplicitExpressionWithMarkup.cshtml"
Write(item => new HelperResult(async(__razor_template_writer) => {
Write(item => new HelperResult(async(__razor_template_writer) => {
WriteLiteralTo(__razor_template_writer, "</div>");
}
));

View File

@ -10,11 +10,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests.TestFiles
public async System.Threading.Tasks.Task ExecuteAsync()
{
WriteLiteral("This is markup\r\n\r\n");
#line 3 "TestFiles/IntegrationTests/RuntimeCodeGenerationIntegrationTest/ImplicitExpressionAtEOF.cshtml"
Write();
#line default
#line hidden
Write();
}
#pragma warning restore 1998
}

View File

@ -6,7 +6,6 @@ Document -
ClassDeclaration - - - - -
DeclareTagHelperFields - - PTagHelper - FormTagHelper - InputTagHelper
RazorMethodDeclaration - - - - -
HtmlContent - (0:0,0 [0] NestedTagHelpers.cshtml) -
TagHelper -
InitializeTagHelperStructure - - p - TagMode.StartTagAndEndTag
HtmlContent - (43:1,12 [4] NestedTagHelpers.cshtml) - Hola

View File

@ -6,7 +6,7 @@ Document -
ClassDeclaration - - - - -
DeclareTagHelperFields - - InputTagHelper
RazorMethodDeclaration - - - - -
HtmlContent - (0:0,0 [0] SimpleTagHelpers.cshtml) - <p>Hola</p>\n<form>\n
HtmlContent - (31:1,0 [25] SimpleTagHelpers.cshtml) - <p>Hola</p>\n<form>\n
TagHelper -
InitializeTagHelperStructure - - input - TagMode.SelfClosing
CreateTagHelper - - InputTagHelper
@ -15,4 +15,4 @@ Document -
AddTagHelperHtmlAttribute - - type - HtmlAttributeValueStyle.SingleQuotes
HtmlContent - (83:3,31 [4] SimpleTagHelpers.cshtml) - text
ExecuteTagHelpers -
HtmlContent - (91:3,39 [2] SimpleTagHelpers.cshtml) - \n</form>
HtmlContent - (91:3,39 [9] SimpleTagHelpers.cshtml) - \n</form>

View File

@ -6,7 +6,7 @@ Document -
ClassDeclaration - - - - -
DeclareTagHelperFields - - InputTagHelper
RazorMethodDeclaration - - - - -
HtmlContent - (0:0,0 [0] TagHelpersWithBoundAttributes.cshtml) - <form>\n
HtmlContent - (31:1,0 [12] TagHelpersWithBoundAttributes.cshtml) - <form>\n
TagHelper -
InitializeTagHelperStructure - - input - TagMode.SelfClosing
CreateTagHelper - - InputTagHelper
@ -16,4 +16,4 @@ Document -
AddTagHelperHtmlAttribute - - type - HtmlAttributeValueStyle.SingleQuotes
HtmlContent - (69:2,30 [4] TagHelpersWithBoundAttributes.cshtml) - text
ExecuteTagHelpers -
HtmlContent - (77:2,38 [2] TagHelpersWithBoundAttributes.cshtml) - \n</form>
HtmlContent - (77:2,38 [9] TagHelpersWithBoundAttributes.cshtml) - \n</form>