From ac57da1dcd60b407fd929b82de3e23a362362295 Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Mon, 17 Nov 2025 12:34:04 -0800 Subject: [PATCH 1/7] Add switch expression tests --- .../test/Legacy/CSharpBlockTest.cs | 111 ++++++++++++++++++ .../SwitchExpression.cspans.txt | 6 + .../SwitchExpression.stree.txt | 52 ++++++++ .../SwitchExpression_Incomplete.cspans.txt | 4 + .../SwitchExpression_Incomplete.diag.txt | 1 + .../SwitchExpression_Incomplete.stree.txt | 50 ++++++++ ...witchExpression_WithGreaterThan.cspans.txt | 6 + ...SwitchExpression_WithGreaterThan.stree.txt | 46 ++++++++ .../SwitchExpression_WithLessThan.cspans.txt | 9 ++ .../SwitchExpression_WithLessThan.diag.txt | 2 + .../SwitchExpression_WithLessThan.stree.txt | 61 ++++++++++ ...ression_WithLessThan_Incomplete.cspans.txt | 9 ++ ...xpression_WithLessThan_Incomplete.diag.txt | 2 + ...pression_WithLessThan_Incomplete.stree.txt | 71 +++++++++++ ...ression_WithMultipleComparisons.cspans.txt | 9 ++ ...xpression_WithMultipleComparisons.diag.txt | 2 + ...pression_WithMultipleComparisons.stree.txt | 93 +++++++++++++++ ...itchExpression_WithWrongKeyword.cspans.txt | 6 + ...witchExpression_WithWrongKeyword.stree.txt | 44 +++++++ ...on_WithWrongKeyword_AndLessThan.cspans.txt | 9 ++ ...sion_WithWrongKeyword_AndLessThan.diag.txt | 2 + ...ion_WithWrongKeyword_AndLessThan.stree.txt | 61 ++++++++++ 22 files changed, 656 insertions(+) create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.diag.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.diag.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.stree.txt diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs index 6142e050d76..e7b58d5df3a 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs @@ -931,6 +931,117 @@ public void CommentOnSameLineAsHtml() """); } + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + 0 => "value", + _ => "no value" + }; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithLessThan() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + < 9 => "less than 10" + }; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithGreaterThan() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + > 10 => "greater than 10" + }; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithMultipleComparisons() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + < 9 => "less than 10", + 10 => "equal to 10", + > 10 => "greater than 10" + }; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_Incomplete() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + 0 => "value" + + var val2 = "value2"; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithLessThan_Incomplete() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + < 9 => "less than 10" + + var val2 = "value2"; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithWrongKeyword() + { + ParseDocumentTest(""" + @{ + var val = 0 using + { + 0 => "value" + }; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithWrongKeyword_AndLessThan() + { + ParseDocumentTest(""" + @{ + var val = 0 using + { + < 9 => "less than 10" + }; + } + """); + } + + + private void RunRazorCommentBetweenClausesTest(string preComment, string postComment, AcceptedCharactersInternal acceptedCharacters = AcceptedCharactersInternal.Any) { ParseDocumentTest(preComment + "@* Foo *@ @* Bar *@" + postComment); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.cspans.txt new file mode 100644 index 00000000000..4735a170f25 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.cspans.txt @@ -0,0 +1,6 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [92] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [92] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [92] ) +Code span at (2:0,2 [89] ) - Parent: Statement block at (0:0,0 [92] ) +MetaCode span at (91:6,0 [1] ) - Parent: Statement block at (0:0,0 [92] ) +Markup span at (92:6,1 [0] ) - Parent: Markup block at (0:0,0 [92] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.stree.txt new file mode 100644 index 00000000000..c12ad43af9b --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression.stree.txt @@ -0,0 +1,52 @@ +RazorDocument - [0..92)::92 - [@{LF var val = 0 switchLF {LF 0 => "value",LF _ => "no value"LF };LF}] + MarkupBlock - [0..92)::92 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..92)::92 + CSharpStatement - [0..92)::92 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..92)::91 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..91)::89 + CSharpStatementLiteral - [2..91)::89 - [LF var val = 0 switchLF {LF 0 => "value",LF _ => "no value"LF };LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["value"]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[_]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["no value"]; + NewLine;[LF]; + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [91..92)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [92..92)::0 - [] - Gen + Marker;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.cspans.txt new file mode 100644 index 00000000000..c01f9850b2a --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.cspans.txt @@ -0,0 +1,4 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [86] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [86] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [86] ) +Code span at (2:0,2 [84] ) - Parent: Statement block at (0:0,0 [86] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.diag.txt new file mode 100644 index 00000000000..bcd37730bb1 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.diag.txt @@ -0,0 +1 @@ +(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.stree.txt new file mode 100644 index 00000000000..cb8363db2ba --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_Incomplete.stree.txt @@ -0,0 +1,50 @@ +RazorDocument - [0..86)::86 - [@{LF var val = 0 switchLF {LF 0 => "value"LFLF var val2 = "value2";LF}] + MarkupBlock - [0..86)::86 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..86)::86 + CSharpStatement - [0..86)::86 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..86)::85 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..86)::84 + CSharpStatementLiteral - [2..86)::84 - [LF var val = 0 switchLF {LF 0 => "value"LFLF var val2 = "value2";LF}] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["value"]; + NewLine;[LF]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val2]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + StringLiteral;["value2"]; + Semicolon;[;]; + NewLine;[LF]; + RightBrace;[}]; + RazorMetaCode - [86..86)::0 - Gen + RightBrace;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.cspans.txt new file mode 100644 index 00000000000..419520ad9b6 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.cspans.txt @@ -0,0 +1,6 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [79] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [79] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [79] ) +Code span at (2:0,2 [76] ) - Parent: Statement block at (0:0,0 [79] ) +MetaCode span at (78:5,0 [1] ) - Parent: Statement block at (0:0,0 [79] ) +Markup span at (79:5,1 [0] ) - Parent: Markup block at (0:0,0 [79] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.stree.txt new file mode 100644 index 00000000000..a55bfd990a0 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithGreaterThan.stree.txt @@ -0,0 +1,46 @@ +RazorDocument - [0..79)::79 - [@{LF var val = 0 switchLF {LF > 10 => "greater than 10"LF };LF}] + MarkupBlock - [0..79)::79 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..79)::79 + CSharpStatement - [0..79)::79 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..79)::78 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..78)::76 + CSharpStatementLiteral - [2..78)::76 - [LF var val = 0 switchLF {LF > 10 => "greater than 10"LF };LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + GreaterThan;[>]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["greater than 10"]; + NewLine;[LF]; + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [78..79)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [79..79)::0 - [] - Gen + Marker;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt new file mode 100644 index 00000000000..8cf345d7c6a --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt @@ -0,0 +1,9 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [75] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [75] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [75] ) +Code span at (2:0,2 [33] ) - Parent: Statement block at (0:0,0 [75] ) +Markup span at (35:3,0 [8] ) - Parent: Markup block at (35:3,0 [40] ) +Markup span at (43:3,8 [1] ) - Parent: Tag block at (43:3,8 [6] ) +Markup span at (44:3,9 [4] ) - Parent: Markup block at (44:3,9 [4] ) +Markup span at (48:3,13 [1] ) - Parent: Tag block at (43:3,8 [6] ) +Markup span at (49:3,14 [26] ) - Parent: Markup block at (35:3,0 [40] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt new file mode 100644 index 00000000000..c692e50ed03 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt @@ -0,0 +1,2 @@ +(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. +(4,10): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt new file mode 100644 index 00000000000..c1e71dffdcd --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt @@ -0,0 +1,61 @@ +RazorDocument - [0..75)::75 - [@{LF var val = 0 switchLF {LF < 9 => "less than 10"LF };LF}] + MarkupBlock - [0..75)::75 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..75)::75 + CSharpStatement - [0..75)::75 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..75)::74 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..75)::73 + CSharpStatementLiteral - [2..35)::33 - [LF var val = 0 switchLF {LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + MarkupBlock - [35..75)::40 + MarkupTextLiteral - [35..43)::8 - [ ] - Gen + Whitespace;[ ]; + MarkupElement - [43..75)::32 + MarkupStartTag - [43..49)::6 - [< 9 =>] - Gen + OpenAngle;[<]; + Text;[]; + MarkupAttributeBlock - [44..48)::4 - [ 9 =] + MarkupTextLiteral - [44..45)::1 - [ ] - Gen + Whitespace;[ ]; + MarkupTextLiteral - [45..46)::1 - [9] - Gen + Text;[9]; + MarkupTextLiteral - [46..47)::1 - [ ] - Gen + Whitespace;[ ]; + Equals;[=]; + CloseAngle;[>]; + MarkupTextLiteral - [49..75)::26 - [ "less than 10"LF };LF}] - Gen + Whitespace;[ ]; + DoubleQuote;["]; + Text;[less]; + Whitespace;[ ]; + Text;[than]; + Whitespace;[ ]; + Text;[10]; + DoubleQuote;["]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[};]; + NewLine;[LF]; + Text;[}]; + RazorMetaCode - [75..75)::0 - Gen + RightBrace;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt new file mode 100644 index 00000000000..cdd6b771e9f --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt @@ -0,0 +1,9 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [95] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [95] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [95] ) +Code span at (2:0,2 [33] ) - Parent: Statement block at (0:0,0 [95] ) +Markup span at (35:3,0 [8] ) - Parent: Markup block at (35:3,0 [60] ) +Markup span at (43:3,8 [1] ) - Parent: Tag block at (43:3,8 [6] ) +Markup span at (44:3,9 [4] ) - Parent: Markup block at (44:3,9 [4] ) +Markup span at (48:3,13 [1] ) - Parent: Tag block at (43:3,8 [6] ) +Markup span at (49:3,14 [46] ) - Parent: Markup block at (35:3,0 [60] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt new file mode 100644 index 00000000000..c692e50ed03 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt @@ -0,0 +1,2 @@ +(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. +(4,10): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt new file mode 100644 index 00000000000..12c494df3cb --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt @@ -0,0 +1,71 @@ +RazorDocument - [0..95)::95 - [@{LF var val = 0 switchLF {LF < 9 => "less than 10"LFLF var val2 = "value2";LF}] + MarkupBlock - [0..95)::95 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..95)::95 + CSharpStatement - [0..95)::95 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..95)::94 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..95)::93 + CSharpStatementLiteral - [2..35)::33 - [LF var val = 0 switchLF {LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + MarkupBlock - [35..95)::60 + MarkupTextLiteral - [35..43)::8 - [ ] - Gen + Whitespace;[ ]; + MarkupElement - [43..95)::52 + MarkupStartTag - [43..49)::6 - [< 9 =>] - Gen + OpenAngle;[<]; + Text;[]; + MarkupAttributeBlock - [44..48)::4 - [ 9 =] + MarkupTextLiteral - [44..45)::1 - [ ] - Gen + Whitespace;[ ]; + MarkupTextLiteral - [45..46)::1 - [9] - Gen + Text;[9]; + MarkupTextLiteral - [46..47)::1 - [ ] - Gen + Whitespace;[ ]; + Equals;[=]; + CloseAngle;[>]; + MarkupTextLiteral - [49..95)::46 - [ "less than 10"LFLF var val2 = "value2";LF}] - Gen + Whitespace;[ ]; + DoubleQuote;["]; + Text;[less]; + Whitespace;[ ]; + Text;[than]; + Whitespace;[ ]; + Text;[10]; + DoubleQuote;["]; + NewLine;[LF]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[var]; + Whitespace;[ ]; + Text;[val2]; + Whitespace;[ ]; + Equals;[=]; + Whitespace;[ ]; + DoubleQuote;["]; + Text;[value2]; + DoubleQuote;["]; + Text;[;]; + NewLine;[LF]; + Text;[}]; + RazorMetaCode - [95..95)::0 - Gen + RightBrace;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt new file mode 100644 index 00000000000..60892a58bad --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt @@ -0,0 +1,9 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [141] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [141] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [141] ) +Code span at (2:0,2 [33] ) - Parent: Statement block at (0:0,0 [141] ) +Markup span at (35:3,0 [8] ) - Parent: Markup block at (35:3,0 [106] ) +Markup span at (43:3,8 [1] ) - Parent: Tag block at (43:3,8 [6] ) +Markup span at (44:3,9 [4] ) - Parent: Markup block at (44:3,9 [4] ) +Markup span at (48:3,13 [1] ) - Parent: Tag block at (43:3,8 [6] ) +Markup span at (49:3,14 [92] ) - Parent: Markup block at (35:3,0 [106] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt new file mode 100644 index 00000000000..c692e50ed03 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt @@ -0,0 +1,2 @@ +(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. +(4,10): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt new file mode 100644 index 00000000000..c47690f82d6 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt @@ -0,0 +1,93 @@ +RazorDocument - [0..141)::141 - [@{LF var val = 0 switchLF {LF < 9 => "less than 10",LF 10 => "equal to 10",LF > 10 => "greater than 10"LF };LF}] + MarkupBlock - [0..141)::141 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..141)::141 + CSharpStatement - [0..141)::141 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..141)::140 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..141)::139 + CSharpStatementLiteral - [2..35)::33 - [LF var val = 0 switchLF {LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + MarkupBlock - [35..141)::106 + MarkupTextLiteral - [35..43)::8 - [ ] - Gen + Whitespace;[ ]; + MarkupElement - [43..141)::98 + MarkupStartTag - [43..49)::6 - [< 9 =>] - Gen + OpenAngle;[<]; + Text;[]; + MarkupAttributeBlock - [44..48)::4 - [ 9 =] + MarkupTextLiteral - [44..45)::1 - [ ] - Gen + Whitespace;[ ]; + MarkupTextLiteral - [45..46)::1 - [9] - Gen + Text;[9]; + MarkupTextLiteral - [46..47)::1 - [ ] - Gen + Whitespace;[ ]; + Equals;[=]; + CloseAngle;[>]; + MarkupTextLiteral - [49..141)::92 - [ "less than 10",LF 10 => "equal to 10",LF > 10 => "greater than 10"LF };LF}] - Gen + Whitespace;[ ]; + DoubleQuote;["]; + Text;[less]; + Whitespace;[ ]; + Text;[than]; + Whitespace;[ ]; + Text;[10]; + DoubleQuote;["]; + Text;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[10]; + Whitespace;[ ]; + Equals;[=]; + CloseAngle;[>]; + Whitespace;[ ]; + DoubleQuote;["]; + Text;[equal]; + Whitespace;[ ]; + Text;[to]; + Whitespace;[ ]; + Text;[10]; + DoubleQuote;["]; + Text;[,]; + NewLine;[LF]; + Whitespace;[ ]; + CloseAngle;[>]; + Whitespace;[ ]; + Text;[10]; + Whitespace;[ ]; + Equals;[=]; + CloseAngle;[>]; + Whitespace;[ ]; + DoubleQuote;["]; + Text;[greater]; + Whitespace;[ ]; + Text;[than]; + Whitespace;[ ]; + Text;[10]; + DoubleQuote;["]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[};]; + NewLine;[LF]; + Text;[}]; + RazorMetaCode - [141..141)::0 - Gen + RightBrace;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.cspans.txt new file mode 100644 index 00000000000..c7077e09573 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.cspans.txt @@ -0,0 +1,6 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [65] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [65] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [65] ) +Code span at (2:0,2 [62] ) - Parent: Statement block at (0:0,0 [65] ) +MetaCode span at (64:5,0 [1] ) - Parent: Statement block at (0:0,0 [65] ) +Markup span at (65:5,1 [0] ) - Parent: Markup block at (0:0,0 [65] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.stree.txt new file mode 100644 index 00000000000..0c6a1ab1dea --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword.stree.txt @@ -0,0 +1,44 @@ +RazorDocument - [0..65)::65 - [@{LF var val = 0 usingLF {LF 0 => "value"LF };LF}] + MarkupBlock - [0..65)::65 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..65)::65 + CSharpStatement - [0..65)::65 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..65)::64 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..64)::62 + CSharpStatementLiteral - [2..64)::62 - [LF var val = 0 usingLF {LF 0 => "value"LF };LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[using]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["value"]; + NewLine;[LF]; + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [64..65)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [65..65)::0 - [] - Gen + Marker;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.cspans.txt new file mode 100644 index 00000000000..f8ab6e1670e --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.cspans.txt @@ -0,0 +1,9 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [75] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [75] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [75] ) +Code span at (2:0,2 [32] ) - Parent: Statement block at (0:0,0 [75] ) +Markup span at (34:3,0 [9] ) - Parent: Markup block at (34:3,0 [41] ) +Markup span at (43:3,9 [1] ) - Parent: Tag block at (43:3,9 [6] ) +Markup span at (44:3,10 [4] ) - Parent: Markup block at (44:3,10 [4] ) +Markup span at (48:3,14 [1] ) - Parent: Tag block at (43:3,9 [6] ) +Markup span at (49:3,15 [26] ) - Parent: Markup block at (34:3,0 [41] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.diag.txt new file mode 100644 index 00000000000..38f0aa59af9 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.diag.txt @@ -0,0 +1,2 @@ +(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. +(4,11): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.stree.txt new file mode 100644 index 00000000000..aa7b280ad89 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithWrongKeyword_AndLessThan.stree.txt @@ -0,0 +1,61 @@ +RazorDocument - [0..75)::75 - [@{LF var val = 0 usingLF {LF < 9 => "less than 10"LF };LF}] + MarkupBlock - [0..75)::75 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..75)::75 + CSharpStatement - [0..75)::75 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..75)::74 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..75)::73 + CSharpStatementLiteral - [2..34)::32 - [LF var val = 0 usingLF {LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[using]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + MarkupBlock - [34..75)::41 + MarkupTextLiteral - [34..43)::9 - [ ] - Gen + Whitespace;[ ]; + MarkupElement - [43..75)::32 + MarkupStartTag - [43..49)::6 - [< 9 =>] - Gen + OpenAngle;[<]; + Text;[]; + MarkupAttributeBlock - [44..48)::4 - [ 9 =] + MarkupTextLiteral - [44..45)::1 - [ ] - Gen + Whitespace;[ ]; + MarkupTextLiteral - [45..46)::1 - [9] - Gen + Text;[9]; + MarkupTextLiteral - [46..47)::1 - [ ] - Gen + Whitespace;[ ]; + Equals;[=]; + CloseAngle;[>]; + MarkupTextLiteral - [49..75)::26 - [ "less than 10"LF };LF}] - Gen + Whitespace;[ ]; + DoubleQuote;["]; + Text;[less]; + Whitespace;[ ]; + Text;[than]; + Whitespace;[ ]; + Text;[10]; + DoubleQuote;["]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[};]; + NewLine;[LF]; + Text;[}]; + RazorMetaCode - [75..75)::0 - Gen + RightBrace;[]; + EndOfFile;[]; From f2fd8346e38ceee4401ae35148f1b391e3816104 Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Mon, 17 Nov 2025 12:44:19 -0800 Subject: [PATCH 2/7] Explicitly handle switch expressions --- .../src/Language/Legacy/CSharpCodeParser.cs | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs index 1270c10c540..9e9b47b7d43 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Legacy/CSharpCodeParser.cs @@ -997,7 +997,8 @@ not SyntaxKind.Transition and not SyntaxKind.LeftBrace and not SyntaxKind.LeftParenthesis and not SyntaxKind.LeftBracket and - not SyntaxKind.RightBrace, + not SyntaxKind.RightBrace and + not SyntaxKind.Keyword, ref read.AsRef()); if ((!Context.Options.AllowRazorInAllCodeBlocks && At(SyntaxKind.LeftBrace)) || @@ -1005,14 +1006,8 @@ not SyntaxKind.LeftBracket and At(SyntaxKind.LeftBracket)) { Accept(in read); - if (Balance(builder, BalancingModes.AllowCommentsAndTemplates | BalancingModes.BacktrackOnFailure)) + if (!TryBalanceBlock(builder)) { - TryAccept(SyntaxKind.RightBrace); - } - else - { - // Recovery - AcceptUntil(SyntaxKind.LessThan, SyntaxKind.RightBrace); return; } } @@ -1106,6 +1101,23 @@ not SyntaxKind.LeftBracket and Accept(in read); return; } + else if (At(SyntaxKind.Keyword)) + { + Accept(in read); + if (CurrentToken.Content == "switch") + { + AcceptUntil(SyntaxKind.LeftBrace); // TODO: how do we do error recovery at this point? + if (!TryBalanceBlock(builder)) + { + return; + } + } + else + { + // unknown keyword, continue parsing + AcceptAndMoveNext(); + } + } else { _tokenizer.Reset(bookmark); @@ -1114,6 +1126,22 @@ not SyntaxKind.LeftBracket and return; } } + + bool TryBalanceBlock(SyntaxListBuilder builder) + { + if (Balance(builder, BalancingModes.AllowCommentsAndTemplates | BalancingModes.BacktrackOnFailure)) + { + TryAccept(SyntaxKind.RightBrace); + } + else + { + // Recovery + AcceptUntil(SyntaxKind.LessThan, SyntaxKind.RightBrace); + return false; + } + + return true; + } } private void ParseTemplate(in SyntaxListBuilder builder) From 415a1509bb0302df90b4a55844fb262b51f63c44 Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Mon, 17 Nov 2025 12:44:29 -0800 Subject: [PATCH 3/7] Update baselines --- .../SwitchExpression_WithLessThan.cspans.txt | 9 +- .../SwitchExpression_WithLessThan.diag.txt | 2 - .../SwitchExpression_WithLessThan.stree.txt | 53 ++++----- ...ression_WithLessThan_Incomplete.cspans.txt | 7 +- ...xpression_WithLessThan_Incomplete.diag.txt | 1 - ...pression_WithLessThan_Incomplete.stree.txt | 63 ++++------- ...ression_WithMultipleComparisons.cspans.txt | 9 +- ...xpression_WithMultipleComparisons.diag.txt | 2 - ...pression_WithMultipleComparisons.stree.txt | 103 +++++++----------- 9 files changed, 85 insertions(+), 164 deletions(-) delete mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt delete mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt index 8cf345d7c6a..11d636f3db8 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.cspans.txt @@ -1,9 +1,6 @@ Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [75] ) Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [75] ) MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [75] ) -Code span at (2:0,2 [33] ) - Parent: Statement block at (0:0,0 [75] ) -Markup span at (35:3,0 [8] ) - Parent: Markup block at (35:3,0 [40] ) -Markup span at (43:3,8 [1] ) - Parent: Tag block at (43:3,8 [6] ) -Markup span at (44:3,9 [4] ) - Parent: Markup block at (44:3,9 [4] ) -Markup span at (48:3,13 [1] ) - Parent: Tag block at (43:3,8 [6] ) -Markup span at (49:3,14 [26] ) - Parent: Markup block at (35:3,0 [40] ) +Code span at (2:0,2 [72] ) - Parent: Statement block at (0:0,0 [75] ) +MetaCode span at (74:5,0 [1] ) - Parent: Statement block at (0:0,0 [75] ) +Markup span at (75:5,1 [0] ) - Parent: Markup block at (0:0,0 [75] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt deleted file mode 100644 index c692e50ed03..00000000000 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.diag.txt +++ /dev/null @@ -1,2 +0,0 @@ -(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. -(4,10): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt index c1e71dffdcd..e0c2dcba4ad 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan.stree.txt @@ -9,8 +9,8 @@ CSharpStatementBody - [1..75)::74 RazorMetaCode - [1..2)::1 - Gen LeftBrace;[{]; - CSharpCodeBlock - [2..75)::73 - CSharpStatementLiteral - [2..35)::33 - [LF var val = 0 switchLF {LF] - Gen + CSharpCodeBlock - [2..74)::72 + CSharpStatementLiteral - [2..74)::72 - [LF var val = 0 switchLF {LF < 9 => "less than 10"LF };LF] - Gen NewLine;[LF]; Whitespace;[ ]; Keyword;[var]; @@ -26,36 +26,21 @@ Whitespace;[ ]; LeftBrace;[{]; NewLine;[LF]; - MarkupBlock - [35..75)::40 - MarkupTextLiteral - [35..43)::8 - [ ] - Gen - Whitespace;[ ]; - MarkupElement - [43..75)::32 - MarkupStartTag - [43..49)::6 - [< 9 =>] - Gen - OpenAngle;[<]; - Text;[]; - MarkupAttributeBlock - [44..48)::4 - [ 9 =] - MarkupTextLiteral - [44..45)::1 - [ ] - Gen - Whitespace;[ ]; - MarkupTextLiteral - [45..46)::1 - [9] - Gen - Text;[9]; - MarkupTextLiteral - [46..47)::1 - [ ] - Gen - Whitespace;[ ]; - Equals;[=]; - CloseAngle;[>]; - MarkupTextLiteral - [49..75)::26 - [ "less than 10"LF };LF}] - Gen - Whitespace;[ ]; - DoubleQuote;["]; - Text;[less]; - Whitespace;[ ]; - Text;[than]; - Whitespace;[ ]; - Text;[10]; - DoubleQuote;["]; - NewLine;[LF]; - Whitespace;[ ]; - Text;[};]; - NewLine;[LF]; - Text;[}]; - RazorMetaCode - [75..75)::0 - Gen - RightBrace;[]; + Whitespace;[ ]; + LessThan;[<]; + Whitespace;[ ]; + NumericLiteral;[9]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["less than 10"]; + NewLine;[LF]; + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [74..75)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [75..75)::0 - [] - Gen + Marker;[]; EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt index cdd6b771e9f..ef773df7ec6 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.cspans.txt @@ -1,9 +1,4 @@ Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [95] ) Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [95] ) MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [95] ) -Code span at (2:0,2 [33] ) - Parent: Statement block at (0:0,0 [95] ) -Markup span at (35:3,0 [8] ) - Parent: Markup block at (35:3,0 [60] ) -Markup span at (43:3,8 [1] ) - Parent: Tag block at (43:3,8 [6] ) -Markup span at (44:3,9 [4] ) - Parent: Markup block at (44:3,9 [4] ) -Markup span at (48:3,13 [1] ) - Parent: Tag block at (43:3,8 [6] ) -Markup span at (49:3,14 [46] ) - Parent: Markup block at (35:3,0 [60] ) +Code span at (2:0,2 [93] ) - Parent: Statement block at (0:0,0 [95] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt index c692e50ed03..bcd37730bb1 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.diag.txt @@ -1,2 +1 @@ (1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. -(4,10): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt index 12c494df3cb..22f33697f27 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithLessThan_Incomplete.stree.txt @@ -10,7 +10,7 @@ RazorMetaCode - [1..2)::1 - Gen LeftBrace;[{]; CSharpCodeBlock - [2..95)::93 - CSharpStatementLiteral - [2..35)::33 - [LF var val = 0 switchLF {LF] - Gen + CSharpStatementLiteral - [2..95)::93 - [LF var val = 0 switchLF {LF < 9 => "less than 10"LFLF var val2 = "value2";LF}] - Gen NewLine;[LF]; Whitespace;[ ]; Keyword;[var]; @@ -26,46 +26,27 @@ Whitespace;[ ]; LeftBrace;[{]; NewLine;[LF]; - MarkupBlock - [35..95)::60 - MarkupTextLiteral - [35..43)::8 - [ ] - Gen - Whitespace;[ ]; - MarkupElement - [43..95)::52 - MarkupStartTag - [43..49)::6 - [< 9 =>] - Gen - OpenAngle;[<]; - Text;[]; - MarkupAttributeBlock - [44..48)::4 - [ 9 =] - MarkupTextLiteral - [44..45)::1 - [ ] - Gen - Whitespace;[ ]; - MarkupTextLiteral - [45..46)::1 - [9] - Gen - Text;[9]; - MarkupTextLiteral - [46..47)::1 - [ ] - Gen - Whitespace;[ ]; - Equals;[=]; - CloseAngle;[>]; - MarkupTextLiteral - [49..95)::46 - [ "less than 10"LFLF var val2 = "value2";LF}] - Gen - Whitespace;[ ]; - DoubleQuote;["]; - Text;[less]; - Whitespace;[ ]; - Text;[than]; - Whitespace;[ ]; - Text;[10]; - DoubleQuote;["]; - NewLine;[LF]; - NewLine;[LF]; - Whitespace;[ ]; - Text;[var]; - Whitespace;[ ]; - Text;[val2]; - Whitespace;[ ]; - Equals;[=]; - Whitespace;[ ]; - DoubleQuote;["]; - Text;[value2]; - DoubleQuote;["]; - Text;[;]; - NewLine;[LF]; - Text;[}]; + Whitespace;[ ]; + LessThan;[<]; + Whitespace;[ ]; + NumericLiteral;[9]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["less than 10"]; + NewLine;[LF]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val2]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + StringLiteral;["value2"]; + Semicolon;[;]; + NewLine;[LF]; + RightBrace;[}]; RazorMetaCode - [95..95)::0 - Gen RightBrace;[]; EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt index 60892a58bad..6c30f50644f 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.cspans.txt @@ -1,9 +1,6 @@ Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [141] ) Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [141] ) MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [141] ) -Code span at (2:0,2 [33] ) - Parent: Statement block at (0:0,0 [141] ) -Markup span at (35:3,0 [8] ) - Parent: Markup block at (35:3,0 [106] ) -Markup span at (43:3,8 [1] ) - Parent: Tag block at (43:3,8 [6] ) -Markup span at (44:3,9 [4] ) - Parent: Markup block at (44:3,9 [4] ) -Markup span at (48:3,13 [1] ) - Parent: Tag block at (43:3,8 [6] ) -Markup span at (49:3,14 [92] ) - Parent: Markup block at (35:3,0 [106] ) +Code span at (2:0,2 [138] ) - Parent: Statement block at (0:0,0 [141] ) +MetaCode span at (140:7,0 [1] ) - Parent: Statement block at (0:0,0 [141] ) +Markup span at (141:7,1 [0] ) - Parent: Markup block at (0:0,0 [141] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt deleted file mode 100644 index c692e50ed03..00000000000 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.diag.txt +++ /dev/null @@ -1,2 +0,0 @@ -(1,2): Error RZ1006: The code block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup. -(4,10): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag. diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt index c47690f82d6..26350dfd805 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMultipleComparisons.stree.txt @@ -9,8 +9,8 @@ CSharpStatementBody - [1..141)::140 RazorMetaCode - [1..2)::1 - Gen LeftBrace;[{]; - CSharpCodeBlock - [2..141)::139 - CSharpStatementLiteral - [2..35)::33 - [LF var val = 0 switchLF {LF] - Gen + CSharpCodeBlock - [2..140)::138 + CSharpStatementLiteral - [2..140)::138 - [LF var val = 0 switchLF {LF < 9 => "less than 10",LF 10 => "equal to 10",LF > 10 => "greater than 10"LF };LF] - Gen NewLine;[LF]; Whitespace;[ ]; Keyword;[var]; @@ -26,68 +26,39 @@ Whitespace;[ ]; LeftBrace;[{]; NewLine;[LF]; - MarkupBlock - [35..141)::106 - MarkupTextLiteral - [35..43)::8 - [ ] - Gen - Whitespace;[ ]; - MarkupElement - [43..141)::98 - MarkupStartTag - [43..49)::6 - [< 9 =>] - Gen - OpenAngle;[<]; - Text;[]; - MarkupAttributeBlock - [44..48)::4 - [ 9 =] - MarkupTextLiteral - [44..45)::1 - [ ] - Gen - Whitespace;[ ]; - MarkupTextLiteral - [45..46)::1 - [9] - Gen - Text;[9]; - MarkupTextLiteral - [46..47)::1 - [ ] - Gen - Whitespace;[ ]; - Equals;[=]; - CloseAngle;[>]; - MarkupTextLiteral - [49..141)::92 - [ "less than 10",LF 10 => "equal to 10",LF > 10 => "greater than 10"LF };LF}] - Gen - Whitespace;[ ]; - DoubleQuote;["]; - Text;[less]; - Whitespace;[ ]; - Text;[than]; - Whitespace;[ ]; - Text;[10]; - DoubleQuote;["]; - Text;[,]; - NewLine;[LF]; - Whitespace;[ ]; - Text;[10]; - Whitespace;[ ]; - Equals;[=]; - CloseAngle;[>]; - Whitespace;[ ]; - DoubleQuote;["]; - Text;[equal]; - Whitespace;[ ]; - Text;[to]; - Whitespace;[ ]; - Text;[10]; - DoubleQuote;["]; - Text;[,]; - NewLine;[LF]; - Whitespace;[ ]; - CloseAngle;[>]; - Whitespace;[ ]; - Text;[10]; - Whitespace;[ ]; - Equals;[=]; - CloseAngle;[>]; - Whitespace;[ ]; - DoubleQuote;["]; - Text;[greater]; - Whitespace;[ ]; - Text;[than]; - Whitespace;[ ]; - Text;[10]; - DoubleQuote;["]; - NewLine;[LF]; - Whitespace;[ ]; - Text;[};]; - NewLine;[LF]; - Text;[}]; - RazorMetaCode - [141..141)::0 - Gen - RightBrace;[]; + Whitespace;[ ]; + LessThan;[<]; + Whitespace;[ ]; + NumericLiteral;[9]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["less than 10"]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["equal to 10"]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + GreaterThan;[>]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["greater than 10"]; + NewLine;[LF]; + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [140..141)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [141..141)::0 - [] - Gen + Marker;[]; EndOfFile;[]; From 1751716c14f8f3ab3407026b586f9e5fc5c85338 Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Mon, 17 Nov 2025 12:49:53 -0800 Subject: [PATCH 4/7] Add test with markup --- .../test/Legacy/CSharpBlockTest.cs | 14 ++++ ...itchExpression_WithMarkupInside.cspans.txt | 6 ++ ...witchExpression_WithMarkupInside.stree.txt | 68 +++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs index e7b58d5df3a..fdb4d7caa56 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs @@ -1040,6 +1040,20 @@ public void SwitchExpression_WithWrongKeyword_AndLessThan() """); } + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithMarkupInside() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + 0 => some html, + _ => "value" + }; + } + """); + } + private void RunRazorCommentBetweenClausesTest(string preComment, string postComment, AcceptedCharactersInternal acceptedCharacters = AcceptedCharactersInternal.Any) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt new file mode 100644 index 00000000000..03865a3f1fa --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt @@ -0,0 +1,6 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [112] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [112] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [112] ) +Code span at (2:0,2 [109] ) - Parent: Statement block at (0:0,0 [112] ) +MetaCode span at (111:6,0 [1] ) - Parent: Statement block at (0:0,0 [112] ) +Markup span at (112:6,1 [0] ) - Parent: Markup block at (0:0,0 [112] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt new file mode 100644 index 00000000000..5f0eb4dcef7 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt @@ -0,0 +1,68 @@ +RazorDocument - [0..112)::112 - [@{LF var val = 0 switchLF {LF 0 => some html,LF _ => "value"LF };LF}] + MarkupBlock - [0..112)::112 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..112)::112 + CSharpStatement - [0..112)::112 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..112)::111 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..111)::109 + CSharpStatementLiteral - [2..111)::109 - [LF var val = 0 switchLF {LF 0 => some html,LF _ => "value"LF };LF] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + LessThan;[<]; + Identifier;[span]; + GreaterThan;[>]; + Identifier;[some]; + Whitespace;[ ]; + LessThan;[<]; + Identifier;[i]; + GreaterThan;[>]; + Identifier;[html]; + LessThan;[<]; + CSharpOperator;[/]; + Identifier;[i]; + GreaterThan;[>]; + LessThan;[<]; + CSharpOperator;[/]; + Identifier;[span]; + GreaterThan;[>]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[_]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["value"]; + NewLine;[LF]; + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [111..112)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [112..112)::0 - [] - Gen + Marker;[]; + EndOfFile;[]; From 02a6a68021542f9fb99296d4480abef330d7ff30 Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Mon, 17 Nov 2025 16:34:38 -0800 Subject: [PATCH 5/7] Add tests for switch expressions in explicit expressions --- .../test/Legacy/CSharpBlockTest.cs | 2 - .../Legacy/CSharpExplicitExpressionTest.cs | 47 +++++++++++ .../SwitchExpression.cspans.txt | 10 +++ .../SwitchExpression.stree.txt | 71 ++++++++++++++++ .../SwitchExpression_WithHtml.cspans.txt | 10 +++ .../SwitchExpression_WithHtml.stree.txt | 78 ++++++++++++++++++ .../SwitchExpression_WithLessThan.cspans.txt | 10 +++ .../SwitchExpression_WithLessThan.stree.txt | 81 +++++++++++++++++++ 8 files changed, 307 insertions(+), 2 deletions(-) create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.stree.txt diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs index fdb4d7caa56..d5b5c770112 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs @@ -1054,8 +1054,6 @@ public void SwitchExpression_WithMarkupInside() """); } - - private void RunRazorCommentBetweenClausesTest(string preComment, string postComment, AcceptedCharactersInternal acceptedCharacters = AcceptedCharactersInternal.Any) { ParseDocumentTest(preComment + "@* Foo *@ @* Bar *@" + postComment); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpExplicitExpressionTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpExplicitExpressionTest.cs index 9dd2aafc484..03775950c01 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpExplicitExpressionTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpExplicitExpressionTest.cs @@ -4,6 +4,7 @@ #nullable disable using System; +using Roslyn.Test.Utilities; using Xunit; namespace Microsoft.AspNetCore.Razor.Language.Legacy; @@ -76,4 +77,50 @@ public void ShouldAcceptConsecutiveEscapedQuotesInVerbatimStrings() { ParseDocumentTest("@(@\"\"\"\"\"\")"); } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression() + { + ParseDocumentTest(""" + @(value switch{ + 10 => "ten", + _ => "other" + }) + + @code{ + public int value = 10; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithLessThan() + { + ParseDocumentTest(""" + @(value switch{ + < 10 => "less than", + 10 => "ten", + _ => "other" + }) + + @code{ + public int value = 10; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithHtml() + { + ParseDocumentTest(""" + @(value switch{ + 10 => ten, + _ => "other" + }) + + @code{ + public int value = 10; + } + """); + } } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.cspans.txt new file mode 100644 index 00000000000..36f15ecb9d1 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.cspans.txt @@ -0,0 +1,10 @@ +Markup span at (0:0,0 [6] ) - Parent: Tag block at (0:0,0 [6] ) +Transition span at (6:0,6 [1] ) - Parent: Expression block at (6:0,6 [55] ) +MetaCode span at (7:0,7 [1] ) - Parent: Expression block at (6:0,6 [55] ) +Code span at (8:0,8 [52] ) - Parent: Expression block at (6:0,6 [55] ) +MetaCode span at (60:3,1 [1] ) - Parent: Expression block at (6:0,6 [55] ) +Markup span at (61:3,2 [7] ) - Parent: Tag block at (61:3,2 [7] ) +Markup span at (68:3,9 [4] ) - Parent: Markup block at (0:0,0 [109] ) +Transition span at (72:5,0 [1] ) - Parent: Expression block at (72:5,0 [5] ) +Code span at (73:5,1 [4] ) - Parent: Expression block at (72:5,0 [5] ) +Markup span at (77:5,5 [32] ) - Parent: Markup block at (0:0,0 [109] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.stree.txt new file mode 100644 index 00000000000..55d1ff488cb --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression.stree.txt @@ -0,0 +1,71 @@ +RazorDocument - [0..109)::109 - [@(value switch{LF 10 => "ten",LF _ => "other"LF})LFLF@code{LF public int value = 10;LF}] + MarkupBlock - [0..109)::109 + MarkupElement - [0..68)::68 + MarkupStartTag - [0..6)::6 - [] - Gen + OpenAngle;[<]; + Text;[span]; + CloseAngle;[>]; + CSharpCodeBlock - [6..61)::55 + CSharpExplicitExpression - [6..61)::55 + CSharpTransition - [6..7)::1 - Gen + Transition;[@]; + CSharpExplicitExpressionBody - [7..61)::54 + RazorMetaCode - [7..8)::1 - Gen + LeftParenthesis;[(]; + CSharpCodeBlock - [8..60)::52 + CSharpExpressionLiteral - [8..60)::52 - [value switch{LF 10 => "ten",LF _ => "other"LF}] - Gen + Identifier;[value]; + Whitespace;[ ]; + Keyword;[switch]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["ten"]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[_]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["other"]; + NewLine;[LF]; + RightBrace;[}]; + RazorMetaCode - [60..61)::1 - Gen + RightParenthesis;[)]; + MarkupEndTag - [61..68)::7 - [] - Gen + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [68..72)::4 - [LFLF] - Gen + NewLine;[LF]; + NewLine;[LF]; + CSharpCodeBlock - [72..77)::5 + CSharpImplicitExpression - [72..77)::5 + CSharpTransition - [72..73)::1 - Gen + Transition;[@]; + CSharpImplicitExpressionBody - [73..77)::4 + CSharpCodeBlock - [73..77)::4 + CSharpExpressionLiteral - [73..77)::4 - [code] - Gen + Identifier;[code]; + MarkupTextLiteral - [77..109)::32 - [{LF public int value = 10;LF}] - Gen + Text;[{]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[public]; + Whitespace;[ ]; + Text;[int]; + Whitespace;[ ]; + Text;[value]; + Whitespace;[ ]; + Equals;[=]; + Whitespace;[ ]; + Text;[10;]; + NewLine;[LF]; + Text;[}]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.cspans.txt new file mode 100644 index 00000000000..eb6c6326224 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.cspans.txt @@ -0,0 +1,10 @@ +Markup span at (0:0,0 [6] ) - Parent: Tag block at (0:0,0 [6] ) +Transition span at (6:0,6 [1] ) - Parent: Expression block at (6:0,6 [66] ) +MetaCode span at (7:0,7 [1] ) - Parent: Expression block at (6:0,6 [66] ) +Code span at (8:0,8 [63] ) - Parent: Expression block at (6:0,6 [66] ) +MetaCode span at (71:3,1 [1] ) - Parent: Expression block at (6:0,6 [66] ) +Markup span at (72:3,2 [7] ) - Parent: Tag block at (72:3,2 [7] ) +Markup span at (79:3,9 [4] ) - Parent: Markup block at (0:0,0 [120] ) +Transition span at (83:5,0 [1] ) - Parent: Expression block at (83:5,0 [5] ) +Code span at (84:5,1 [4] ) - Parent: Expression block at (83:5,0 [5] ) +Markup span at (88:5,5 [32] ) - Parent: Markup block at (0:0,0 [120] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.stree.txt new file mode 100644 index 00000000000..498c07c5c23 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithHtml.stree.txt @@ -0,0 +1,78 @@ +RazorDocument - [0..120)::120 - [@(value switch{LF 10 => ten,LF _ => "other"LF})LFLF@code{LF public int value = 10;LF}] + MarkupBlock - [0..120)::120 + MarkupElement - [0..79)::79 + MarkupStartTag - [0..6)::6 - [] - Gen + OpenAngle;[<]; + Text;[span]; + CloseAngle;[>]; + CSharpCodeBlock - [6..72)::66 + CSharpExplicitExpression - [6..72)::66 + CSharpTransition - [6..7)::1 - Gen + Transition;[@]; + CSharpExplicitExpressionBody - [7..72)::65 + RazorMetaCode - [7..8)::1 - Gen + LeftParenthesis;[(]; + CSharpCodeBlock - [8..71)::63 + CSharpExpressionLiteral - [8..71)::63 - [value switch{LF 10 => ten,LF _ => "other"LF}] - Gen + Identifier;[value]; + Whitespace;[ ]; + Keyword;[switch]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + LessThan;[<]; + Identifier;[span]; + GreaterThan;[>]; + Identifier;[ten]; + LessThan;[<]; + CSharpOperator;[/]; + Identifier;[span]; + GreaterThan;[>]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[_]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["other"]; + NewLine;[LF]; + RightBrace;[}]; + RazorMetaCode - [71..72)::1 - Gen + RightParenthesis;[)]; + MarkupEndTag - [72..79)::7 - [] - Gen + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [79..83)::4 - [LFLF] - Gen + NewLine;[LF]; + NewLine;[LF]; + CSharpCodeBlock - [83..88)::5 + CSharpImplicitExpression - [83..88)::5 + CSharpTransition - [83..84)::1 - Gen + Transition;[@]; + CSharpImplicitExpressionBody - [84..88)::4 + CSharpCodeBlock - [84..88)::4 + CSharpExpressionLiteral - [84..88)::4 - [code] - Gen + Identifier;[code]; + MarkupTextLiteral - [88..120)::32 - [{LF public int value = 10;LF}] - Gen + Text;[{]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[public]; + Whitespace;[ ]; + Text;[int]; + Whitespace;[ ]; + Text;[value]; + Whitespace;[ ]; + Equals;[=]; + Whitespace;[ ]; + Text;[10;]; + NewLine;[LF]; + Text;[}]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.cspans.txt new file mode 100644 index 00000000000..3fda7945558 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.cspans.txt @@ -0,0 +1,10 @@ +Markup span at (0:0,0 [6] ) - Parent: Tag block at (0:0,0 [6] ) +Transition span at (6:0,6 [1] ) - Parent: Expression block at (6:0,6 [81] ) +MetaCode span at (7:0,7 [1] ) - Parent: Expression block at (6:0,6 [81] ) +Code span at (8:0,8 [78] ) - Parent: Expression block at (6:0,6 [81] ) +MetaCode span at (86:4,1 [1] ) - Parent: Expression block at (6:0,6 [81] ) +Markup span at (87:4,2 [7] ) - Parent: Tag block at (87:4,2 [7] ) +Markup span at (94:4,9 [4] ) - Parent: Markup block at (0:0,0 [135] ) +Transition span at (98:6,0 [1] ) - Parent: Expression block at (98:6,0 [5] ) +Code span at (99:6,1 [4] ) - Parent: Expression block at (98:6,0 [5] ) +Markup span at (103:6,5 [32] ) - Parent: Markup block at (0:0,0 [135] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.stree.txt new file mode 100644 index 00000000000..df30587b92d --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpExplicitExpressionTest/SwitchExpression_WithLessThan.stree.txt @@ -0,0 +1,81 @@ +RazorDocument - [0..135)::135 - [@(value switch{LF < 10 => "less than",LF 10 => "ten",LF _ => "other"LF})LFLF@code{LF public int value = 10;LF}] + MarkupBlock - [0..135)::135 + MarkupElement - [0..94)::94 + MarkupStartTag - [0..6)::6 - [] - Gen + OpenAngle;[<]; + Text;[span]; + CloseAngle;[>]; + CSharpCodeBlock - [6..87)::81 + CSharpExplicitExpression - [6..87)::81 + CSharpTransition - [6..7)::1 - Gen + Transition;[@]; + CSharpExplicitExpressionBody - [7..87)::80 + RazorMetaCode - [7..8)::1 - Gen + LeftParenthesis;[(]; + CSharpCodeBlock - [8..86)::78 + CSharpExpressionLiteral - [8..86)::78 - [value switch{LF < 10 => "less than",LF 10 => "ten",LF _ => "other"LF}] - Gen + Identifier;[value]; + Whitespace;[ ]; + Keyword;[switch]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + LessThan;[<]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["less than"]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["ten"]; + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[_]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + StringLiteral;["other"]; + NewLine;[LF]; + RightBrace;[}]; + RazorMetaCode - [86..87)::1 - Gen + RightParenthesis;[)]; + MarkupEndTag - [87..94)::7 - [] - Gen + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [94..98)::4 - [LFLF] - Gen + NewLine;[LF]; + NewLine;[LF]; + CSharpCodeBlock - [98..103)::5 + CSharpImplicitExpression - [98..103)::5 + CSharpTransition - [98..99)::1 - Gen + Transition;[@]; + CSharpImplicitExpressionBody - [99..103)::4 + CSharpCodeBlock - [99..103)::4 + CSharpExpressionLiteral - [99..103)::4 - [code] - Gen + Identifier;[code]; + MarkupTextLiteral - [103..135)::32 - [{LF public int value = 10;LF}] - Gen + Text;[{]; + NewLine;[LF]; + Whitespace;[ ]; + Text;[public]; + Whitespace;[ ]; + Text;[int]; + Whitespace;[ ]; + Text;[value]; + Whitespace;[ ]; + Equals;[=]; + Whitespace;[ ]; + Text;[10;]; + NewLine;[LF]; + Text;[}]; + EndOfFile;[]; From 1f83024f78f9665ef5f1d24206b0121ca25dbb7c Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Tue, 18 Nov 2025 16:26:13 -0800 Subject: [PATCH 6/7] Add extra tests for @ --- .../test/Legacy/CSharpBlockTest.cs | 30 +++++- ...on_WithMarkupInside_ViaAtSymbol.cspans.txt | 17 ++++ ...ion_WithMarkupInside_ViaAtSymbol.stree.txt | 85 +++++++++++++++++ ...n_WithMarkupInside_WithLessThan.cspans.txt | 17 ++++ ...on_WithMarkupInside_WithLessThan.stree.txt | 91 +++++++++++++++++++ 5 files changed, 239 insertions(+), 1 deletion(-) create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.stree.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.cspans.txt create mode 100644 src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.stree.txt diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs index d5b5c770112..59dd15d9e23 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Legacy/CSharpBlockTest.cs @@ -1047,13 +1047,41 @@ public void SwitchExpression_WithMarkupInside() @{ var val = 0 switch { - 0 => some html, + 0 => some html, _ => "value" }; } """); } + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithMarkupInside_ViaAtSymbol() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + 0 => @zero, + _ => @one + }; + } + """); + } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/7230")] + public void SwitchExpression_WithMarkupInside_WithLessThan() + { + ParseDocumentTest(""" + @{ + var val = 0 switch + { + < 10 => @less than 10, + _ => @other + }; + } + """); + } + private void RunRazorCommentBetweenClausesTest(string preComment, string postComment, AcceptedCharactersInternal acceptedCharacters = AcceptedCharactersInternal.Any) { ParseDocumentTest(preComment + "@* Foo *@ @* Bar *@" + postComment); diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.cspans.txt new file mode 100644 index 00000000000..7ec01ca859a --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.cspans.txt @@ -0,0 +1,17 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [110] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [110] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [110] ) +Code span at (2:0,2 [46] ) - Parent: Statement block at (0:0,0 [110] ) +Transition span at (48:3,13 [1] ) - Parent: Markup block at (48:3,13 [18] ) +Markup span at (49:3,14 [6] ) - Parent: Tag block at (49:3,14 [6] ) +Markup span at (55:3,20 [4] ) - Parent: Markup block at (48:3,13 [18] ) +Markup span at (59:3,24 [7] ) - Parent: Tag block at (59:3,24 [7] ) +Code span at (66:3,31 [16] ) - Parent: Statement block at (0:0,0 [110] ) +Transition span at (82:4,13 [1] ) - Parent: Markup block at (82:4,13 [19] ) +Markup span at (83:4,14 [6] ) - Parent: Tag block at (83:4,14 [6] ) +Markup span at (89:4,20 [3] ) - Parent: Markup block at (82:4,13 [19] ) +Markup span at (92:4,23 [7] ) - Parent: Tag block at (92:4,23 [7] ) +Markup span at (99:4,30 [2] ) - Parent: Markup block at (82:4,13 [19] ) +Code span at (101:5,0 [8] ) - Parent: Statement block at (0:0,0 [110] ) +MetaCode span at (109:6,0 [1] ) - Parent: Statement block at (0:0,0 [110] ) +Markup span at (110:6,1 [0] ) - Parent: Markup block at (0:0,0 [110] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.stree.txt new file mode 100644 index 00000000000..7e71ccb5aba --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_ViaAtSymbol.stree.txt @@ -0,0 +1,85 @@ +RazorDocument - [0..110)::110 - [@{LF var val = 0 switchLF {LF 0 => @zero,LF _ => @oneLF };LF}] + MarkupBlock - [0..110)::110 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..110)::110 + CSharpStatement - [0..110)::110 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..110)::109 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..109)::107 + CSharpStatementLiteral - [2..48)::46 - [LF var val = 0 switchLF {LF 0 => ] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + CSharpTemplateBlock - [48..66)::18 + MarkupBlock - [48..66)::18 + MarkupTransition - [48..49)::1 - Gen + Transition;[@]; + MarkupElement - [49..66)::17 + MarkupStartTag - [49..55)::6 - [] - Gen + OpenAngle;[<]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [55..59)::4 - [zero] - Gen + Text;[zero]; + MarkupEndTag - [59..66)::7 - [] - Gen + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[span]; + CloseAngle;[>]; + CSharpStatementLiteral - [66..82)::16 - [,LF _ => ] - Gen + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[_]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + CSharpTemplateBlock - [82..101)::19 + MarkupBlock - [82..101)::19 + MarkupTransition - [82..83)::1 - Gen + Transition;[@]; + MarkupElement - [83..99)::16 + MarkupStartTag - [83..89)::6 - [] - Gen + OpenAngle;[<]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [89..92)::3 - [one] - Gen + Text;[one]; + MarkupEndTag - [92..99)::7 - [] - Gen + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [99..101)::2 - [LF] - Gen + NewLine;[LF]; + CSharpStatementLiteral - [101..109)::8 - [ };LF] - Gen + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [109..110)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [110..110)::0 - [] - Gen + Marker;[]; + EndOfFile;[]; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.cspans.txt new file mode 100644 index 00000000000..9abc93e8674 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.cspans.txt @@ -0,0 +1,17 @@ +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [123] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [123] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [123] ) +Code span at (2:0,2 [49] ) - Parent: Statement block at (0:0,0 [123] ) +Transition span at (51:3,16 [1] ) - Parent: Markup block at (51:3,16 [26] ) +Markup span at (52:3,17 [6] ) - Parent: Tag block at (52:3,17 [6] ) +Markup span at (58:3,23 [12] ) - Parent: Markup block at (51:3,16 [26] ) +Markup span at (70:3,35 [7] ) - Parent: Tag block at (70:3,35 [7] ) +Code span at (77:3,42 [16] ) - Parent: Statement block at (0:0,0 [123] ) +Transition span at (93:4,13 [1] ) - Parent: Markup block at (93:4,13 [21] ) +Markup span at (94:4,14 [6] ) - Parent: Tag block at (94:4,14 [6] ) +Markup span at (100:4,20 [5] ) - Parent: Markup block at (93:4,13 [21] ) +Markup span at (105:4,25 [7] ) - Parent: Tag block at (105:4,25 [7] ) +Markup span at (112:4,32 [2] ) - Parent: Markup block at (93:4,13 [21] ) +Code span at (114:5,0 [8] ) - Parent: Statement block at (0:0,0 [123] ) +MetaCode span at (122:6,0 [1] ) - Parent: Statement block at (0:0,0 [123] ) +Markup span at (123:6,1 [0] ) - Parent: Markup block at (0:0,0 [123] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.stree.txt new file mode 100644 index 00000000000..c77d1d81932 --- /dev/null +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside_WithLessThan.stree.txt @@ -0,0 +1,91 @@ +RazorDocument - [0..123)::123 - [@{LF var val = 0 switchLF {LF < 10 => @less than 10,LF _ => @otherLF };LF}] + MarkupBlock - [0..123)::123 + MarkupTextLiteral - [0..0)::0 - [] - Gen + Marker;[]; + CSharpCodeBlock - [0..123)::123 + CSharpStatement - [0..123)::123 + CSharpTransition - [0..1)::1 - Gen + Transition;[@]; + CSharpStatementBody - [1..123)::122 + RazorMetaCode - [1..2)::1 - Gen + LeftBrace;[{]; + CSharpCodeBlock - [2..122)::120 + CSharpStatementLiteral - [2..51)::49 - [LF var val = 0 switchLF {LF < 10 => ] - Gen + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[var]; + Whitespace;[ ]; + Identifier;[val]; + Whitespace;[ ]; + Assign;[=]; + Whitespace;[ ]; + NumericLiteral;[0]; + Whitespace;[ ]; + Keyword;[switch]; + NewLine;[LF]; + Whitespace;[ ]; + LeftBrace;[{]; + NewLine;[LF]; + Whitespace;[ ]; + LessThan;[<]; + Whitespace;[ ]; + NumericLiteral;[10]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + CSharpTemplateBlock - [51..77)::26 + MarkupBlock - [51..77)::26 + MarkupTransition - [51..52)::1 - Gen + Transition;[@]; + MarkupElement - [52..77)::25 + MarkupStartTag - [52..58)::6 - [] - Gen + OpenAngle;[<]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [58..70)::12 - [less than 10] - Gen + Text;[less]; + Whitespace;[ ]; + Text;[than]; + Whitespace;[ ]; + Text;[10]; + MarkupEndTag - [70..77)::7 - [] - Gen + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[span]; + CloseAngle;[>]; + CSharpStatementLiteral - [77..93)::16 - [,LF _ => ] - Gen + Comma;[,]; + NewLine;[LF]; + Whitespace;[ ]; + Keyword;[_]; + Whitespace;[ ]; + CSharpOperator;[=>]; + Whitespace;[ ]; + CSharpTemplateBlock - [93..114)::21 + MarkupBlock - [93..114)::21 + MarkupTransition - [93..94)::1 - Gen + Transition;[@]; + MarkupElement - [94..112)::18 + MarkupStartTag - [94..100)::6 - [] - Gen + OpenAngle;[<]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [100..105)::5 - [other] - Gen + Text;[other]; + MarkupEndTag - [105..112)::7 - [] - Gen + OpenAngle;[<]; + ForwardSlash;[/]; + Text;[span]; + CloseAngle;[>]; + MarkupTextLiteral - [112..114)::2 - [LF] - Gen + NewLine;[LF]; + CSharpStatementLiteral - [114..122)::8 - [ };LF] - Gen + Whitespace;[ ]; + RightBrace;[}]; + Semicolon;[;]; + NewLine;[LF]; + RazorMetaCode - [122..123)::1 - Gen + RightBrace;[}]; + MarkupTextLiteral - [123..123)::0 - [] - Gen + Marker;[]; + EndOfFile;[]; From c33668d8091e34cc4a7cef094f49101e3f5eb2df Mon Sep 17 00:00:00 2001 From: Chris Sienkiewicz Date: Tue, 18 Nov 2025 16:44:59 -0800 Subject: [PATCH 7/7] Fix baseline due to fixed test --- ...itchExpression_WithMarkupInside.cspans.txt | 12 +++++------ ...witchExpression_WithMarkupInside.stree.txt | 20 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt index 03865a3f1fa..4731ba0bbe5 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.cspans.txt @@ -1,6 +1,6 @@ -Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [112] ) -Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [112] ) -MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [112] ) -Code span at (2:0,2 [109] ) - Parent: Statement block at (0:0,0 [112] ) -MetaCode span at (111:6,0 [1] ) - Parent: Statement block at (0:0,0 [112] ) -Markup span at (112:6,1 [0] ) - Parent: Markup block at (0:0,0 [112] ) +Markup span at (0:0,0 [0] ) - Parent: Markup block at (0:0,0 [111] ) +Transition span at (0:0,0 [1] ) - Parent: Statement block at (0:0,0 [111] ) +MetaCode span at (1:0,1 [1] ) - Parent: Statement block at (0:0,0 [111] ) +Code span at (2:0,2 [108] ) - Parent: Statement block at (0:0,0 [111] ) +MetaCode span at (110:6,0 [1] ) - Parent: Statement block at (0:0,0 [111] ) +Markup span at (111:6,1 [0] ) - Parent: Markup block at (0:0,0 [111] ) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt index 5f0eb4dcef7..3bd30db8269 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/ParserTests/CSharpBlockTest/SwitchExpression_WithMarkupInside.stree.txt @@ -1,16 +1,16 @@ -RazorDocument - [0..112)::112 - [@{LF var val = 0 switchLF {LF 0 => some html,LF _ => "value"LF };LF}] - MarkupBlock - [0..112)::112 +RazorDocument - [0..111)::111 - [@{LF var val = 0 switchLF {LF 0 => some html,LF _ => "value"LF };LF}] + MarkupBlock - [0..111)::111 MarkupTextLiteral - [0..0)::0 - [] - Gen Marker;[]; - CSharpCodeBlock - [0..112)::112 - CSharpStatement - [0..112)::112 + CSharpCodeBlock - [0..111)::111 + CSharpStatement - [0..111)::111 CSharpTransition - [0..1)::1 - Gen Transition;[@]; - CSharpStatementBody - [1..112)::111 + CSharpStatementBody - [1..111)::110 RazorMetaCode - [1..2)::1 - Gen LeftBrace;[{]; - CSharpCodeBlock - [2..111)::109 - CSharpStatementLiteral - [2..111)::109 - [LF var val = 0 switchLF {LF 0 => some html,LF _ => "value"LF };LF] - Gen + CSharpCodeBlock - [2..110)::108 + CSharpStatementLiteral - [2..110)::108 - [LF var val = 0 switchLF {LF 0 => some html,LF _ => "value"LF };LF] - Gen NewLine;[LF]; Whitespace;[ ]; Keyword;[var]; @@ -30,7 +30,7 @@ NumericLiteral;[0]; Whitespace;[ ]; CSharpOperator;[=>]; - Whitespace;[ ]; + Whitespace;[ ]; LessThan;[<]; Identifier;[span]; GreaterThan;[>]; @@ -61,8 +61,8 @@ RightBrace;[}]; Semicolon;[;]; NewLine;[LF]; - RazorMetaCode - [111..112)::1 - Gen + RazorMetaCode - [110..111)::1 - Gen RightBrace;[}]; - MarkupTextLiteral - [112..112)::0 - [] - Gen + MarkupTextLiteral - [111..111)::0 - [] - Gen Marker;[]; EndOfFile;[];