From e0957bde0a29bf67371f9eeafe90252dbffa44bd Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Wed, 6 Sep 2023 14:30:36 +0200 Subject: [PATCH 1/2] Avoid expanding tabs to spaces in codegen --- .../CodeGeneration/CodeWriterExtensions.cs | 5 +- .../RazorSourceGeneratorComponentTests.cs | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs index 12e04ee25c0..03406da4922 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/src/CodeGeneration/CodeWriterExtensions.cs @@ -68,12 +68,9 @@ int CalculatePadding() { break; } - else if (@char == '\t') - { - spaceCount += writer.TabSize; - } else { + // Note that a tab is also replaced with a single space so character indices match. spaceCount++; } } diff --git a/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorComponentTests.cs b/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorComponentTests.cs index 2b9908a13b2..3a272f06fe3 100644 --- a/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorComponentTests.cs +++ b/src/Compiler/test/Microsoft.NET.Sdk.Razor.SourceGenerators.Tests/RazorSourceGeneratorComponentTests.cs @@ -578,4 +578,57 @@ private static int RaiseHere() Assert.Equal(new TextSpan(originalIndex, snippet.Length), mappedSpan); } } + + [Fact, WorkItem("https://github.com/dotnet/razor/issues/9050")] + public async Task LineMapping_Tabs() + { + // Arrange + var tab = '\t'; + var source = $$""" +
+ {{tab}}@if (true) + {{tab}}{ + {{tab}}{{tab}}@("code") + {{tab}}} +
+ """; + var project = CreateTestProject(new() + { + ["Shared/Component1.razor"] = source, + }); + var compilation = await project.GetCompilationAsync(); + var driver = await GetDriverAsync(project); + + // Act + var result = RunGenerator(compilation!, ref driver); + + // Assert + result.Diagnostics.Verify(); + + var original = project.AdditionalDocuments.Single(); + var originalText = await original.GetTextAsync(); + Assert.Equal(source, originalText.ToString()); + var generated = result.GeneratedSources.Single(); + var generatedText = generated.SourceText; + var generatedTextString = generatedText.ToString(); + + // Find snippets and verify their mapping. + var snippets = new[] { "true", "code" }; + var expectedLines = new[] { 1, 3 }; + var originalIndex = -1; + var generatedIndex = -1; + foreach (var (snippet, expectedLine) in snippets.Zip(expectedLines)) + { + originalIndex = source.IndexOf(snippet, originalIndex + 1, StringComparison.Ordinal); + generatedIndex = generatedTextString.IndexOf(snippet, generatedIndex + 1, StringComparison.Ordinal); + var mapped = generated.SyntaxTree.GetMappedLineSpan(new TextSpan(generatedIndex, snippet.Length)); + Assert.True(mapped.IsValid); + Assert.True(mapped.HasMappedPath); + Assert.Equal("Shared/Component1.razor", mapped.Path); + Assert.Equal(expectedLine, mapped.StartLinePosition.Line); + Assert.Equal(expectedLine, mapped.EndLinePosition.Line); + var mappedSpan = originalText.Lines.GetTextSpan(mapped.Span); + Assert.Equal(new TextSpan(originalIndex, snippet.Length), mappedSpan); + } + } } From 3dde2620ef184d2ab8944e2f76e5c90445f5534c Mon Sep 17 00:00:00 2001 From: Jan Jones Date: Wed, 6 Sep 2023 14:35:17 +0200 Subject: [PATCH 2/2] Update baselines --- .../CodeBlockWithTextElement_DesignTime.codegen.cs | 4 ++-- .../CodeBlockWithTextElement_DesignTime.mappings.txt | 4 ++-- .../FunctionsBlockMinimal_DesignTime.codegen.cs | 2 +- .../FunctionsBlockMinimal_DesignTime.mappings.txt | 2 +- .../FunctionsBlockMinimal_Runtime.codegen.cs | 2 +- .../SimpleUnspacedIf_DesignTime.codegen.cs | 2 +- .../SimpleUnspacedIf_DesignTime.mappings.txt | 2 +- 7 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.codegen.cs index 7671c42ea16..5848cdd68d4 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.codegen.cs @@ -33,14 +33,14 @@ public async System.Threading.Tasks.Task ExecuteAsync() #nullable disable #nullable restore #line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml" - __o = a+b; + __o = a+b; #line default #line hidden #nullable disable #nullable restore #line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml" - + #line default #line hidden diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.mappings.txt index 00cc508cbb8..ab29f83952e 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement_DesignTime.mappings.txt @@ -14,13 +14,13 @@ Generated Location: (980:27,31 [22] ) Source Location: (69:2,29 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml) |a+b| -Generated Location: (1211:35,38 [3] ) +Generated Location: (1202:35,29 [3] ) |a+b| Source Location: (80:2,40 [2] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/CodeBlockWithTextElement.cshtml) | | -Generated Location: (1435:42,49 [2] ) +Generated Location: (1417:42,40 [2] ) | | diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.codegen.cs index 7fe252184ef..76d62a74e44 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.codegen.cs @@ -19,7 +19,7 @@ public async System.Threading.Tasks.Task ExecuteAsync() #pragma warning restore 1998 #nullable restore #line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal.cshtml" - + string foo(string input) { return input + "!"; } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.mappings.txt index 3ec3fb4f7ec..7758e2fb4fe 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_DesignTime.mappings.txt @@ -4,7 +4,7 @@ string foo(string input) { return input + "!"; } | -Generated Location: (817:21,15 [55] ) +Generated Location: (814:21,12 [55] ) | string foo(string input) { return input + "!"; diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_Runtime.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_Runtime.codegen.cs index f5ebdeaad1b..4eebe958146 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_Runtime.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal_Runtime.codegen.cs @@ -16,7 +16,7 @@ public async System.Threading.Tasks.Task ExecuteAsync() #pragma warning restore 1998 #nullable restore #line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/FunctionsBlockMinimal.cshtml" - + string foo(string input) { return input + "!"; } diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.codegen.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.codegen.cs index 0a6b991de06..1fc351a2e8e 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.codegen.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.codegen.cs @@ -26,7 +26,7 @@ public async System.Threading.Tasks.Task ExecuteAsync() #nullable disable #nullable restore #line 3 "TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf.cshtml" - + } #line default diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.mappings.txt b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.mappings.txt index 4be4709a6e0..2dee3710d1d 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.mappings.txt +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf_DesignTime.mappings.txt @@ -10,7 +10,7 @@ Generated Location: (744:19,1 [15] ) Source Location: (27:2,12 [3] TestFiles/IntegrationTests/CodeGenerationIntegrationTest/SimpleUnspacedIf.cshtml) | }| -Generated Location: (937:28,15 [3] ) +Generated Location: (934:28,12 [3] ) | }|