Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
[Fixes #804] Fixed unsafe cast in CSharpCodeVisitor
Browse files Browse the repository at this point in the history
  • Loading branch information
ajaybhargavb committed Aug 3, 2016
1 parent d337bac commit 1cb8423
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -399,29 +399,38 @@ protected override void Visit(SectionChunk chunk)

public void RenderDesignTimeExpressionBlockChunk(ExpressionBlockChunk chunk)
{
var firstChild = (ExpressionChunk)chunk.Children.FirstOrDefault();

var firstChild = chunk.Children.FirstOrDefault();
if (firstChild != null)
{
var currentIndent = Writer.CurrentIndent;
var designTimeAssignment = "__o = ";
Writer.ResetIndent();

var documentLocation = firstChild.Association.Start;

// This is only here to enable accurate formatting by the C# editor.
Writer.WriteLineNumberDirective(documentLocation, Context.SourceFile);

// We build the padding with an offset of the design time assignment statement.
Writer.Write(_paddingBuilder.BuildExpressionPadding((Span)firstChild.Association, designTimeAssignment.Length))
.Write(designTimeAssignment);
var designTimeAssignment = "__o = ";
var firstChildExpressionChunk = firstChild as ExpressionChunk;
if (firstChildExpressionChunk != null)
{
// We build the padding with an offset of the design time assignment statement.
Writer.Write(_paddingBuilder.BuildExpressionPadding((Span)firstChildExpressionChunk.Association, designTimeAssignment.Length))
.Write(designTimeAssignment);

// We map the first line of code but do not write the line pragmas associated with it.
CreateRawCodeMapping(firstChild.Code, documentLocation);
// We map the first line of code but do not write the line pragmas associated with it.
CreateRawCodeMapping(firstChildExpressionChunk.Code, documentLocation);

// Render all but the first child.
// 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.
Accept(chunk.Children.Skip(1).ToList());
// Render all but the first child.
// 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.
Accept(chunk.Children.Skip(1).ToList());
}
else
{
// First child is not an expression block
Accept(chunk.Children.ToList());
}

Writer.WriteLine(";")
.WriteLine()
Expand Down
4 changes: 4 additions & 0 deletions src/Microsoft.AspNetCore.Razor/Parser/CSharpCodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,11 @@ protected override void HandleEmbeddedTransition()
{
if (Language.IsTransition(CurrentSymbol))
{
//Accept(CurrentSymbol);
//NextToken();
//Output(SpanKind.Transition);
PutCurrentBack();
Output(SpanKind.Code);
Template();
}
else if (Language.IsCommentStart(CurrentSymbol))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ public void CSharpChunkGeneratorCorrectlyGeneratesDesignTimePragmasMarkupAndExpr
});
}

[Fact]
public void CSharpChunkGeneratorCorrectlyGeneratesDesignTimePragmasForExplicitExpressionContainingMarkup()
{
RunTest("ExplicitExpressionWithMarkup", designTimeMode: true, expectedDesignTimePragmas: new List<LineMapping>());
}

[Fact]
public void CSharpChunkGeneratorCorrectlyGeneratesDesignTimePragmasForImplicitExpressionStartedAtEOF()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>@(@</div>

0 comments on commit 1cb8423

Please sign in to comment.