Skip to content
This repository was archived by the owner on Dec 19, 2018. It is now read-only.

Commit 888e6bf

Browse files
N. Taylor Mullenajaybhargavb
N. Taylor Mullen
authored andcommitted
Fix ModelExpression's in section directive blocks.
- Changed `SectionDirectivePass` to move non-token body nodes from the original `DirectiveIntermediateNode` to the `SectionIntermediateNode`. By doing this there's no longer dual references of `SectionIntermediateNode` bodies. - Added MVC tests for current and 1_X extensions.
1 parent 6af519e commit 888e6bf

File tree

8 files changed

+394
-1
lines changed

8 files changed

+394
-1
lines changed

src/Microsoft.AspNetCore.Razor.Language/Extensions/SectionDirectivePass.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,11 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
3535
}
3636
}
3737

38-
for (; i < directive.Node.Children.Count; i++)
38+
while (i != directive.Node.Children.Count)
3939
{
40+
// Move non-token children over to the section node so we don't have double references to children nodes.
4041
section.Children.Add(directive.Node.Children[i]);
42+
directive.Node.Children.RemoveAt(i);
4143
}
4244

4345
directive.InsertAfter(section);

test/Microsoft.AspNetCore.Mvc.Razor.Extensions.Test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ public void Basic_Runtime()
106106
RunRuntimeTest(references);
107107
}
108108

109+
[Fact]
110+
public void Sections_Runtime()
111+
{
112+
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@"
113+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
114+
115+
public class InputTestTagHelper : {typeof(TagHelper).FullName}
116+
{{
117+
public ModelExpression For {{ get; set; }}
118+
}}
119+
");
120+
RunRuntimeTest(references);
121+
}
122+
109123
[Fact]
110124
public void _ViewImports_Runtime()
111125
{
@@ -344,6 +358,24 @@ public void Basic_DesignTime()
344358
RunDesignTimeTest(references);
345359
}
346360

