From faf1d80af1b0521ed120f767efd630999200a02e Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Mon, 24 Aug 2020 14:02:21 +0100 Subject: [PATCH] Follow-ups from PR 23385 (#24834) * Make preservewhitespace use FileScopedSinglyOccurring * Design-time experience for boolean directive tokens * Update baselines --- .../ComponentPreserveWhitespaceDirective.cs | 2 +- .../DesignTimeDirectiveTargetExtension.cs | 16 ++++++++ .../DesignTimeDirectiveTargetExtensionTest.cs | 41 +++++++++++++++++++ .../TestComponent.codegen.cs | 7 ++++ .../TestComponent.ir.txt | 1 - .../TestComponent.mappings.txt | 7 +++- .../TestComponent.codegen.cs | 7 ++++ .../TestComponent.mappings.txt | 11 +++-- .../TestComponent.codegen.cs | 7 ++++ .../TestComponent.mappings.txt | 11 +++-- .../TestComponent.codegen.cs | 7 ++++ .../TestComponent.mappings.txt | 9 +++- 12 files changed, 115 insertions(+), 11 deletions(-) diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentPreserveWhitespaceDirective.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentPreserveWhitespaceDirective.cs index 72f657aac7..b8cbd690c0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentPreserveWhitespaceDirective.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Components/ComponentPreserveWhitespaceDirective.cs @@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Razor.Language.Components builder => { builder.AddBooleanToken(ComponentResources.PreserveWhitespaceDirective_BooleanToken_Name, ComponentResources.PreserveWhitespaceDirective_BooleanToken_Description); - builder.Usage = DirectiveUsage.FileScopedMultipleOccurring; + builder.Usage = DirectiveUsage.FileScopedSinglyOccurring; builder.Description = ComponentResources.PreserveWhitespaceDirective_Description; }); diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DesignTimeDirectiveTargetExtension.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DesignTimeDirectiveTargetExtension.cs index ff4f131630..6e5a87f47c 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DesignTimeDirectiveTargetExtension.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/src/Extensions/DesignTimeDirectiveTargetExtension.cs @@ -176,6 +176,22 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions context.CodeWriter.WriteLine(";"); } break; + + case DirectiveTokenKind.Boolean: + // global::System.Boolean __typeHelper = {node.Content}; + using (context.CodeWriter.BuildLinePragma(node.Source, context)) + { + context.CodeWriter + .Write("global::") + .Write(typeof(bool).FullName) + .Write(" ") + .WriteStartAssignment(TypeHelper); + + context.AddSourceMappingFor(node); + context.CodeWriter.Write(node.Content); + context.CodeWriter.WriteLine(";"); + } + break; } context.CodeWriter.CurrentIndent = originalIndent; } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs index 4d61b0ef6c..d8a850aa8e 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/Extensions/DesignTimeDirectiveTargetExtensionTest.cs @@ -202,6 +202,47 @@ global::System.Object __typeHelper = ""Value""; #line 1 ""test.cshtml"" global::System.Object __typeHelper = ""Value""; +#line default +#line hidden +#nullable disable +} +))(); +} +#pragma warning restore 219 +", + csharp, + ignoreLineEndingDifferences: true); + } + + [Fact] + public void WriteDesignTimeDirective_WithBooleanToken_WritesLambda() + { + // Arrange + var extension = new DesignTimeDirectiveTargetExtension(); + var context = TestCodeRenderingContext.CreateDesignTime(); + + var node = new DesignTimeDirectiveIntermediateNode(); + var token = new DirectiveTokenIntermediateNode() + { + Source = new SourceSpan("test.cshtml", 0, 0, 0, 5), + Content = "true", + DirectiveToken = DirectiveTokenDescriptor.CreateToken(DirectiveTokenKind.Boolean), + }; + node.Children.Add(token); + + // Act + extension.WriteDesignTimeDirective(context, node); + + // Assert + var csharp = context.CodeWriter.GenerateCode(); + Assert.Equal( +@"#pragma warning disable 219 +private void __RazorDirectiveTokenHelpers__() { +((System.Action)(() => { +#nullable restore +#line 1 ""test.cshtml"" +global::System.Boolean __typeHelper = true; + #line default #line hidden #nullable disable diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.codegen.cs index d75d0e63e4..b7ba2abc4f 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.codegen.cs @@ -13,6 +13,13 @@ namespace Test #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { ((System.Action)(() => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +global::System.Boolean __typeHelper = false; + +#line default +#line hidden +#nullable disable } ))(); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.ir.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.ir.txt index 94eecda6a9..7f9c6b3e1e 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.ir.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.ir.txt @@ -7,7 +7,6 @@ Document - UsingDirective - (104:5,1 [37] ) - Microsoft.AspNetCore.Components ClassDeclaration - - public partial - TestComponent - Microsoft.AspNetCore.Components.ComponentBase - DesignTimeDirective - - DirectiveToken - (20:0,20 [4] x:\dir\subdir\Test\_Imports.razor) - true DirectiveToken - (20:0,20 [5] x:\dir\subdir\Test\TestComponent.cshtml) - false CSharpCode - IntermediateToken - - CSharp - #pragma warning disable 0414 diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.mappings.txt index 234aca0387..0ea257e66d 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_PreserveWhitespaceDirective_OverrideImports/TestComponent.mappings.txt @@ -1,5 +1,10 @@ +Source Location: (20:0,20 [5] x:\dir\subdir\Test\TestComponent.cshtml) +|false| +Generated Location: (581:17,38 [5] ) +|false| + Source Location: (52:3,13 [12] x:\dir\subdir\Test\TestComponent.cshtml) |DateTime.Now| -Generated Location: (925:27,13 [12] ) +Generated Location: (1091:34,13 [12] ) |DateTime.Now| diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.codegen.cs index 7b36a7dcc8..b900c349b4 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.codegen.cs @@ -13,6 +13,13 @@ namespace Test #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { ((System.Action)(() => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +global::System.Boolean __typeHelper = false; + +#line default +#line hidden +#nullable disable } ))(); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.mappings.txt index 13d244d11c..c031640dce 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_False/TestComponent.mappings.txt @@ -1,21 +1,26 @@ +Source Location: (20:0,20 [5] x:\dir\subdir\Test\TestComponent.cshtml) +|false| +Generated Location: (581:17,38 [5] ) +|false| + Source Location: (40:3,5 [63] x:\dir\subdir\Test\TestComponent.cshtml) |foreach (var item in Enumerable.Range(1, 100)) { | -Generated Location: (917:27,5 [63] ) +Generated Location: (1083:34,5 [63] ) |foreach (var item in Enumerable.Range(1, 100)) { | Source Location: (122:6,13 [4] x:\dir\subdir\Test\TestComponent.cshtml) |item| -Generated Location: (1115:36,13 [4] ) +Generated Location: (1281:43,13 [4] ) |item| Source Location: (141:7,13 [7] x:\dir\subdir\Test\TestComponent.cshtml) | }| -Generated Location: (1255:43,13 [7] ) +Generated Location: (1421:50,13 [7] ) | }| diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.codegen.cs index 7b36a7dcc8..3e47ac30a0 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.codegen.cs @@ -13,6 +13,13 @@ namespace Test #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { ((System.Action)(() => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +global::System.Boolean __typeHelper = true; + +#line default +#line hidden +#nullable disable } ))(); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.mappings.txt index a78b63be25..9766eb798d 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/Component_WithPreserveWhitespaceDirective_True/TestComponent.mappings.txt @@ -1,21 +1,26 @@ +Source Location: (20:0,20 [4] x:\dir\subdir\Test\TestComponent.cshtml) +|true| +Generated Location: (581:17,38 [4] ) +|true| + Source Location: (39:3,5 [63] x:\dir\subdir\Test\TestComponent.cshtml) |foreach (var item in Enumerable.Range(1, 100)) { | -Generated Location: (917:27,5 [63] ) +Generated Location: (1082:34,5 [63] ) |foreach (var item in Enumerable.Range(1, 100)) { | Source Location: (121:6,13 [4] x:\dir\subdir\Test\TestComponent.cshtml) |item| -Generated Location: (1115:36,13 [4] ) +Generated Location: (1280:43,13 [4] ) |item| Source Location: (140:7,13 [7] x:\dir\subdir\Test\TestComponent.cshtml) | }| -Generated Location: (1255:43,13 [7] ) +Generated Location: (1420:50,13 [7] ) | }| diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.codegen.cs b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.codegen.cs index 977f3d0e56..a9bf62d406 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.codegen.cs +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.codegen.cs @@ -13,6 +13,13 @@ namespace Test #pragma warning disable 219 private void __RazorDirectiveTokenHelpers__() { ((System.Action)(() => { +#nullable restore +#line 1 "x:\dir\subdir\Test\TestComponent.cshtml" +global::System.Boolean __typeHelper = true; + +#line default +#line hidden +#nullable disable } ))(); } diff --git a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.mappings.txt b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.mappings.txt index 6f2c28937b..2859972654 100644 --- a/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.mappings.txt +++ b/src/Razor/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/ComponentDesignTimeCodeGenerationTest/WhiteSpace_WithPreserveWhitespace/TestComponent.mappings.txt @@ -1,13 +1,18 @@ +Source Location: (20:0,20 [4] x:\dir\subdir\Test\TestComponent.cshtml) +|true| +Generated Location: (581:17,38 [4] ) +|true| + Source Location: (44:2,16 [3] x:\dir\subdir\Test\TestComponent.cshtml) |Foo| -Generated Location: (948:28,16 [3] ) +Generated Location: (1113:35,16 [3] ) |Foo| Source Location: (95:6,11 [29] x:\dir\subdir\Test\TestComponent.cshtml) | int Foo = 18; | -Generated Location: (1148:38,11 [29] ) +Generated Location: (1313:45,11 [29] ) | int Foo = 18; |