dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
500 stars 191 forks source link

Relational Patterns are confused with tags in Razor #7230

Open analogrelay opened 2 years ago

analogrelay commented 2 years ago

Is there an existing issue for this?

Describe the bug

When building a razor page, many of the C# pattern matching features work just fine. However, using "Relational Patterns" seems to cause an issue. For example, consider the following page:

    @{
        var i = 0;
        var val = i switch {
            < 10 => "less than 10",
            10 => "10",
            > 10 => "more than 10",
        };
    }
    <span>There are @val items.</span>

This produces a series of compilation errors:

One or more compilation failures occurred:
SNIP/Test.cshtml(14,2): Error RZ1034: Found a malformed 'body' tag helper. Tag helpers must have a start and end tag or be self closing.
SNIP/Test.cshtml(16,6): 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.
SNIP/Test.cshtml(19,14): Error RZ1025: The "" element was not closed. All elements must be either self-closing or have a matching end tag.
SNIP/Test.cshtml(26,3): Error RZ1026: Encountered end tag "body" with no matching start tag. Are your start/end tags properly balanced?
SNIP/Test.cshtml(26,3): Error RZ1034: Found a malformed 'body' tag helper. Tag helpers must have a start and end tag or be self closing.
SNIP/Test.cshtml(27,3): Error RZ1026: Encountered end tag "html" with no matching start tag. Are your start/end tags properly balanced?

From this, it appears that the < 10 syntax is being interpreted as an HTML tag. That's not terribly surprising given the way Razor allows HTML tags to appear in line with code, but generally it doesn't permit HTML tags outside of a "statement" position. The pattern in a switch expression shouldn't be a place in which HTML would be allowed, so I'd expect Razor to ignore HTML markup here.

This isn't completely blocking though, one can workaround the issue by wrapping the pattern in ():

    @{
        var i = 0;
        var val = i switch {
            (< 10) => "less than 10",
            10 => "10",
            (> 10) => "more than 10",
        };
    }
    <span>There are @val items.</span>

Expected Behavior

I'd expect valid C# code to be usable in a Razor code block, or for a specific error describing the limitation ("relational patterns cannot be used in Razor") be produced.

Steps To Reproduce

  1. Create the following page in any ASP.NET Core Razor Pages app:
    @{
        var i = 0;
        var val = i switch {
            < 10 => "less than 10",
            10 => "10",
            > 10 => "more than 10",
        };
    }
    <span>There are @val items.</span>
  1. Run the page

Exceptions (if any)

No response

.NET Version

6.0.103

Anything else?

No response

TanayParikh commented 2 years ago

Thanks @anurse! Transferring to the razor-compiler repo.