Skip to content

Commit faf1d80

Browse files
Follow-ups from PR 23385 (#24834)
* Make preservewhitespace use FileScopedSinglyOccurring * Design-time experience for boolean directive tokens * Update baselines
1 parent 04ca38b commit faf1d80

File tree

12 files changed

+115
-11
lines changed

12 files changed

+115
-11
lines changed

src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentPreserveWhitespaceDirective.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ internal static class ComponentPreserveWhitespaceDirective
1313
builder =>
1414
{
1515
builder.AddBooleanToken(ComponentResources.PreserveWhitespaceDirective_BooleanToken_Name, ComponentResources.PreserveWhitespaceDirective_BooleanToken_Description);
16-
builder.Usage = DirectiveUsage.FileScopedMultipleOccurring;
16+
builder.Usage = DirectiveUsage.FileScopedSinglyOccurring;
1717
builder.Description = ComponentResources.PreserveWhitespaceDirective_Description;
1818
});
1919

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ private void WriteDesignTimeDirectiveToken(CodeRenderingContext context, Directi
176176
context.CodeWriter.WriteLine(";");
177177
}
178178
break;
179+
180+
case DirectiveTokenKind.Boolean:
181+
// global::System.Boolean __typeHelper = {node.Content};
182+
using (context.CodeWriter.BuildLinePragma(node.Source, context))
183+
{
184+
context.CodeWriter
185+
.Write("global::")
186+
.Write(typeof(bool).FullName)
187+
.Write(" ")
188+
.WriteStartAssignment(TypeHelper);
189+
190+
context.AddSourceMappingFor(node);
191+
context.CodeWriter.Write(node.Content);
192+
context.CodeWriter.WriteLine(";");
193+
}
194+
break;
179195
}
180196
context.CodeWriter.CurrentIndent = originalIndent;
181197
}

