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
501 stars 191 forks source link

[Blazor] Semi-colon inside @() raises syntax errors #10701

Open Joe-Lonsdale opened 3 months ago

Joe-Lonsdale commented 3 months ago

Is there an existing issue for this?

Describe the bug

Inside a razor page:

<p class="@(GetClass())">foo</p>

This works as expected.

<p class="@(GetClass();)">bar</p>

This raises CS0116 and CS0115 errors. I don't know if this is intended behaviour, but the errors thrown aren't very indicative of the error at hand.

Expected Behavior

Not entirely sure, either a better error message or for it to compile properly.

Steps To Reproduce

See description above.

Exceptions (if any)

CS0115, CS0116

.NET Version

8.0.302

Anything else?

No response

javiercn commented 3 months ago

@Joe-Lonsdale thanks for contacting us.

That's by design. @() only accepts expressions, which means you can't use the semi-colon to add statements. If you need to emit a block of code, you can use @{}, assign a variable and then use the variable within @() (or @variable directly)

chsienki commented 3 months ago

Behavior is intended but agree we should have a razor error here if possible.

chsienki commented 3 months ago

Related: https://github.com/dotnet/razor/issues/10383