Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit and map empty csharp tokens #10969

Merged
merged 3 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,34 @@ public class dynamic : ComponentBase
CompileToAssembly(generated);
}

[IntegrationTestFact, WorkItem("https://github.com/dotnet/razor/issues/10965")]
public void InvalidCode_EmptyTransition()
{
// Act
var generated = CompileToCSharp("""
<TestComponent Value="Hello" />

@

@code {
[Parameter] public int Param { get; set; }
}
""");

// Assert
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
CompileToAssembly(generated, DesignTime?[
// x:\dir\subdir\Test\TestComponent.cshtml(3,7): error CS1525: Invalid expression term ';'
// __o = ;
Diagnostic(ErrorCode.ERR_InvalidExprTerm, ";").WithArguments(";").WithLocation(3, 7)
] : [
// (24,36): error CS1525: Invalid expression term ')'
// __builder.AddContent(3,
Diagnostic(ErrorCode.ERR_InvalidExprTerm, "").WithArguments(")").WithLocation(24, 36)
]);
}

#endregion

#region Bind
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#line default
#line hidden
#nullable restore
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 219
private void __RazorDirectiveTokenHelpers__() {
}
#pragma warning restore 219
#pragma warning disable 0414
private static object __o = null;
#pragma warning restore 0414
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__o = "";
__builder.AddAttribute(-1, "ChildContent", (global::Microsoft.AspNetCore.Components.RenderFragment)((__builder2) => {
}
));
#nullable restore
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = typeof(global::Test.TestComponent);

#line default
#line hidden
#nullable disable
#nullable restore
#line 3 "x:\dir\subdir\Test\TestComponent.cshtml"
__o = ;

#line default
#line hidden
#nullable disable
}
#pragma warning restore 1998
#nullable restore
#line 5 "x:\dir\subdir\Test\TestComponent.cshtml"

[Parameter] public int Param { get; set; }