src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,47 @@ private void __RazorDirectiveTokenHelpers__() {
202202
#line 1 ""test.cshtml""
203203
global::System.Object __typeHelper = ""Value"";
204204
205+
#line default
206+
#line hidden
207+
#nullable disable
208+
}
209+
))();
210+
}
211+
#pragma warning restore 219
212+
",
213+
csharp,
214+
ignoreLineEndingDifferences: true);
215+
}
216+
217+
[Fact]
218+
public void WriteDesignTimeDirective_WithBooleanToken_WritesLambda()
219+
{
220+
// Arrange
221+
var extension = new DesignTimeDirectiveTargetExtension();
222+
var context = TestCodeRenderingContext.CreateDesignTime();
223+
224+
var node = new DesignTimeDirectiveIntermediateNode();
225+
var token = new DirectiveTokenIntermediateNode()
226+
{
227+
Source = new SourceSpan("test.cshtml", 0, 0, 0, 5),
228+
Content = "true",
229+
DirectiveToken = DirectiveTokenDescriptor.CreateToken(DirectiveTokenKind.Boolean),
230+
};
231+
node.Children.Add(token);
232+
233+
// Act
234+
extension.WriteDesignTimeDirective(context, node);
235+
236+
// Assert
237+
var csharp = context.CodeWriter.GenerateCode();
238+
Assert.Equal(
239+
@"#pragma warning disable 219
240+
private void __RazorDirectiveTokenHelpers__() {
241+
((System.Action)(() => {
242+
#nullable restore
243+
#line 1 ""test.cshtml""
244+
global::System.Boolean __typeHelper = true;
245+
205246
#line default
206247
#line hidden
207248
#nullable disable

src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.codegen.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
1313
#pragma warning disable 219
1414
private void __RazorDirectiveTokenHelpers__() {
1515
((System.Action)(() => {
16+
#nullable restore
17+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
18+
global::System.Boolean __typeHelper = false;
19+
20+
#line default
21+
#line hidden
22+
#nullable disable
1623
}
1724
))();
1825
}

src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.ir.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Document -
77
UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components
88
ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase -
99
DesignTimeDirective -
10-
DirectiveToken - (20:0,20 [4] x:\dir\subdir\Test\_Imports.razor) - true
1110
DirectiveToken - (20:0,20 [5] x:\dir\subdir\Test\TestComponent.cshtml) - false
1211
CSharpCode -
1312
IntermediateToken - - CSharp - #pragma warning disable 0414
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
Source Location: (20:0,20 [5] x:\dir\subdir\Test\TestComponent.cshtml)
2+
|false|
3+
Generated Location: (581:17,38 [5] )
4+
|false|
5+
16
Source Location: (52:3,13 [12] x:\dir\subdir\Test\TestComponent.cshtml)
27
|DateTime.Now|
3-
Generated Location: (925:27,13 [12] )
8+
Generated Location: (1091:34,13 [12] )
49
|DateTime.Now|
510

src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.codegen.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
1313
#pragma warning disable 219
1414
private void __RazorDirectiveTokenHelpers__() {
1515
((System.Action)(() => {
16+
#nullable restore
17+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
18+
global::System.Boolean __typeHelper = false;
19+
20+
#line default
21+
#line hidden
22+
#nullable disable
1623
}
1724
))();
1825
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
Source Location: (20:0,20 [5] x:\dir\subdir\Test\TestComponent.cshtml)
2+
|false|
3+
Generated Location: (581:17,38 [5] )
4+
|false|
5+
16
Source Location: (40:3,5 [63] x:\dir\subdir\Test\TestComponent.cshtml)
27
|foreach (var item in Enumerable.Range(1, 100))
38
{
49
|
5-
Generated Location: (917:27,5 [63] )
10+
Generated Location: (1083:34,5 [63] )
611
|foreach (var item in Enumerable.Range(1, 100))
712
{
813
|
914

1015
Source Location: (122:6,13 [4] x:\dir\subdir\Test\TestComponent.cshtml)
1116
|item|
12-
Generated Location: (1115:36,13 [4] )
17+
Generated Location: (1281:43,13 [4] )
1318
|item|
1419

1520
Source Location: (141:7,13 [7] x:\dir\subdir\Test\TestComponent.cshtml)
1621
|
1722
}|
18-
Generated Location: (1255:43,13 [7] )
23+
Generated Location: (1421:50,13 [7] )
1924
|
2025
}|
2126

src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.codegen.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ public partial class TestComponent : Microsoft.AspNetCore.Components.ComponentBa
1313
#pragma warning disable 219
1414
private void __RazorDirectiveTokenHelpers__() {
1515
((System.Action)(() => {
16+
#nullable restore
17+
#line 1 "x:\dir\subdir\Test\TestComponent.cshtml"
18+
global::System.Boolean __typeHelper = true;
19+
20+
#line default
21+
#line hidden
22+
#nullable disable
1623
}
1724
))();
1825
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
Source Location: (20:0,20 [4] x:\dir\subdir\Test\TestComponent.cshtml)
2+
|true|
3+
Generated Location: (581:17,38 [4] )
4+
|true|
5+
16
Source Location: (39:3,5 [63] x:\dir\subdir\Test\TestComponent.cshtml)
27
|foreach (var item in Enumerable.Range(1, 100))
38
{
49
|
5-
Generated Location: (917:27,5 [63] )
10+
Generated Location: (1082:34,5 [63] )
611
|foreach (var item in Enumerable.Range(1, 100))
712
{
813
|
914

1015
Source Location: (121:6,13 [4] x:\dir\subdir\Test\TestComponent.cshtml)
1116
|item|
12-
Generated Location: (1115:36,13 [4] )
17+
Generated Location: (1280:43,13 [4] )
1318
|item|
1419

1520
Source Location: (140:7,13 [7] x:\dir\subdir\Test\TestComponent.cshtml)
1621
|
1722
}|
18-
Generated Location: (1255:43,13 [7] )
23+
Generated Location: (1420:50,13 [7] )
1924
|
2025
}|
2126

0 commit comments

Comments
 (0)