aspnet / Razor

[Archived] Parser and code generator for CSHTML files used in view pages for MVC web apps. Project moved to https://github.com/aspnet/AspNetCore
Apache License 2.0
883 stars 225 forks source link

Need to evaluate whether any C# 6 features will require changes to the Razor parser #44

Closed Eilon closed 9 years ago

Eilon commented 10 years ago

Please don't close this bug until the C#6 features are finalized (which won't be any time soon). We can use this bug to track potential changes we might need to make to Razor. E.g. imagine a new C# 6 feature has some additional @ signs in it somewhere - we'll likely have to adapt to that.

yishaigalatzer commented 10 years ago

@NTaylorMullen let us verify out var in a functions block, and ?. @Foo.FirstOrDefault()?.Something

NTaylorMullen commented 10 years ago

?. breaks rendering.

dougbu commented 10 years ago

please be more specific about the exact way you're using ?. and what gets broken. also does the breakage vary depending on

NTaylorMullen commented 10 years ago

@DateTime?.Now Generates:

Write(DateTime);
WriteLiteral("?.Now");

When it should generate:

Write(DateTime?.Now);
Eilon commented 9 years ago

Also need to support exception filters:

Today with this code:

try
{
    throw new InvalidOperationException();
}
    catch (Exception e) if (e.Message.Contains("Operation is not valid"))
{
}

You get this error

CompilationFailedException: Compilation for 'c:\users\mattgal\documents\visual studio 2015\Projects\WebApplication1\src\WebApplication1\Views\Home\Index.cshtml' failed:
Expected a "{" but found a "if". Block statements must be enclosed in "{" and "}". You cannot use single-statement control-flow statements in CSHTML pages. For example, the following is not allowed:

@if(isLoggedIn)
<p>Hello, @user</p>

Instead, wrap the contents of the block in "{}":

@if(isLoggedIn) {
<p>Hello, @user</p>
} 

From TFS bug 1099131.

NTaylorMullen commented 9 years ago

I investigated and it looks like the only pieces that aren't working are:

  1. Exception Filters
  2. Null-Conditional for expressions (works fine in statement blocks)

/cc @DamianEdwards Do you feel strongly one way or the other about exception filters making it?

Eilon commented 9 years ago

Just making sure these are tested:

_Using Static Members_

using static System.Console;

_Expression-bodied members_

public double Dist => Sqrt(X * X + Y * Y);  

_String interpolation_

$"{p.Name} is {p.Age} years old."  

var @class = "History";
var happiness = 6;
$"I like taking {@class} class? {(happiness > 5).ToString()}."  
NTaylorMullen commented 9 years ago

Status

_Null Conditional Operators_ Breaks by using them in implicit expressions such as @Foo?.Bar, @Foo?[0].

Fixed: 58c0a36200727b449996242ec9976f8f9fd1e612

_Using Static Members_ Breaks in the scenario @Eilon mentioned: @using static System.Console

Fixed: 1879ac642754b5f84e6055580e6fc60e13fa2100

_String Interpolation_ Breaks in the following scenario.

@{
    $"abc{"123\""}.def";
}

Note the \" in combination with the {" from the string interpolation cause the chaos and results in the } being treated as the end of the @{ block.

https://github.com/aspnet/Razor/issues/401

_Exception Filters_ Breaks by using them :trollface:

https://github.com/aspnet/Razor/issues/402

Conclusion

I'm addressing each of these issues (in order mentioned) and associating their fixes with this issue. If needed I can break them into separate issues.

dougbu commented 9 years ago

What about the following features within a c# block?

NTaylorMullen commented 9 years ago

@dougbu Yup, all of those work :smile:

NTaylorMullen commented 9 years ago

Investigated string interpolation and the fix for it is relatively complex. Talked to @DamianEdwards about prioritization and it's not a must-have for beta5. Will file two separate issues for exception filters and string interpolation so I can close this issue out in regards to null conditional operators and static usings once I've completed them.

NTaylorMullen commented 9 years ago

58c0a36200727b449996242ec9976f8f9fd1e612 1879ac642754b5f84e6055580e6fc60e13fa2100