361+
[Fact]
362+
public void Sections_DesignTime()
363+
{
364+
// This is a problem because we don't yet have multitargeting.
365+
var error = "Delegate 'RenderAsyncDelegate' does not take 1 arguments";
366+
367+
var references = CreateCompilationReferences(CurrentMvcShim, appCode: $@"
368+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
369+
370+
public class InputTestTagHelper : {typeof(TagHelper).FullName}
371+
{{
372+
public ModelExpression For {{ get; set; }}
373+
}}
374+
");
375+
376+
RunDesignTimeTest(references, new[] { error });
377+
}
378+
347379
[Fact]
348380
public void _ViewImports_DesignTime()
349381
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
@model DateTime
2+
3+
@addTagHelper "InputTestTagHelper, AppCode"
4+
5+
@{
6+
Layout = "_SectionTestLayout.cshtml";
7+
}
8+
9+
<div>Some body</div>
10+
11+
@section Section1 {
12+
<div>This is in Section 1</div>
13+
<input-test for="Date" />
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// <auto-generated/>
2+
#pragma warning disable 1591
3+
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml))]
4+
namespace AspNetCore
5+
{
6+
#line hidden
7+
using TModel = global::System.Object;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Threading.Tasks;
12+
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.AspNetCore.Mvc.Rendering;
14+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
15+
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<DateTime>
16+
{
17+
private global::InputTestTagHelper __InputTestTagHelper;
18+
#pragma warning disable 219
19+
private void __RazorDirectiveTokenHelpers__() {
20+
((System.Action)(() => {
21+
DateTime __typeHelper = default(DateTime);
22+
}
23+
))();
24+
((System.Action)(() => {
25+
global::System.Object __typeHelper = "InputTestTagHelper, AppCode";
26+
}
27+
))();
28+
((System.Action)(() => {
29+
global::System.Object Section1 = null;
30+
}
31+
))();
32+
}
33+
#pragma warning restore 219
34+
#pragma warning disable 0414
35+
private static System.Object __o = null;
36+
#pragma warning restore 0414
37+
#pragma warning disable 1998
38+
public async override global::System.Threading.Tasks.Task ExecuteAsync()
39+
{
40+
#line 5 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
41+
42+
Layout = "_SectionTestLayout.cshtml";
43+
44+
#line default
45+
#line hidden
46+
DefineSection("Section1", async(__razor_section_writer) => {
47+
__InputTestTagHelper = CreateTagHelper<global::InputTestTagHelper>();
48+
#line 13 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
49+
__InputTestTagHelper.For = ModelExpressionProvider.CreateModelExpression(ViewData, __model => __model.Date);
50+
51+
#line default
52+
#line hidden
53+
}
54+
);
55+
}
56+
#pragma warning restore 1998
57+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
58+
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
59+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
60+
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
61+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
62+
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
63+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
64+
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
65+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
66+
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
67+
}
68+
}
69+
#pragma warning restore 1591
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
Document -
2+
CSharpCode -
3+
IntermediateToken - - CSharp - [assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml))]
4+
NamespaceDeclaration - - AspNetCore
5+
UsingDirective - - TModel = global::System.Object
6+
UsingDirective - (1:0,1 [12] ) - System
7+
UsingDirective - (16:1,1 [32] ) - System.Collections.Generic
8+
UsingDirective - (51:2,1 [17] ) - System.Linq
9+
UsingDirective - (71:3,1 [28] ) - System.Threading.Tasks
10+
UsingDirective - (102:4,1 [30] ) - Microsoft.AspNetCore.Mvc
11+
UsingDirective - (135:5,1 [40] ) - Microsoft.AspNetCore.Mvc.Rendering
12+
UsingDirective - (178:6,1 [43] ) - Microsoft.AspNetCore.Mvc.ViewFeatures
13+
ClassDeclaration - - public - TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml - global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<DateTime> -
14+
DefaultTagHelperRuntime -
15+
FieldDeclaration - - private - global::InputTestTagHelper - __InputTestTagHelper
16+
DesignTimeDirective -
17+
DirectiveToken - (231:7,8 [62] ) - global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<TModel>
18+
DirectiveToken - (294:7,71 [4] ) - Html
19+
DirectiveToken - (308:8,8 [54] ) - global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper
20+
DirectiveToken - (363:8,63 [4] ) - Json
21+
DirectiveToken - (377:9,8 [53] ) - global::Microsoft.AspNetCore.Mvc.IViewComponentHelper
22+
DirectiveToken - (431:9,62 [9] ) - Component
23+
DirectiveToken - (450:10,8 [43] ) - global::Microsoft.AspNetCore.Mvc.IUrlHelper
24+
DirectiveToken - (494:10,52 [3] ) - Url
25+
DirectiveToken - (507:11,8 [70] ) - global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider
26+
DirectiveToken - (578:11,79 [23] ) - ModelExpressionProvider
27+
DirectiveToken - (617:12,14 [96] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.UrlResolutionTagHelper, Microsoft.AspNetCore.Mvc.Razor
28+
DirectiveToken - (729:13,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.HeadTagHelper, Microsoft.AspNetCore.Mvc.Razor
29+
DirectiveToken - (832:14,14 [87] ) - Microsoft.AspNetCore.Mvc.Razor.TagHelpers.BodyTagHelper, Microsoft.AspNetCore.Mvc.Razor
30+
DirectiveToken - (7:0,7 [8] Sections.cshtml) - DateTime
31+
DirectiveToken - (33:2,14 [29] Sections.cshtml) - "InputTestTagHelper, AppCode"
32+
DirectiveToken - (152:10,9 [8] Sections.cshtml) - Section1
33+
CSharpCode -
34+
IntermediateToken - - CSharp - #pragma warning disable 0414
35+
CSharpCode -
36+
IntermediateToken - - CSharp - private static System.Object __o = null;
37+
CSharpCode -
38+
IntermediateToken - - CSharp - #pragma warning restore 0414
39+
MethodDeclaration - - public async override - global::System.Threading.Tasks.Task - ExecuteAsync
40+
HtmlContent - (17:1,0 [2] Sections.cshtml)
41+
IntermediateToken - (17:1,0 [2] Sections.cshtml) - Html - \n
42+
HtmlContent - (62:2,43 [4] Sections.cshtml)
43+
IntermediateToken - (62:2,43 [4] Sections.cshtml) - Html - \n\n
44+
CSharpCode - (68:4,2 [46] Sections.cshtml)
45+
IntermediateToken - (68:4,2 [46] Sections.cshtml) - CSharp - \n Layout = "_SectionTestLayout.cshtml";\n
46+
HtmlContent - (117:7,0 [26] Sections.cshtml)
47+
IntermediateToken - (117:7,0 [2] Sections.cshtml) - Html - \n
48+
IntermediateToken - (119:8,0 [5] Sections.cshtml) - Html - <div>
49+
IntermediateToken - (124:8,5 [9] Sections.cshtml) - Html - Some body
50+
IntermediateToken - (133:8,14 [6] Sections.cshtml) - Html - </div>
51+
IntermediateToken - (139:8,20 [4] Sections.cshtml) - Html - \n\n
52+
Section - - Section1
53+
HtmlContent - (162:10,19 [43] Sections.cshtml)
54+
IntermediateToken - (162:10,19 [6] Sections.cshtml) - Html - \n
55+
IntermediateToken - (168:11,4 [5] Sections.cshtml) - Html - <div>
56+
IntermediateToken - (173:11,9 [20] Sections.cshtml) - Html - This is in Section 1
57+
IntermediateToken - (193:11,29 [6] Sections.cshtml) - Html - </div>
58+
IntermediateToken - (199:11,35 [6] Sections.cshtml) - Html - \n
59+
TagHelper - (205:12,4 [25] Sections.cshtml) - input-test - TagMode.SelfClosing
60+
DefaultTagHelperBody -
61+
DefaultTagHelperCreate - - InputTestTagHelper
62+
DefaultTagHelperProperty - (222:12,21 [4] Sections.cshtml) - for - Microsoft.AspNetCore.Mvc.ViewFeatures.ModelExpression InputTestTagHelper.For - HtmlAttributeValueStyle.DoubleQuotes
63+
CSharpExpression -
64+
IntermediateToken - - CSharp - ModelExpressionProvider.CreateModelExpression(ViewData, __model =>
65+
IntermediateToken - - CSharp - __model.
66+
IntermediateToken - (222:12,21 [4] Sections.cshtml) - CSharp - Date
67+
IntermediateToken - - CSharp - )
68+
DefaultTagHelperExecute -
69+
HtmlContent - (230:12,29 [2] Sections.cshtml)
70+
IntermediateToken - (230:12,29 [2] Sections.cshtml) - Html - \n
71+
Inject -
72+
Inject -
73+
Inject -
74+
Inject -
75+
Inject -
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Source Location: (7:0,7 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
2+
|DateTime|
3+
Generated Location: (923:20,0 [8] )
4+
|DateTime|
5+
6+
Source Location: (33:2,14 [29] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
7+
|"InputTestTagHelper, AppCode"|
8+
Generated Location: (1064:24,37 [29] )
9+
|"InputTestTagHelper, AppCode"|
10+
11+
Source Location: (152:10,9 [8] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
12+
|Section1|
13+
Generated Location: (1178:28,22 [8] )
14+
|Section1|
15+
16+
Source Location: (68:4,2 [46] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
17+
|
18+
Layout = "_SectionTestLayout.cshtml";
19+
|
20+
Generated Location: (1613:40,2 [46] )
21+
|
22+
Layout = "_SectionTestLayout.cshtml";
23+
|
24+
25+
Source Location: (222:12,21 [4] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml)
26+
|Date|
27+
Generated Location: (2038:48,102 [4] )
28+
|Date|
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#pragma checksum "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "dbec91fd88a09c6a2e35b5adedb3f8ab8e3ae486"
2+
// <auto-generated/>
3+
#pragma warning disable 1591
4+
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(null, typeof(AspNetCore.TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml))]
5+
namespace AspNetCore
6+
{
7+
#line hidden
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Linq;
11+
using System.Threading.Tasks;
12+
using Microsoft.AspNetCore.Mvc;
13+
using Microsoft.AspNetCore.Mvc.Rendering;
14+
using Microsoft.AspNetCore.Mvc.ViewFeatures;
15+
public class TestFiles_IntegrationTests_CodeGenerationIntegrationTest_Sections_cshtml : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<DateTime>
16+
{
17+
#line hidden
18+
#pragma warning disable 0169
19+
private string __tagHelperStringValueBuffer;
20+
#pragma warning restore 0169
21+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext;
22+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner();
23+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __backed__tagHelperScopeManager = null;
24+
private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager
25+
{
26+
get
27+
{
28+
if (__backed__tagHelperScopeManager == null)
29+
{
30+
__backed__tagHelperScopeManager = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager(StartTagHelperWritingScope, EndTagHelperWritingScope);
31+
}
32+
return __backed__tagHelperScopeManager;
33+
}
34+
}
35+
private global::InputTestTagHelper __InputTestTagHelper;
36+
#pragma warning disable 1998
37+
public async override global::System.Threading.Tasks.Task ExecuteAsync()
38+
{
39+
BeginContext(17, 2, true);
40+
WriteLiteral("\r\n");
41+
EndContext();
42+
BeginContext(64, 2, true);
43+
WriteLiteral("\r\n");
44+
EndContext();
45+
#line 5 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
46+
47+
Layout = "_SectionTestLayout.cshtml";
48+
49+
#line default
50+
#line hidden
51+
BeginContext(117, 26, true);
52+
WriteLiteral("\r\n<div>Some body</div>\r\n\r\n");
53+
EndContext();
54+
DefineSection("Section1", async() => {
55+
BeginContext(162, 43, true);
56+
WriteLiteral("\r\n <div>This is in Section 1</div>\r\n ");
57+
EndContext();
58+
BeginContext(205, 25, false);
59+
__tagHelperExecutionContext = __tagHelperScopeManager.Begin("input-test", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "test", async() => {
60+
}
61+
);
62+
__InputTestTagHelper = CreateTagHelper<global::InputTestTagHelper>();
63+
__tagHelperExecutionContext.Add(__InputTestTagHelper);
64+
#line 13 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/Sections.cshtml"
65+
__InputTestTagHelper.For = ModelExpressionProvider.CreateModelExpression(ViewData, __model => __model.Date);
66+
67+
#line default
68+
#line hidden
69+
__tagHelperExecutionContext.AddTagHelperAttribute("for", __InputTestTagHelper.For, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
70+
await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
71+
if (!__tagHelperExecutionContext.Output.IsContentModified)
72+
{
73+
await __tagHelperExecutionContext.SetOutputContentAsync();
74+
}
75+
Write(__tagHelperExecutionContext.Output);
76+
__tagHelperExecutionContext = __tagHelperScopeManager.End();
77+
EndContext();
78+
BeginContext(230, 2, true);
79+
WriteLiteral("\r\n");
80+
EndContext();
81+
}
82+
);
83+
}
84+
#pragma warning restore 1998
85+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
86+
public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
87+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
88+
public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
89+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
90+
public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
91+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
92+
public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
93+
[global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
94+
public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<DateTime> Html { get; private set; }
95+
}
96+
}
97+
#pragma warning restore 1591

0 commit comments

Comments
 (0)