#line default
#line hidden
#nullable disable
}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x:\dir\subdir\Test\TestComponent.cshtml(3,2): Error RZ1003: A space or line break was encountered after the "@" character. Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following "@" with no space in between.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [20] ) - global::System
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [25] ) - global::System.Linq
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
DesignTimeDirective -
CSharpCode -
IntermediateToken - - CSharp - #pragma warning disable 0414
CSharpCode -
IntermediateToken - - CSharp - private static object __o = null;
CSharpCode -
IntermediateToken - - CSharp - #pragma warning restore 0414
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [31] x:\dir\subdir\Test\TestComponent.cshtml) - TestComponent
ComponentAttribute - - Value - - AttributeStructure.DoubleQuotes
HtmlContent - (22:0,22 [5] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (22:0,22 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Hello
HtmlContent - (31:0,31 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (31:0,31 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpExpression - (36:2,1 [0] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (36:2,1 [0] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp -
HtmlContent - (36:2,1 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (36:2,1 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpCode - (47:4,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (47:4,7 [50] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n [Parameter] public int Param { get; set; }\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Source Location: (36:2,1 [0] x:\dir\subdir\Test\TestComponent.cshtml)
||
Generated Location: (1340:39,6 [0] )
||

Source Location: (47:4,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
|
[Parameter] public int Param { get; set; }
|
Generated Location: (1519:48,7 [50] )
|
[Parameter] public int Param { get; set; }
|

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@ protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.
#line hidden
#nullable disable
));
string __formName = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<string>();
string __formName = global::Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.TypeCheck<string>(
#nullable restore
#line (2,56)-(2,57) "x:\dir\subdir\Test\TestComponent.cshtml"


#line default
#line hidden
#nullable disable
);
__builder.AddNamedEvent("onsubmit", __formName);
__builder.CloseElement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ Source Location: (75:1,31 [9] x:\dir\subdir\Test\TestComponent.cshtml)
Generated Location: (1138:30,0 [9] )
|() => { }|

Source Location: (99:1,55 [1] x:\dir\subdir\Test\TestComponent.cshtml)
| |
Generated Location: (1422:39,0 [1] )
| |

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <auto-generated/>
#pragma warning disable 1591
namespace Test
{
#line default
using global::System;
using global::System.Collections.Generic;
using global::System.Linq;
using global::System.Threading.Tasks;
using global::Microsoft.AspNetCore.Components;
#line default
#line hidden
#nullable restore
public partial class TestComponent : global::Microsoft.AspNetCore.Components.ComponentBase
#nullable disable
{
#pragma warning disable 1998
protected override void BuildRenderTree(global::Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder __builder)
{
__builder.OpenComponent<global::Test.TestComponent>(0);
__builder.AddComponentParameter(1, "Value", "Hello");
__builder.CloseComponent();
__builder.AddMarkupContent(2, "\r\n\r\n");
__builder.AddContent(3,
#nullable restore
#line (3,2)-(3,2) "x:\dir\subdir\Test\TestComponent.cshtml"

#line default
#line hidden
#nullable disable
);
}
#pragma warning restore 1998
#nullable restore
#line (5,8)-(7,1) "x:\dir\subdir\Test\TestComponent.cshtml"

[Parameter] public int Param { get; set; }

#line default
#line hidden
#nullable disable

}
}
#pragma warning restore 1591
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
x:\dir\subdir\Test\TestComponent.cshtml(3,2): Error RZ1003: A space or line break was encountered after the "@" character. Only valid identifiers, keywords, comments, "(" and "{" are valid at the start of a code block and they must occur immediately following "@" with no space in between.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Document -
NamespaceDeclaration - - Test
UsingDirective - (3:1,1 [20] ) - global::System
UsingDirective - (26:2,1 [40] ) - global::System.Collections.Generic
UsingDirective - (69:3,1 [25] ) - global::System.Linq
UsingDirective - (97:4,1 [36] ) - global::System.Threading.Tasks
UsingDirective - (136:5,1 [45] ) - global::Microsoft.AspNetCore.Components
ClassDeclaration - - public partial - TestComponent - global::Microsoft.AspNetCore.Components.ComponentBase -
MethodDeclaration - - protected override - void - BuildRenderTree
Component - (0:0,0 [31] x:\dir\subdir\Test\TestComponent.cshtml) - TestComponent
ComponentAttribute - - Value - - AttributeStructure.DoubleQuotes
HtmlContent - (22:0,22 [5] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (22:0,22 [5] x:\dir\subdir\Test\TestComponent.cshtml) - Html - Hello
HtmlContent - (31:0,31 [4] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (31:0,31 [4] x:\dir\subdir\Test\TestComponent.cshtml) - Html - \n\n
CSharpExpression - (36:2,1 [0] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (36:2,1 [0] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp -
CSharpCode - (47:4,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
LazyIntermediateToken - (47:4,7 [50] x:\dir\subdir\Test\TestComponent.cshtml) - CSharp - \n [Parameter] public int Param { get; set; }\n
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
Source Location: (36:2,1 [0] x:\dir\subdir\Test\TestComponent.cshtml)
||
Generated Location: (1005:26,0 [0] )
||

Source Location: (47:4,7 [50] x:\dir\subdir\Test\TestComponent.cshtml)
|
[Parameter] public int Param { get; set; }
|
Generated Location: (1200:35,0 [50] )
|
[Parameter] public int Param { get; set; }
|

Original file line number Diff line number Diff line change
Expand Up @@ -1188,11 +1188,6 @@ private static void WriteAttributeValue(CodeRenderingContext context, IReadOnlyL

private static void WriteCSharpToken(CodeRenderingContext context, IntermediateToken token)
{
if (string.IsNullOrWhiteSpace(token.Content))
{
return;
}

if (token.Source?.FilePath == null)
{
context.CodeWriter.Write(token.Content);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected override void PrepareProjectForFirstOpen(string projectFileName)
base.PrepareProjectForFirstOpen(projectFileName);
}

[IdeFact(Skip = "https://github.com/dotnet/razor/issues/10965")] // Completion is broken in FUSE for sole `@` characters
[IdeFact]
public async Task Completion_DateTime()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Integration tests don't run automatically on PRs, right? Should we enqueue a run manually to verify this test?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ran integration tests on this PR and everything looks good.

{
// We open the Index.razor file, and wait for 3 RazorComponentElement's to be classified, as that
Expand Down