[Fixes #804] Fixed unsafe cast in CSharpCodeVisitor
This commit is contained in:
parent
687b3f6911
commit
f09e2bf571
|
|
@ -399,36 +399,47 @@ namespace Microsoft.AspNetCore.Razor.CodeGenerators.Visitors
|
||||||
|
|
||||||
public void RenderDesignTimeExpressionBlockChunk(ExpressionBlockChunk chunk)
|
public void RenderDesignTimeExpressionBlockChunk(ExpressionBlockChunk chunk)
|
||||||
{
|
{
|
||||||
var firstChild = (ExpressionChunk)chunk.Children.FirstOrDefault();
|
var firstChild = chunk.Children.FirstOrDefault();
|
||||||
|
if (firstChild == null)
|
||||||
if (firstChild != null)
|
|
||||||
{
|
{
|
||||||
var currentIndent = Writer.CurrentIndent;
|
return;
|
||||||
var designTimeAssignment = "__o = ";
|
}
|
||||||
Writer.ResetIndent();
|
|
||||||
|
|
||||||
var documentLocation = firstChild.Association.Start;
|
var currentIndent = Writer.CurrentIndent;
|
||||||
// This is only here to enable accurate formatting by the C# editor.
|
Writer.ResetIndent();
|
||||||
Writer.WriteLineNumberDirective(documentLocation, Context.SourceFile);
|
|
||||||
|
|
||||||
|
var documentLocation = firstChild.Association.Start;
|
||||||
|
|
||||||
|
// This is only here to enable accurate formatting by the C# editor.
|
||||||
|
Writer.WriteLineNumberDirective(documentLocation, Context.SourceFile);
|
||||||
|
|
||||||
|
var designTimeAssignment = "__o = ";
|
||||||
|
var firstChildExpressionChunk = firstChild as ExpressionChunk;
|
||||||
|
if (firstChildExpressionChunk != null)
|
||||||
|
{
|
||||||
// We build the padding with an offset of the design time assignment statement.
|
// We build the padding with an offset of the design time assignment statement.
|
||||||
Writer.Write(_paddingBuilder.BuildExpressionPadding((Span)firstChild.Association, designTimeAssignment.Length))
|
Writer.Write(_paddingBuilder.BuildExpressionPadding((Span)firstChildExpressionChunk.Association, designTimeAssignment.Length))
|
||||||
.Write(designTimeAssignment);
|
.Write(designTimeAssignment);
|
||||||
|
|
||||||
// We map the first line of code but do not write the line pragmas associated with it.
|
// We map the first line of code but do not write the line pragmas associated with it.
|
||||||
CreateRawCodeMapping(firstChild.Code, documentLocation);
|
CreateRawCodeMapping(firstChildExpressionChunk.Code, documentLocation);
|
||||||
|
|
||||||
// Render all but the first child.
|
// Render all but the first child.
|
||||||
// The reason why we render the other children differently is because when formatting the C# code
|
// The reason why we render the other children differently is because when formatting the C# code
|
||||||
// the formatter expects the start line to have the assignment statement on it.
|
// the formatter expects the start line to have the assignment statement on it.
|
||||||
Accept(chunk.Children.Skip(1).ToList());
|
Accept(chunk.Children.Skip(1).ToList());
|
||||||
|
|
||||||
Writer.WriteLine(";")
|
|
||||||
.WriteLine()
|
|
||||||
.WriteLineDefaultDirective()
|
|
||||||
.WriteLineHiddenDirective()
|
|
||||||
.SetIndent(currentIndent);
|
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// First child is not an expression chunk
|
||||||
|
Accept(chunk.Children);
|
||||||
|
}
|
||||||
|
|
||||||
|
Writer.WriteLine(";")
|
||||||
|
.WriteLine()
|
||||||
|
.WriteLineDefaultDirective()
|
||||||
|
.WriteLineHiddenDirective()
|
||||||
|
.SetIndent(currentIndent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void RenderRuntimeExpressionBlockChunk(ExpressionBlockChunk chunk)
|
public void RenderRuntimeExpressionBlockChunk(ExpressionBlockChunk chunk)
|
||||||
|
|
|
||||||
|
|
@ -72,6 +72,7 @@ namespace Microsoft.AspNetCore.Razor.CodeGenerators
|
||||||
[InlineData("ConditionalAttributes")]
|
[InlineData("ConditionalAttributes")]
|
||||||
[InlineData("Await")]
|
[InlineData("Await")]
|
||||||
[InlineData("CodeBlockWithTextElement")]
|
[InlineData("CodeBlockWithTextElement")]
|
||||||
|
[InlineData("ExplicitExpressionWithMarkup")]
|
||||||
public void CSharpChunkGeneratorCorrectlyGeneratesRunTimeCode(string testType)
|
public void CSharpChunkGeneratorCorrectlyGeneratesRunTimeCode(string testType)
|
||||||
{
|
{
|
||||||
RunTest(testType);
|
RunTest(testType);
|
||||||
|
|
@ -404,6 +405,15 @@ namespace Microsoft.AspNetCore.Razor.CodeGenerators
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void CSharpChunkGeneratorCorrectlyGeneratesDesignTimePragmasForExplicitExpressionContainingMarkup()
|
||||||
|
{
|
||||||
|
RunTest(
|
||||||
|
"ExplicitExpressionWithMarkup",
|
||||||
|
"ExplicitExpressionWithMarkup.DesignTime",
|
||||||
|
designTimeMode: true,
|
||||||
|
expectedDesignTimePragmas: new List<LineMapping>());
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void CSharpChunkGeneratorCorrectlyGeneratesDesignTimePragmasForImplicitExpressionStartedAtEOF()
|
public void CSharpChunkGeneratorCorrectlyGeneratesDesignTimePragmasForImplicitExpressionStartedAtEOF()
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,33 @@
|
||||||
|
namespace TestOutput
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public class ExplicitExpressionWithMarkup
|
||||||
|
{
|
||||||
|
private static object @__o;
|
||||||
|
private void @__RazorDesignTimeHelpers__()
|
||||||
|
{
|
||||||
|
#pragma warning disable 219
|
||||||
|
#pragma warning restore 219
|
||||||
|
}
|
||||||
|
#line hidden
|
||||||
|
public ExplicitExpressionWithMarkup()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable 1998
|
||||||
|
public override async Task ExecuteAsync()
|
||||||
|
{
|
||||||
|
#line 1 "ExplicitExpressionWithMarkup.cshtml"
|
||||||
|
item => new Template(async(__razor_template_writer) => {
|
||||||
|
}
|
||||||
|
)
|
||||||
|
;
|
||||||
|
|
||||||
|
#line default
|
||||||
|
#line hidden
|
||||||
|
}
|
||||||
|
#pragma warning restore 1998
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,36 @@
|
||||||
|
#pragma checksum "ExplicitExpressionWithMarkup.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "1252c799cdeb86a71e4304f01ebaae540fa26894"
|
||||||
|
namespace TestOutput
|
||||||
|
{
|
||||||
|
using System;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
public class ExplicitExpressionWithMarkup
|
||||||
|
{
|
||||||
|
#line hidden
|
||||||
|
public ExplicitExpressionWithMarkup()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
#pragma warning disable 1998
|
||||||
|
public override async Task ExecuteAsync()
|
||||||
|
{
|
||||||
|
Instrumentation.BeginContext(0, 5, true);
|
||||||
|
WriteLiteral("<div>");
|
||||||
|
Instrumentation.EndContext();
|
||||||
|
Instrumentation.BeginContext(14, 0, false);
|
||||||
|
#line 1 "ExplicitExpressionWithMarkup.cshtml"
|
||||||
|
Write(item => new Template(async(__razor_template_writer) => {
|
||||||
|
Instrumentation.BeginContext(8, 6, true);
|
||||||
|
WriteLiteralTo(__razor_template_writer, "</div>");
|
||||||
|
Instrumentation.EndContext();
|
||||||
|
}
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
#line default
|
||||||
|
#line hidden
|
||||||
|
Instrumentation.EndContext();
|
||||||
|
}
|
||||||
|
#pragma warning restore 1998
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
<div>@(@</div>
|
||||||
Loading…
Reference in New Issue