From c755804389e0d364d0a446dddd5ef3faa8b07d89 Mon Sep 17 00:00:00 2001 From: Dustin Campbell Date: Fri, 9 Aug 2024 14:20:46 -0700 Subject: [PATCH] Swap RazorCSharpDocument.Create methods for constructors --- .../test/RazorCodeDocumentExtensionsTest.cs | 4 +-- .../src/Language/RazorCSharpDocument.cs | 34 ++----------------- .../RazorDiagnosticsBenchmark.cs | 2 +- .../UnsupportedCSharpLoweringPhase.cs | 8 ++--- .../DefaultCSharpCodeActionProviderTest.cs | 2 +- ...TypeAccessibilityCodeActionProviderTest.cs | 2 +- ...nentAccessibilityCodeActionProviderTest.cs | 2 +- .../RazorDiagnosticsPublisherTest.cs | 2 +- .../FormattingLanguageServerTestBase.cs | 4 +-- .../Mapping/RazorLanguageQueryEndpointTest.cs | 2 +- .../RazorMapToDocumentRangesEndpointTest.cs | 3 +- .../RazorDocumentMappingServiceTest.cs | 4 +-- 12 files changed, 17 insertions(+), 52 deletions(-) diff --git a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/RazorCodeDocumentExtensionsTest.cs b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/RazorCodeDocumentExtensionsTest.cs index 51eb9b0395..c1fafa832a 100644 --- a/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/RazorCodeDocumentExtensionsTest.cs +++ b/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/RazorCodeDocumentExtensionsTest.cs @@ -113,7 +113,7 @@ public class RazorCodeDocumentExtensionsTest // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); - var expected = RazorCSharpDocument.Create(codeDocument, "", RazorCodeGenerationOptions.CreateDefault(), diagnostics: []); + var expected = new RazorCSharpDocument(codeDocument, "", RazorCodeGenerationOptions.CreateDefault(), diagnostics: []); codeDocument.Items[typeof(RazorCSharpDocument)] = expected; // Act @@ -129,7 +129,7 @@ public class RazorCodeDocumentExtensionsTest // Arrange var codeDocument = TestRazorCodeDocument.CreateEmpty(); - var expected = RazorCSharpDocument.Create(codeDocument, "", RazorCodeGenerationOptions.CreateDefault(), diagnostics: []); + var expected = new RazorCSharpDocument(codeDocument, "", RazorCodeGenerationOptions.CreateDefault(), diagnostics: []); // Act codeDocument.SetCSharpDocument(expected); diff --git a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorCSharpDocument.cs b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorCSharpDocument.cs index 20baddcb80..f31792488e 100644 --- a/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorCSharpDocument.cs +++ b/src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/RazorCSharpDocument.cs @@ -3,7 +3,6 @@ using System.Collections.Generic; using System.Collections.Immutable; -using System.Linq; using Microsoft.AspNetCore.Razor.Language.CodeGeneration; namespace Microsoft.AspNetCore.Razor.Language; @@ -22,11 +21,12 @@ public sealed class RazorCSharpDocument : IRazorGeneratedDocument string generatedCode, RazorCodeGenerationOptions options, RazorDiagnostic[] diagnostics, - ImmutableArray sourceMappings, - ImmutableArray linePragmas) + ImmutableArray sourceMappings = default, + ImmutableArray linePragmas = default) { ArgHelper.ThrowIfNull(codeDocument); ArgHelper.ThrowIfNull(generatedCode); + ArgHelper.ThrowIfNull(options); CodeDocument = codeDocument; GeneratedCode = generatedCode; @@ -36,32 +36,4 @@ public sealed class RazorCSharpDocument : IRazorGeneratedDocument SourceMappings = sourceMappings.NullToEmpty(); LinePragmas = linePragmas.NullToEmpty(); } - - public static RazorCSharpDocument Create( - RazorCodeDocument codeDocument, - string generatedCode, - RazorCodeGenerationOptions options, - IEnumerable diagnostics) - { - ArgHelper.ThrowIfNull(generatedCode); - ArgHelper.ThrowIfNull(options); - ArgHelper.ThrowIfNull(diagnostics); - - return new(codeDocument, generatedCode, options, diagnostics.ToArray(), sourceMappings: [], linePragmas: []); - } - - public static RazorCSharpDocument Create( - RazorCodeDocument codeDocument, - string generatedCode, - RazorCodeGenerationOptions options, - IEnumerable diagnostics, - ImmutableArray sourceMappings, - ImmutableArray linePragmas) - { - ArgHelper.ThrowIfNull(generatedCode); - ArgHelper.ThrowIfNull(options); - ArgHelper.ThrowIfNull(diagnostics); - - return new(codeDocument, generatedCode, options, diagnostics.ToArray(), sourceMappings, linePragmas); - } } diff --git a/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorDiagnosticsBenchmark.cs b/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorDiagnosticsBenchmark.cs index 0c49b8c586..b07988f16c 100644 --- a/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorDiagnosticsBenchmark.cs +++ b/src/Razor/benchmarks/Microsoft.AspNetCore.Razor.Microbenchmarks/LanguageServer/RazorDiagnosticsBenchmark.cs @@ -57,7 +57,7 @@ public class RazorDiagnosticsBenchmark : RazorLanguageServerBenchmarkBase var stringSourceDocument = RazorSourceDocument.Create(GetFileContents(), UTF8Encoding.UTF8, RazorSourceDocumentProperties.Default); var mockRazorCodeDocument = new Mock(MockBehavior.Strict); - var mockRazorCSharpDocument = RazorCSharpDocument.Create( + var mockRazorCSharpDocument = new RazorCSharpDocument( mockRazorCodeDocument.Object, GeneratedCode, RazorCodeGenerationOptions.CreateDesignTimeDefault(), diff --git a/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/ProjectEngineHost/UnsupportedCSharpLoweringPhase.cs b/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/ProjectEngineHost/UnsupportedCSharpLoweringPhase.cs index c709d94366..606a35ef44 100644 --- a/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/ProjectEngineHost/UnsupportedCSharpLoweringPhase.cs +++ b/src/Razor/src/Microsoft.AspNetCore.Razor.ProjectEngineHost/ProjectEngineHost/UnsupportedCSharpLoweringPhase.cs @@ -14,12 +14,8 @@ internal class UnsupportedCSharpLoweringPhase : RazorEnginePhaseBase, IRazorCSha var documentNode = codeDocument.GetDocumentIntermediateNode(); ThrowForMissingDocumentDependency(documentNode); - var cSharpDocument = RazorCSharpDocument.Create( - codeDocument, - UnsupportedDisclaimer, - documentNode.Options, - diagnostics: []); - codeDocument.SetCSharpDocument(cSharpDocument); + var csharpDocument = new RazorCSharpDocument(codeDocument, UnsupportedDisclaimer, documentNode.Options, diagnostics: []); + codeDocument.SetCSharpDocument(csharpDocument); codeDocument.SetUnsupported(); } } diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/DefaultCSharpCodeActionProviderTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/DefaultCSharpCodeActionProviderTest.cs index fb1bd67fc0..bd93514e88 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/DefaultCSharpCodeActionProviderTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/DefaultCSharpCodeActionProviderTest.cs @@ -329,7 +329,7 @@ $$Path; var csharpDocument = codeDocument.GetCSharpDocument(); var diagnosticDescriptor = new RazorDiagnosticDescriptor("RZ10012", "diagnostic", RazorDiagnosticSeverity.Error); var diagnostic = RazorDiagnostic.Create(diagnosticDescriptor, componentSourceSpan); - var csharpDocumentWithDiagnostic = RazorCSharpDocument.Create(codeDocument, csharpDocument.GeneratedCode, csharpDocument.Options, [diagnostic]); + var csharpDocumentWithDiagnostic = new RazorCSharpDocument(codeDocument, csharpDocument.GeneratedCode, csharpDocument.Options, [diagnostic]); codeDocument.SetCSharpDocument(csharpDocumentWithDiagnostic); var documentSnapshot = Mock.Of(document => diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/TypeAccessibilityCodeActionProviderTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/TypeAccessibilityCodeActionProviderTest.cs index 5b48f7c429..610e046226 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/TypeAccessibilityCodeActionProviderTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/CSharp/TypeAccessibilityCodeActionProviderTest.cs @@ -460,7 +460,7 @@ public class TypeAccessibilityCodeActionProviderTest(ITestOutputHelper testOutpu var csharpDocument = codeDocument.GetCSharpDocument(); var diagnosticDescriptor = new RazorDiagnosticDescriptor("RZ10012", "diagnostic", RazorDiagnosticSeverity.Error); var diagnostic = RazorDiagnostic.Create(diagnosticDescriptor, componentSourceSpan); - var csharpDocumentWithDiagnostic = RazorCSharpDocument.Create(codeDocument, csharpDocument.GeneratedCode, csharpDocument.Options, [diagnostic]); + var csharpDocumentWithDiagnostic = new RazorCSharpDocument(codeDocument, csharpDocument.GeneratedCode, csharpDocument.Options, [diagnostic]); codeDocument.SetCSharpDocument(csharpDocumentWithDiagnostic); var documentSnapshot = Mock.Of(document => diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ComponentAccessibilityCodeActionProviderTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ComponentAccessibilityCodeActionProviderTest.cs index e9e6fd617a..56874c402d 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ComponentAccessibilityCodeActionProviderTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/CodeActions/Razor/ComponentAccessibilityCodeActionProviderTest.cs @@ -460,7 +460,7 @@ public class ComponentAccessibilityCodeActionProviderTest(ITestOutputHelper test var csharpDocument = codeDocument.GetCSharpDocument(); var diagnosticDescriptor = new RazorDiagnosticDescriptor("RZ10012", "diagnostic", RazorDiagnosticSeverity.Error); var diagnostic = RazorDiagnostic.Create(diagnosticDescriptor, componentSourceSpan); - var csharpDocumentWithDiagnostic = RazorCSharpDocument.Create(codeDocument, csharpDocument.GeneratedCode, csharpDocument.Options, [diagnostic]); + var csharpDocumentWithDiagnostic = new RazorCSharpDocument(codeDocument, csharpDocument.GeneratedCode, csharpDocument.Options, [diagnostic]); codeDocument.SetCSharpDocument(csharpDocumentWithDiagnostic); var documentSnapshot = Mock.Of(document => diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs index 31587d7314..58e03d3d2b 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Diagnostics/RazorDiagnosticsPublisherTest.cs @@ -528,7 +528,7 @@ public class RazorDiagnosticsPublisherTest(ITestOutputHelper testOutput) : Langu private static RazorCodeDocument CreateCodeDocument(IEnumerable diagnostics) { var codeDocument = TestRazorCodeDocument.Create("hello"); - var razorCSharpDocument = RazorCSharpDocument.Create(codeDocument, "hello", RazorCodeGenerationOptions.CreateDefault(), diagnostics.ToImmutableArray()); + var razorCSharpDocument = new RazorCSharpDocument(codeDocument, "hello", RazorCodeGenerationOptions.CreateDefault(), diagnostics.ToImmutableArray()); codeDocument.SetCSharpDocument(razorCSharpDocument); return codeDocument; diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs index 7e924e88a2..70ad34fc7a 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Formatting_NetFx/FormattingLanguageServerTestBase.cs @@ -1,8 +1,6 @@ // Copyright (c) .NET Foundation. All rights reserved. // Licensed under the MIT license. See License.txt in the project root for license information. -using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Threading; using System.Threading.Tasks; @@ -24,7 +22,7 @@ public abstract class FormattingLanguageServerTestBase(ITestOutputHelper testOut var sourceDocument = TestRazorSourceDocument.Create(content); var codeDocument = RazorCodeDocument.Create(sourceDocument); var syntaxTree = RazorSyntaxTree.Parse(sourceDocument, RazorParserOptions.CreateDefault()); - var razorCSharpDocument = RazorCSharpDocument.Create( + var razorCSharpDocument = new RazorCSharpDocument( codeDocument, content, RazorCodeGenerationOptions.CreateDefault(), diagnostics: [], sourceMappings, linePragmas: []); codeDocument.SetSyntaxTree(syntaxTree); codeDocument.SetCSharpDocument(razorCSharpDocument); diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorLanguageQueryEndpointTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorLanguageQueryEndpointTest.cs index eb3985c14b..406250585c 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorLanguageQueryEndpointTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorLanguageQueryEndpointTest.cs @@ -145,7 +145,7 @@ public class RazorLanguageQueryEndpointTest : LanguageServerTestBase private static RazorCodeDocument CreateCodeDocumentWithCSharpProjection(string razorSource, string projectedCSharpSource, ImmutableArray sourceMappings) { var codeDocument = CreateCodeDocument(razorSource, tagHelpers: []); - var csharpDocument = RazorCSharpDocument.Create( + var csharpDocument = new RazorCSharpDocument( codeDocument, projectedCSharpSource, RazorCodeGenerationOptions.CreateDefault(), diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorMapToDocumentRangesEndpointTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorMapToDocumentRangesEndpointTest.cs index b9a3f275cc..bda6189e8e 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorMapToDocumentRangesEndpointTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/Mapping/RazorMapToDocumentRangesEndpointTest.cs @@ -2,7 +2,6 @@ // Licensed under the MIT license. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using System.Collections.Immutable; using System.Threading.Tasks; using Microsoft.AspNetCore.Razor.Language; @@ -232,7 +231,7 @@ public class RazorMapToDocumentRangesEndpointTest : LanguageServerTestBase private static RazorCodeDocument CreateCodeDocumentWithCSharpProjection(string razorSource, string projectedCSharpSource, ImmutableArray sourceMappings) { var codeDocument = CreateCodeDocument(razorSource, tagHelpers: []); - var csharpDocument = RazorCSharpDocument.Create( + var csharpDocument = new RazorCSharpDocument( codeDocument, projectedCSharpSource, RazorCodeGenerationOptions.CreateDefault(), diff --git a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorDocumentMappingServiceTest.cs b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorDocumentMappingServiceTest.cs index 533abcd063..ec59499715 100644 --- a/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorDocumentMappingServiceTest.cs +++ b/src/Razor/test/Microsoft.AspNetCore.Razor.LanguageServer.Test/RazorDocumentMappingServiceTest.cs @@ -1032,12 +1032,12 @@ public class RazorDocumentMappingServiceTest(ITestOutputHelper testOutput) : Too private static RazorCodeDocument CreateCodeDocumentWithCSharpProjection(string razorSource, string projectedCSharpSource, ImmutableArray sourceMappings) { var codeDocument = CreateCodeDocument(razorSource, tagHelpers: []); - var csharpDocument = RazorCSharpDocument.Create( + var csharpDocument = new RazorCSharpDocument( codeDocument, projectedCSharpSource, RazorCodeGenerationOptions.CreateDefault(), diagnostics: [], - sourceMappings.ToImmutableArray(), + sourceMappings, linePragmas: []); codeDocument.SetCSharpDocument(csharpDocument); return codeDocument;