Replace CSharpIRToken with RazorIRToken
Deletes CSharpIRToken to use the more general RazorIRToken class. Rather than using the visitor to visit tokens, now writing a CSharpExpresionIRNode is an 'atom', and will write its tokens itself.
This commit is contained in:
parent
2e4b1f4d18
commit
1ae0b21630
|
|
@ -14,11 +14,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
{
|
||||
}
|
||||
|
||||
public override void VisitCSharpToken(CSharpTokenIRNode node)
|
||||
{
|
||||
Context.Writer.Write(node.Content);
|
||||
}
|
||||
|
||||
public override void VisitCSharpExpression(CSharpExpressionIRNode node)
|
||||
{
|
||||
if (node.Children.Count == 0)
|
||||
|
|
@ -38,14 +33,17 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
|
||||
for (var i = 0; i < node.Children.Count; i++)
|
||||
{
|
||||
var childNode = node.Children[i];
|
||||
|
||||
if (childNode is CSharpTokenIRNode)
|
||||
var token = node.Children[i] as RazorIRToken;
|
||||
if (token != null && token.IsCSharp)
|
||||
{
|
||||
AddLineMappingFor(childNode);
|
||||
AddLineMappingFor(token);
|
||||
Context.Writer.Write(token.Content);
|
||||
}
|
||||
else
|
||||
{
|
||||
// There may be something else inside the expression like a Template or another extension node.
|
||||
Visit(node.Children[i]);
|
||||
}
|
||||
|
||||
childNode.Accept(this);
|
||||
}
|
||||
|
||||
Context.Writer.WriteLine(";");
|
||||
|
|
@ -278,9 +276,15 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
|
||||
private void AddLineMappingFor(RazorIRNode node)
|
||||
{
|
||||
var sourceLocation = node.Source.Value;
|
||||
var generatedLocation = new SourceSpan(Context.Writer.GetCurrentSourceLocation(), sourceLocation.Length);
|
||||
var lineMapping = new LineMapping(sourceLocation, generatedLocation);
|
||||
if (node.Source == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var source = node.Source.Value;
|
||||
|
||||
var generatedLocation = new SourceSpan(Context.Writer.GetCurrentSourceLocation(), source.Length);
|
||||
var lineMapping = new LineMapping(source, generatedLocation);
|
||||
|
||||
Context.LineMappings.Add(lineMapping);
|
||||
}
|
||||
|
|
@ -305,14 +309,14 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
|
||||
Context.Writer.Write(((HtmlContentIRNode)node).Content);
|
||||
}
|
||||
else if (node is CSharpTokenIRNode)
|
||||
else if (node is RazorIRToken token && token.IsCSharp)
|
||||
{
|
||||
if (node.Source != null)
|
||||
{
|
||||
AddLineMappingFor(node);
|
||||
}
|
||||
|
||||
Context.Writer.Write(((CSharpTokenIRNode)node).Content);
|
||||
Context.Writer.Write(token.Content);
|
||||
}
|
||||
else if (node is CSharpStatementIRNode)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -116,9 +116,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
|
||||
protected static void RenderExpressionInline(RazorIRNode node, CSharpRenderingContext context)
|
||||
{
|
||||
if (node is CSharpTokenIRNode)
|
||||
if (node is RazorIRToken token && token.IsCSharp)
|
||||
{
|
||||
context.Writer.Write(((CSharpTokenIRNode)node).Content);
|
||||
context.Writer.Write(token.Content);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -32,11 +32,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
}
|
||||
}
|
||||
|
||||
public override void VisitCSharpToken(CSharpTokenIRNode node)
|
||||
{
|
||||
Context.Writer.Write(node.Content);
|
||||
}
|
||||
|
||||
public override void VisitHtml(HtmlContentIRNode node)
|
||||
{
|
||||
const int MaxStringLiteralLength = 1024;
|
||||
|
|
@ -78,7 +73,18 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
|
||||
Context.Writer.Write(Context.RenderingConventions.StartWriteMethod);
|
||||
|
||||
VisitDefault(node);
|
||||
for (var i = 0; i < node.Children.Count; i++)
|
||||
{
|
||||
if (node.Children[i] is RazorIRToken token && token.IsCSharp)
|
||||
{
|
||||
Context.Writer.Write(token.Content);
|
||||
}
|
||||
else
|
||||
{
|
||||
// There may be something else inside the expression like a Template or another extension node.
|
||||
Visit(node.Children[i]);
|
||||
}
|
||||
}
|
||||
|
||||
Context.Writer.WriteEndMethodInvocation();
|
||||
|
||||
|
|
@ -651,9 +657,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution.CodeGeneration
|
|||
{
|
||||
Context.Writer.Write(((HtmlContentIRNode)node).Content);
|
||||
}
|
||||
else if (node is CSharpTokenIRNode)
|
||||
else if (node is RazorIRToken token && token.IsCSharp)
|
||||
{
|
||||
Context.Writer.Write(((CSharpTokenIRNode)node).Content);
|
||||
Context.Writer.Write(token.Content);
|
||||
}
|
||||
else if (node is CSharpStatementIRNode)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -18,7 +18,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
ThrowForMissingDependency(syntaxTree);
|
||||
|
||||
var builder = RazorIRBuilder.Document();
|
||||
|
||||
|
||||
var document = (DocumentIRNode)builder.Current;
|
||||
document.Options = syntaxTree.Options;
|
||||
|
||||
|
|
@ -58,11 +58,11 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
|
||||
var visitor = new MainSourceVisitor(document, builder, namespaces)
|
||||
{
|
||||
Filename = syntaxTree.Source.Filename,
|
||||
Filename = syntaxTree.Source.Filename,
|
||||
};
|
||||
|
||||
visitor.VisitBlock(syntaxTree.Root);
|
||||
|
||||
|
||||
codeDocument.SetIRDocument(document);
|
||||
}
|
||||
|
||||
|
|
@ -304,7 +304,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
VisitDefault(block);
|
||||
|
||||
_builder.Pop();
|
||||
|
||||
|
||||
if (templateNode.Children.Count > 0)
|
||||
{
|
||||
var sourceRangeStart = templateNode
|
||||
|
|
@ -376,9 +376,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
}
|
||||
}
|
||||
|
||||
_builder.Add(new CSharpTokenIRNode()
|
||||
_builder.Add(new RazorIRToken()
|
||||
{
|
||||
Content = span.Content,
|
||||
Kind = RazorIRToken.TokenKind.CSharp,
|
||||
Source = BuildSourceSpanFromNode(span),
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,39 +0,0 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
|
||||
{
|
||||
public class CSharpTokenIRNode : RazorIRNode
|
||||
{
|
||||
public override IList<RazorIRNode> Children { get; } = EmptyArray;
|
||||
|
||||
public override RazorIRNode Parent { get; set; }
|
||||
|
||||
public override SourceSpan? Source { get; set; }
|
||||
|
||||
public string Content { get; set; }
|
||||
|
||||
public override void Accept(RazorIRNodeVisitor visitor)
|
||||
{
|
||||
if (visitor == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(visitor));
|
||||
}
|
||||
|
||||
visitor.VisitCSharpToken(this);
|
||||
}
|
||||
|
||||
public override TResult Accept<TResult>(RazorIRNodeVisitor<TResult> visitor)
|
||||
{
|
||||
if (visitor == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(visitor));
|
||||
}
|
||||
|
||||
return visitor.VisitCSharpToken(this);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -49,11 +49,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
|
|||
VisitDefault(node);
|
||||
}
|
||||
|
||||
public virtual void VisitCSharpToken(CSharpTokenIRNode node)
|
||||
{
|
||||
VisitDefault(node);
|
||||
}
|
||||
|
||||
public virtual void VisitHtmlAttributeValue(HtmlAttributeValueIRNode node)
|
||||
{
|
||||
VisitDefault(node);
|
||||
|
|
|
|||
|
|
@ -50,11 +50,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
|
|||
return VisitDefault(node);
|
||||
}
|
||||
|
||||
public virtual TResult VisitCSharpToken(CSharpTokenIRNode node)
|
||||
{
|
||||
return VisitDefault(node);
|
||||
}
|
||||
|
||||
public virtual TResult VisitHtmlAttributeValue(HtmlAttributeValueIRNode node)
|
||||
{
|
||||
return VisitDefault(node);
|
||||
|
|
|
|||
|
|
@ -67,9 +67,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
{
|
||||
Source = CreateSource(2),
|
||||
});
|
||||
builder.Add(new CSharpTokenIRNode()
|
||||
builder.Add(new RazorIRToken()
|
||||
{
|
||||
Content = "Hi",
|
||||
Kind = RazorIRToken.TokenKind.CSharp,
|
||||
});
|
||||
|
||||
var irDocument = (DocumentIRNode)builder.Build();
|
||||
|
|
@ -93,9 +94,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
// Arrange
|
||||
var builder = RazorIRBuilder.Document();
|
||||
builder.Push(new CSharpExpressionIRNode());
|
||||
builder.Add(new CSharpTokenIRNode()
|
||||
builder.Add(new RazorIRToken()
|
||||
{
|
||||
Content = "Hi",
|
||||
Kind = RazorIRToken.TokenKind.CSharp,
|
||||
});
|
||||
|
||||
var irDocument = (DocumentIRNode)builder.Build();
|
||||
|
|
@ -128,9 +130,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
Source = CreateSource(5)
|
||||
});
|
||||
|
||||
builder.Add(new CSharpTokenIRNode()
|
||||
builder.Add(new RazorIRToken()
|
||||
{
|
||||
Content = "Hi",
|
||||
Kind = RazorIRToken.TokenKind.CSharp,
|
||||
});
|
||||
|
||||
var irDocument = (DocumentIRNode)builder.Build();
|
||||
|
|
@ -175,9 +178,10 @@ namespace Microsoft.AspNetCore.Razor.Evolution
|
|||
Source = CreateSource(5)
|
||||
});
|
||||
|
||||
builder.Add(new CSharpTokenIRNode()
|
||||
builder.Add(new RazorIRToken()
|
||||
{
|
||||
Content = "Hi",
|
||||
Kind = RazorIRToken.TokenKind.CSharp,
|
||||
});
|
||||
|
||||
var irDocument = (DocumentIRNode)builder.Build();
|
||||
|
|
|
|||
|
|
@ -61,6 +61,7 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests
|
|||
|
||||
// Assert
|
||||
AssertIRMatchesBaseline(document.GetIRDocument());
|
||||
|
||||
var csharpDocument = document.GetCSharpDocument();
|
||||
AssertCSharpDocumentMatchesBaseline(csharpDocument);
|
||||
Assert.Empty(csharpDocument.Diagnostics);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
// Copyright (c) .NET Foundation. All rights reserved.
|
||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
|
@ -42,11 +41,6 @@ namespace Microsoft.AspNetCore.Razor.Evolution.IntegrationTests
|
|||
WriteContentNode(node, node.Content);
|
||||
}
|
||||
|
||||
public override void VisitCSharpToken(CSharpTokenIRNode node)
|
||||
{
|
||||
WriteContentNode(node, node.Content);
|
||||
}
|
||||
|
||||
public override void VisitToken(RazorIRToken node)
|
||||
{
|
||||
WriteContentNode(node, node.Kind.ToString(), node.Content);
|
||||
|
|
|
|||
|
|
@ -203,7 +203,9 @@ namespace Microsoft.AspNetCore.Razor.Evolution.Intermediate
|
|||
var content = new StringBuilder();
|
||||
for (var i = 0; i < cSharp.Children.Count; i++)
|
||||
{
|
||||
content.Append(((CSharpTokenIRNode)cSharp.Children[i]).Content);
|
||||
var token = Assert.IsType<RazorIRToken>(cSharp.Children[i]);
|
||||
Assert.Equal(RazorIRToken.TokenKind.CSharp, token.Kind);
|
||||
content.Append(token.Content);
|
||||
}
|
||||
|
||||
Assert.Equal(expected, content.ToString());
|
||||
|
|
|
|||
|
|
@ -9,5 +9,5 @@ Document -
|
|||
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
|
||||
RazorIRToken - (32:2,16 [5] HtmlWithConditionalAttribute.cshtml) - CSharp - Hello
|
||||
HtmlContent - (38:2,22 [22] HtmlWithConditionalAttribute.cshtml) - />\n</body>\n</html>"
|
||||
|
|
|
|||
|
|
@ -7,5 +7,5 @@ Document -
|
|||
RazorMethodDeclaration - - - - -
|
||||
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
|
||||
RazorIRToken - (37:2,21 [5] HtmlWithDataDashAttribute.cshtml) - CSharp - Hello
|
||||
HtmlContent - (42:2,26 [23] HtmlWithDataDashAttribute.cshtml) - " />\n</body>\n</html>"
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ Document -
|
|||
CSharpStatement - - EndContext();
|
||||
CSharpStatement - - BeginContext(61, 7, false);
|
||||
CSharpExpression - (61:2,2 [7] BasicTest.cshtml)
|
||||
CSharpToken - (61:2,2 [7] BasicTest.cshtml) - "Hello"
|
||||
RazorIRToken - (61:2,2 [7] BasicTest.cshtml) - CSharp - "Hello"
|
||||
CSharpStatement - - EndContext();
|
||||
CSharpStatement - - BeginContext(69, 2, true);
|
||||
HtmlContent - (69:2,10 [2] BasicTest.cshtml) - \n
|
||||
|
|
@ -30,7 +30,7 @@ Document -
|
|||
SetPreallocatedTagHelperProperty - - __tagHelperAttribute_0 - value - FooProp
|
||||
SetTagHelperProperty - (121:4,28 [13] BasicTest.cshtml) - date - BarProp - HtmlAttributeValueStyle.DoubleQuotes
|
||||
CSharpExpression - (122:4,29 [12] BasicTest.cshtml)
|
||||
CSharpToken - (122:4,29 [12] BasicTest.cshtml) - DateTime.Now
|
||||
RazorIRToken - (122:4,29 [12] BasicTest.cshtml) - CSharp - DateTime.Now
|
||||
AddPreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_1
|
||||
CSharpStatement - - BeginContext(97, 52, false);
|
||||
ExecuteTagHelpers -
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@ Document -
|
|||
CreateTagHelper - - InputTagHelper
|
||||
SetTagHelperProperty - (56:2,17 [6] TagHelpersWithBoundAttributes.cshtml) - bound - FooProp - HtmlAttributeValueStyle.DoubleQuotes
|
||||
CSharpExpression - (57:2,18 [5] TagHelpersWithBoundAttributes.cshtml)
|
||||
CSharpToken - (57:2,18 [5] TagHelpersWithBoundAttributes.cshtml) - Hello
|
||||
RazorIRToken - (57:2,18 [5] TagHelpersWithBoundAttributes.cshtml) - CSharp - Hello
|
||||
AddPreallocatedTagHelperHtmlAttribute - - __tagHelperAttribute_0
|
||||
ExecuteTagHelpers -
|
||||
HtmlContent - (77:2,38 [9] TagHelpersWithBoundAttributes.cshtml) - \n</form>
|
||||
|
|
|
|||
Loading…
Reference in New Issue