From 392871beb62683bc366a8add4697af657b61a859 Mon Sep 17 00:00:00 2001
From: "N. Taylor Mullen"
Date: Thu, 10 Dec 2015 12:00:46 -0800
Subject: [PATCH] Change missing section end brace error to work at EOF.
- Updated existing tests and added a new case to understand `@section {....` scenarios.
- Instead of trying to guess 1 character prior to EOF decided to log error on opening brace since we know its position. Updated error to account for change in location.
#625
---
.../Parser/CSharpCodeParser.Directives.cs | 9 ++-
.../Parser/CSharp/CSharpAutoCompleteTest.cs | 8 +--
.../Parser/CSharp/CSharpSectionTest.cs | 65 +++++++++++++++++--
3 files changed, 72 insertions(+), 10 deletions(-)
diff --git a/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Directives.cs b/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Directives.cs
index dc99f0f39a..f07115df11 100644
--- a/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Directives.cs
+++ b/src/Microsoft.AspNet.Razor/Parser/CSharpCodeParser.Directives.cs
@@ -114,6 +114,8 @@ namespace Microsoft.AspNet.Razor.Parser
Accept(whitespace);
}
+ var startingBraceLocation = CurrentLocation;
+
// Set up edit handler
var editHandler = new AutoCompleteEditHandler(Language.TokenizeString, autoCompleteAtEndOfSpan: true);
@@ -130,8 +132,11 @@ namespace Microsoft.AspNet.Razor.Parser
{
editHandler.AutoCompleteString = "}";
Context.OnError(
- CurrentLocation,
- RazorResources.FormatParseError_Expected_X(Language.GetSample(CSharpSymbolType.RightBrace)),
+ startingBraceLocation,
+ RazorResources.FormatParseError_Expected_EndOfBlock_Before_EOF(
+ SyntaxConstants.CSharp.SectionKeyword,
+ Language.GetSample(CSharpSymbolType.RightBrace),
+ Language.GetSample(CSharpSymbolType.LeftBrace)),
length: 1 /* } */);
}
else
diff --git a/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpAutoCompleteTest.cs b/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpAutoCompleteTest.cs
index 13457dfbed..0d376bf11c 100644
--- a/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpAutoCompleteTest.cs
+++ b/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpAutoCompleteTest.cs
@@ -46,8 +46,8 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
.Accepts(AcceptedCharacters.Any),
new MarkupBlock()),
new RazorError(
- RazorResources.FormatParseError_Expected_X("}"),
- new SourceLocation(17, 0, 17),
+ RazorResources.FormatParseError_Expected_EndOfBlock_Before_EOF("section", "}", "{"),
+ new SourceLocation(16, 0, 16),
length: 1));
}
@@ -109,8 +109,8 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
new MarkupTagBlock(
Factory.Markup("
")))),
new RazorError(
- RazorResources.FormatParseError_Expected_X("}"),
- new SourceLocation(27 + Environment.NewLine.Length, 1, 10),
+ RazorResources.FormatParseError_Expected_EndOfBlock_Before_EOF("section", "}", "{"),
+ new SourceLocation(16, 0, 16),
length: 1));
}
diff --git a/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpSectionTest.cs b/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpSectionTest.cs
index bbc77fb3dc..3738d8d7dc 100644
--- a/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpSectionTest.cs
+++ b/test/Microsoft.AspNet.Razor.Test/Parser/CSharp/CSharpSectionTest.cs
@@ -170,8 +170,30 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
.AutoCompleteWith("}", atEndOfSpan: true),
new MarkupBlock())),
new RazorError(
- RazorResources.FormatParseError_Expected_X("}"),
- new SourceLocation(14, 0, 14),
+ RazorResources.FormatParseError_Expected_EndOfBlock_Before_EOF("section", "}", "{"),
+ new SourceLocation(13, 0, 13),
+ length: 1));
+ }
+
+ [Theory]
+ [InlineData(" ")]
+ [InlineData("\n")]
+ [InlineData(" abc")]
+ [InlineData(" \n abc")]
+ public void ParseSectionBlockHandlesEOFAfterOpenContent(string postStartBrace)
+ {
+ ParseDocumentTest("@section foo {" + postStartBrace,
+ new MarkupBlock(
+ Factory.EmptyHtml(),
+ new SectionBlock(new SectionChunkGenerator("foo"),
+ Factory.CodeTransition(),
+ Factory.MetaCode("section foo {")
+ .AutoCompleteWith("}", atEndOfSpan: true),
+ new MarkupBlock(
+ Factory.Markup(postStartBrace)))),
+ new RazorError(
+ RazorResources.FormatParseError_Expected_EndOfBlock_Before_EOF("section", "}", "{"),
+ new SourceLocation(13, 0, 13),
length: 1));
}
@@ -194,8 +216,43 @@ namespace Microsoft.AspNet.Razor.Test.Parser.CSharp
new MarkupTagBlock(
Factory.Markup(""))))),
new RazorError(
- RazorResources.FormatParseError_Expected_X("}"),
- new SourceLocation(27, 0, 27),
+ RazorResources.FormatParseError_Expected_EndOfBlock_Before_EOF("section", "}", "{"),
+ new SourceLocation(13, 0, 13),
+ length: 1));
+ }
+
+ [Fact]
+ public void ParseSectionBlockHandlesUnterminatedSectionWithNestedIf()
+ {
+ var newLine = Environment.NewLine;
+ var spaces = " ";
+ ParseDocumentTest(
+ string.Format(
+ "@section Test{0}{{{0}{1}@if(true){0}{1}{{{0}{1}{1}Hello World
{0}{1}}}",
+ newLine,
+ spaces),
+ new MarkupBlock(
+ Factory.EmptyHtml(),
+ new SectionBlock(new SectionChunkGenerator("Test"),
+ Factory.CodeTransition(),
+ Factory.MetaCode($"section Test{newLine}{{")
+ .AutoCompleteWith("}", atEndOfSpan: true),
+ new MarkupBlock(
+ Factory.Markup(newLine),
+ new StatementBlock(
+ Factory.Code(spaces).AsStatement(),
+ Factory.CodeTransition(),
+ Factory.Code($"if(true){newLine}{spaces}{{{newLine}").AsStatement(),
+ new MarkupBlock(
+ Factory.Markup($"{spaces}{spaces}"),
+ BlockFactory.MarkupTagBlock("", AcceptedCharacters.None),
+ Factory.Markup("Hello World"),
+ BlockFactory.MarkupTagBlock("
", AcceptedCharacters.None),
+ Factory.Markup(newLine).Accepts(AcceptedCharacters.None)),
+ Factory.Code($"{spaces}}}").AsStatement())))),
+ new RazorError(
+ RazorResources.FormatParseError_Expected_EndOfBlock_Before_EOF("section", "}", "{"),
+ new SourceLocation(13 + newLine.Length, 1, 0),
length: 1));
}