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
This commit is contained in:
parent
5fecd5bd1e
commit
392871beb6
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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("</p>")))),
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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("</p>"))))),
|
||||
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}<p>Hello World</p>{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("<p>", AcceptedCharacters.None),
|
||||
Factory.Markup("Hello World"),
|
||||
BlockFactory.MarkupTagBlock("</p>", 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));
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue