Skip to content

Commit 296f127

Browse files
Clean up RazorConfiguration, RazorLanguageVersion, and RazorExtension types (#10001)
This change converts `RazorConfiguration`, `RazorLanguageVersion`, and `RazorExtension` to sealed records and removes all extraneous subtypes. To achieve this, I've also removed the `AssemblyExtension` type along with `ProvideRazorExtensionInitializerAttribute`. After the compiler assembly merge, the set of Razor extensions is known. So, I've removed the reflection code that would initialize a Razor extension and just handle the extensions statically.
2 parents c126dfc + a618454 commit 296f127

File tree

70 files changed

+317
-783
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+317
-783
lines changed

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/test/InjectDirectiveTest.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
#nullable disable
55

6-
using System;
76
using System.Text;
87
using Microsoft.AspNetCore.Razor.Language;
98
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -176,11 +175,11 @@ private ClassDeclarationIntermediateNode FindClassNode(IntermediateNode node)
176175

177176
private RazorEngine CreateEngine()
178177
{
179-
var configuration = RazorConfiguration.Create(RazorLanguageVersion.Version_1_1, "test", Array.Empty<RazorExtension>());
178+
var configuration = new RazorConfiguration(RazorLanguageVersion.Version_1_1, "test", Extensions: []);
180179
return RazorProjectEngine.Create(configuration, RazorProjectFileSystem.Empty, b =>
181180
{
182-
// Notice we're not registering the InjectDirective.Pass here so we can run it on demand.
183-
b.AddDirective(InjectDirective.Directive);
181+
// Notice we're not registering the InjectDirective.Pass here so we can run it on demand.
182+
b.AddDirective(InjectDirective.Directive);
184183
b.AddDirective(ModelDirective.Directive);
185184
}).Engine;
186185
}

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ public class CodeGenerationIntegrationTest : IntegrationTestBase
1818
public CodeGenerationIntegrationTest()
1919
: base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version1_X")
2020
{
21-
Configuration = RazorConfiguration.Create(
22-
RazorLanguageVersion.Version_1_1,
23-
"MVC-1.1",
24-
new[] { new AssemblyExtension("MVC-1.1", typeof(ExtensionInitializer).Assembly) });
21+
Configuration = new(RazorLanguageVersion.Version_1_1, "MVC-1.1", Extensions: []);
2522
}
2623

2724
protected override CSharpCompilation BaseCompilation => DefaultBaseCompilation;

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public class CodeGenerationIntegrationTest : IntegrationTestBase
2020
public CodeGenerationIntegrationTest()
2121
: base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X")
2222
{
23-
Configuration = RazorConfiguration.Create(
24-
RazorLanguageVersion.Version_2_0,
25-
"MVC-2.1",
26-
new[] { new AssemblyExtension("MVC-2.1", typeof(ExtensionInitializer).Assembly) });
23+
Configuration = new(RazorLanguageVersion.Version_2_0, "MVC-2.1", Extensions: []);
2724
}
2825

2926
protected override CSharpCompilation BaseCompilation => DefaultBaseCompilation;

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X/test/IntegrationTests/InstrumentationPassIntegrationTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ public class InstrumentationPassIntegrationTest : IntegrationTestBase
2121
public InstrumentationPassIntegrationTest()
2222
: base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions.Version2_X")
2323
{
24-
Configuration = RazorConfiguration.Create(
25-
RazorLanguageVersion.Version_2_0,
26-
"MVC-2.1",
27-
new[] { new AssemblyExtension("MVC-2.1", typeof(ExtensionInitializer).Assembly) });
24+
Configuration = new(RazorLanguageVersion.Version_2_0, "MVC-2.1", Extensions: []);
2825
}
2926

3027
protected override CSharpCompilation BaseCompilation => DefaultBaseCompilation;

src/Compiler/Microsoft.AspNetCore.Mvc.Razor.Extensions/test/IntegrationTests/CodeGenerationIntegrationTest.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@ public class CodeGenerationIntegrationTest : IntegrationTestBase
2020
public CodeGenerationIntegrationTest()
2121
: base(layer: TestProject.Layer.Compiler, generateBaselines: null, projectDirectoryHint: "Microsoft.AspNetCore.Mvc.Razor.Extensions")
2222
{
23-
Configuration = RazorConfiguration.Create(
24-
RazorLanguageVersion.Latest,
25-
"MVC-3.0",
26-
new[] { new AssemblyExtension("MVC-3.0", typeof(ExtensionInitializer).Assembly) });
23+
Configuration = new(RazorLanguageVersion.Latest, "MVC-3.0", Extensions: []);
2724
}
2825

2926
protected override CSharpCompilation BaseCompilation { get; set; } = DefaultBaseCompilation;

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentCodeGenerationTestBase.cs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ public void OmitsMinimizedAttributeValueParameter()
991991
public void IncludesMinimizedAttributeValueParameterBeforeLanguageVersion5()
992992
{
993993
// Arrange
994-
_configuration = RazorConfiguration.Create(
994+
_configuration = new(
995995
RazorLanguageVersion.Version_3_0,
996996
base.Configuration.ConfigurationName,
997997
base.Configuration.Extensions);
@@ -1385,7 +1385,7 @@ public class ComponentWithEditorRequiredChildContent : ComponentBase
13851385
[IntegrationTestFact, WorkItem("https://github.com/dotnet/aspnetcore/issues/18042")]
13861386
public void AddAttribute_ImplicitStringConversion_TypeInference()
13871387
{
1388-
_configuration = base.Configuration.WithVersion(RazorLanguageVersion.Version_7_0);
1388+
_configuration = base.Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 };
13891389

13901390
AdditionalSyntaxTrees.Add(Parse("""
13911391
using Microsoft.AspNetCore.Components;
@@ -1436,7 +1436,7 @@ public class MyComponent<T> : ComponentBase
14361436
[IntegrationTestFact, WorkItem("https://github.com/dotnet/aspnetcore/issues/18042")]
14371437
public void AddAttribute_ImplicitStringConversion_Bind()
14381438
{
1439-
_configuration = base.Configuration.WithVersion(RazorLanguageVersion.Version_7_0);
1439+
_configuration = base.Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 };
14401440

14411441
AdditionalSyntaxTrees.Add(Parse("""
14421442
using Microsoft.AspNetCore.Components;
@@ -1490,7 +1490,7 @@ public class MyComponent<T> : ComponentBase
14901490
[IntegrationTestFact, WorkItem("https://github.com/dotnet/aspnetcore/issues/18042")]
14911491
public void AddAttribute_ImplicitStringConversion_CustomEvent()
14921492
{
1493-
_configuration = base.Configuration.WithVersion(RazorLanguageVersion.Version_7_0);
1493+
_configuration = base.Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 };
14941494

14951495
AdditionalSyntaxTrees.Add(Parse("""
14961496
using Microsoft.AspNetCore.Components;
@@ -1545,7 +1545,7 @@ public class MyComponent<T> : ComponentBase
15451545
[IntegrationTestFact, WorkItem("https://github.com/dotnet/aspnetcore/issues/18042")]
15461546
public void AddAttribute_ImplicitStringConversion_BindUnknown()
15471547
{
1548-
_configuration = base.Configuration.WithVersion(RazorLanguageVersion.Version_7_0);
1548+
_configuration = base.Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 };
15491549

15501550
AdditionalSyntaxTrees.Add(Parse("""
15511551
using Microsoft.AspNetCore.Components;
@@ -1578,7 +1578,7 @@ public class MyComponent : ComponentBase
15781578
[IntegrationTestFact, WorkItem("https://github.com/dotnet/aspnetcore/issues/18042")]
15791579
public void AddAttribute_ImplicitStringConversion_BindUnknown_Assignment()
15801580
{
1581-
_configuration = base.Configuration.WithVersion(RazorLanguageVersion.Version_7_0);
1581+
_configuration = base.Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 };
15821582

15831583
AdditionalSyntaxTrees.Add(Parse("""
15841584
using Microsoft.AspNetCore.Components;
@@ -1612,7 +1612,7 @@ public class MyComponent : ComponentBase
16121612
[IntegrationTestFact, WorkItem("https://github.com/dotnet/aspnetcore/issues/18042")]
16131613
public void AddAttribute_ImplicitBooleanConversion()
16141614
{
1615-
_configuration = base.Configuration.WithVersion(RazorLanguageVersion.Version_7_0);
1615+
_configuration = base.Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 };
16161616

16171617
AdditionalSyntaxTrees.Add(Parse("""
16181618
using Microsoft.AspNetCore.Components;
@@ -3926,11 +3926,11 @@ public static class BindAttributes
39263926
[IntegrationTestFact]
39273927
public void BindToComponent_WithGetSet_ProducesErrorOnOlderLanguageVersions()
39283928
{
3929-
_configuration = RazorConfiguration.Create(
3929+
_configuration = new(
39303930
RazorLanguageVersion.Version_6_0,
39313931
"unnamed",
3932-
Array.Empty<RazorExtension>(),
3933-
false);
3932+
Extensions: [],
3933+
UseConsolidatedMvcViews: false);
39343934

39353935
// Arrange
39363936
AdditionalSyntaxTrees.Add(Parse(@"
@@ -9063,7 +9063,7 @@ @preservewhitespace true
90639063
public void Legacy_3_1_LeadingWhiteSpace_WithDirective()
90649064
{
90659065
// Arrange/Act
9066-
_configuration = RazorConfiguration.Create(
9066+
_configuration = new(
90679067
RazorLanguageVersion.Version_3_0,
90689068
base.Configuration.ConfigurationName,
90699069
base.Configuration.Extensions);
@@ -9084,7 +9084,7 @@ @using System
90849084
public void Legacy_3_1_LeadingWhiteSpace_WithCSharpExpression()
90859085
{
90869086
// Arrange/Act
9087-
_configuration = RazorConfiguration.Create(
9087+
_configuration = new(
90889088
RazorLanguageVersion.Version_3_0,
90899089
base.Configuration.ConfigurationName,
90909090
base.Configuration.Extensions);
@@ -9105,7 +9105,7 @@ public void Legacy_3_1_LeadingWhiteSpace_WithCSharpExpression()
91059105
public void Legacy_3_1_LeadingWhiteSpace_WithComponent()
91069106
{
91079107
// Arrange
9108-
_configuration = RazorConfiguration.Create(
9108+
_configuration = new(
91099109
RazorLanguageVersion.Version_3_0,
91109110
base.Configuration.ConfigurationName,
91119111
base.Configuration.Extensions);
@@ -9141,7 +9141,7 @@ public class SomeOtherComponent : ComponentBase
91419141
public void Legacy_3_1_TrailingWhiteSpace_WithDirective()
91429142
{
91439143
// Arrange/Act
9144-
_configuration = RazorConfiguration.Create(
9144+
_configuration = new(
91459145
RazorLanguageVersion.Version_3_0,
91469146
base.Configuration.ConfigurationName,
91479147
base.Configuration.Extensions);
@@ -9163,7 +9163,7 @@ public void Legacy_3_1_TrailingWhiteSpace_WithDirective()
91639163
public void Legacy_3_1_TrailingWhiteSpace_WithCSharpExpression()
91649164
{
91659165
// Arrange/Act
9166-
_configuration = RazorConfiguration.Create(
9166+
_configuration = new(
91679167
RazorLanguageVersion.Version_3_0,
91689168
base.Configuration.ConfigurationName,
91699169
base.Configuration.Extensions);
@@ -9185,7 +9185,7 @@ public void Legacy_3_1_TrailingWhiteSpace_WithCSharpExpression()
91859185
public void Legacy_3_1_TrailingWhiteSpace_WithComponent()
91869186
{
91879187
// Arrange
9188-
_configuration = RazorConfiguration.Create(
9188+
_configuration = new(
91899189
RazorLanguageVersion.Version_3_0,
91909190
base.Configuration.ConfigurationName,
91919191
base.Configuration.Extensions);
@@ -9219,7 +9219,7 @@ public class SomeOtherComponent : ComponentBase
92199219
public void Legacy_3_1_Whitespace_BetweenElementAndFunctions()
92209220
{
92219221
// Arrange
9222-
_configuration = RazorConfiguration.Create(
9222+
_configuration = new(
92239223
RazorLanguageVersion.Version_3_0,
92249224
base.Configuration.ConfigurationName,
92259225
base.Configuration.Extensions);
@@ -9242,7 +9242,7 @@ public void Legacy_3_1_Whitespace_BetweenElementAndFunctions()
92429242
public void Legacy_3_1_WhiteSpace_InsideAttribute_InMarkupBlock()
92439243
{
92449244
// Arrange
9245-
_configuration = RazorConfiguration.Create(
9245+
_configuration = new(
92469246
RazorLanguageVersion.Version_3_0,
92479247
base.Configuration.ConfigurationName,
92489248
base.Configuration.Extensions);
@@ -9260,7 +9260,7 @@ public void Legacy_3_1_WhiteSpace_InsideAttribute_InMarkupBlock()
92609260
public void Legacy_3_1_WhiteSpace_InMarkupInFunctionsBlock()
92619261
{
92629262
// Arrange
9263-
_configuration = RazorConfiguration.Create(
9263+
_configuration = new(
92649264
RazorLanguageVersion.Version_3_0,
92659265
base.Configuration.ConfigurationName,
92669266
base.Configuration.Extensions);
@@ -10340,7 +10340,7 @@ public void ScriptTag_Razor8([CombinatorialValues("8.0", "latest")] string langV
1034010340
var generated = CompileToCSharp("""
1034110341
<script>alert("Hello");</script>
1034210342
""",
10343-
configuration: Configuration.WithVersion(RazorLanguageVersion.Parse(langVersion)));
10343+
configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Parse(langVersion) });
1034410344
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
1034510345
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
1034610346
CompileToAssembly(generated);
@@ -10352,7 +10352,7 @@ public void ScriptTag_Razor7()
1035210352
var generated = CompileToCSharp("""
1035310353
<script>alert("Hello");</script>
1035410354
""",
10355-
configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
10355+
configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 });
1035610356
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
1035710357
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
1035810358
CompileToAssembly(generated);
@@ -10473,7 +10473,7 @@ public void RenderMode_Directive_FullyQualified()
1047310473
{
1047410474
var generated = CompileToCSharp("""
1047510475
@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server
10476-
""", throwOnFailure: true);
10476+
""", throwOnFailure: true);
1047710477

1047810478
// Assert
1047910479
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
@@ -10944,7 +10944,7 @@ @using Microsoft.AspNetCore.Components.Web
1094410944
<div method="post" @onsubmit="() => { }" @formname="named-form-handler"></div>
1094510945
<div method="post" @onsubmit="() => { }" @formname="@("named-form-handler")"></div>
1094610946
""",
10947-
configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
10947+
configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 });
1094810948

1094910949
// Assert
1095010950
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
@@ -11006,7 +11006,7 @@ @using Microsoft.AspNetCore.Components.Web
1100611006
<TestComponent method="post" @onsubmit="() => { }" @formname="named-form-handler" />
1100711007
<TestComponent method="post" @onsubmit="() => { }" @formname="@("named-form-handler")" />
1100811008
""",
11009-
configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
11009+
configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 });
1101011010

1101111011
// Assert
1101211012
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
@@ -11046,7 +11046,7 @@ @typeparam T
1104611046
[Parameter] public T Parameter { get; set; }
1104711047
}
1104811048
""",
11049-
configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
11049+
configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 });
1105011050

1105111051
// Assert
1105211052
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
@@ -11256,7 +11256,7 @@ public void FormName_RazorLangVersion7()
1125611256
@using Microsoft.AspNetCore.Components.Web
1125711257
<form method="post" @onsubmit="() => { }" @formname="named-form-handler"></form>
1125811258
""",
11259-
configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
11259+
configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 });
1126011260

1126111261
// Assert
1126211262
AssertDocumentNodeMatchesBaseline(generated.CodeDocument);

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentRenderModeDirectiveIntegrationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public void LanguageVersion()
122122
{
123123
var compilationResult = CompileToCSharp("""
124124
@rendermode Microsoft.AspNetCore.Components.Web.RenderMode.Server
125-
""", configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
125+
""", configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 });
126126

127127
Assert.Empty(compilationResult.Diagnostics);
128128

@@ -144,7 +144,7 @@ @rendermode Foo
144144
{
145145
string rendermode = "Something";
146146
}
147-
""", configuration: Configuration.WithVersion(RazorLanguageVersion.Version_7_0));
147+
""", configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_7_0 });
148148

149149
Assert.Empty(compilationResult.Diagnostics);
150150

@@ -162,7 +162,7 @@ @rendermode Foo
162162
{
163163
string rendermode = "Something";
164164
}
165-
""", configuration: Configuration.WithVersion(RazorLanguageVersion.Version_8_0));
165+
""", configuration: Configuration with { LanguageVersion = RazorLanguageVersion.Version_8_0 });
166166

167167
Assert.Empty(compilationResult.Diagnostics);
168168

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/CSharp/CompilerFeatures.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static void Register(RazorProjectEngineBuilder builder)
2424
throw new ArgumentNullException(nameof(builder));
2525
}
2626

27-
if (builder.Configuration.LanguageVersion.CompareTo(RazorLanguageVersion.Version_3_0) >= 0)
27+
if (builder.Configuration.LanguageVersion >= RazorLanguageVersion.Version_3_0)
2828
{
2929
builder.Features.Add(new BindTagHelperDescriptorProvider());
3030
builder.Features.Add(new ComponentTagHelperDescriptorProvider());
@@ -36,7 +36,7 @@ public static void Register(RazorProjectEngineBuilder builder)
3636
builder.Features.Add(new DefaultTypeNameFeature());
3737
}
3838

39-
if (builder.Configuration.LanguageVersion.CompareTo(RazorLanguageVersion.Version_8_0) >= 0)
39+
if (builder.Configuration.LanguageVersion >= RazorLanguageVersion.Version_8_0)
4040
{
4141
builder.Features.Add(new RenderModeTagHelperDescriptorProvider());
4242
builder.Features.Add(new FormNameTagHelperDescriptorProvider());

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/AssemblyExtension.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)