Skip to content

Commit 3423142

Browse files
Remove ItemCollection from CodeRenderingContext (#10764)
Because of a dare from @333fred, I'm currently on a crusade to remove `ItemCollection` from the Razor Compiler completely. Previously, I removed `ItemCollection` from `TagHelperDescriptorProviderContext` (#10720). This time, I'm removing it from `CodeRenderingContext`. It turns out that every `CodeRenderingContext` greedily creates an `ItemCollection`, though there are only ever two things stored there: 1. A string to use for new lines in `CodeWriter`. 2. A string representing "suppress IDs", which is already specified in `RazorCodeGenerationOptions`. These two items were really present as a hook that compiler tests could set. However, it's much easier and less wasteful to elevate both items to `RazorCodeGenerationOptions` and make tests set the options directly. I made a few other refactorings as part of this change: - I merged several abstract base classes with their single default concrete type: - `CodeRenderingContext` and `DefaultCodeRenderingContext` - `RazorCSharpDocument` and `DefaultRazorCSharpDocument`, - `RazorCodeGenerationOptions` and `DefaultRazorCodeGenerationOptions` - `RazorCodeGenerationOptionsBuilder` and `DefaultRazorCodeGenerationOptionsBuilder`. - Reworked `RazorCodeGenerationOptions` and introduced `RazorCodeGenerationOptionsFlags` to track its boolean fields. - Cleaned up `RazorCSharpDocument` and unified its collections on `ImmutableArray<>` rather than `IReadOnlyList<>`. - Enabled nullability annotations for several types. - Used more pooled collections in `CodeRenderingContext`. Note: I was careful with my commit history, and it should be clean enough to review commit-by-commit if that's your preference.
2 parents fc8332f + 934fea8 commit 3423142

File tree

60 files changed

+1538
-1256
lines changed

Some content is hidden

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

60 files changed

+1538
-1256
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class MyService<TModel>
6666
AssertSourceMappingsMatchBaseline(compiled.CodeDocument);
6767

6868
// We expect this test to generate a bunch of errors.
69-
Assert.True(compiled.CodeDocument.GetCSharpDocument().Diagnostics.Count > 0);
69+
Assert.NotEmpty(compiled.CodeDocument.GetCSharpDocument().Diagnostics);
7070
}
7171

7272
[Fact]

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void Execute_NoOps_IfNamespaceNodeIsMissing()
1919
// Arrange
2020
var irDocument = new DocumentIntermediateNode()
2121
{
22-
Options = RazorCodeGenerationOptions.CreateDefault(),
22+
Options = RazorCodeGenerationOptions.Default,
2323
};
2424

2525
var pass = new AssemblyAttributeInjectionPass
@@ -40,7 +40,7 @@ public void Execute_NoOps_IfNamespaceNodeHasEmptyContent()
4040
// Arrange
4141
var irDocument = new DocumentIntermediateNode()
4242
{
43-
Options = RazorCodeGenerationOptions.CreateDefault(),
43+
Options = RazorCodeGenerationOptions.Default,
4444
};
4545
var builder = IntermediateNodeBuilder.Create(irDocument);
4646
var @namespace = new NamespaceDeclarationIntermediateNode() { Content = string.Empty };
@@ -66,7 +66,7 @@ public void Execute_NoOps_IfClassNameNodeIsMissing()
6666
// Arrange
6767
var irDocument = new DocumentIntermediateNode()
6868
{
69-
Options = RazorCodeGenerationOptions.CreateDefault(),
69+
Options = RazorCodeGenerationOptions.Default,
7070
};
7171

7272
var builder = IntermediateNodeBuilder.Create(irDocument);
@@ -93,7 +93,7 @@ public void Execute_NoOps_IfClassNameIsEmpty()
9393
// Arrange
9494
var irDocument = new DocumentIntermediateNode()
9595
{
96-
Options = RazorCodeGenerationOptions.CreateDefault(),
96+
Options = RazorCodeGenerationOptions.Default,
9797
};
9898
var builder = IntermediateNodeBuilder.Create(irDocument);
9999
var @namespace = new NamespaceDeclarationIntermediateNode
@@ -134,7 +134,7 @@ public void Execute_NoOps_IfDocumentIsNotViewOrPage()
134134
var irDocument = new DocumentIntermediateNode
135135
{
136136
DocumentKind = "Default",
137-
Options = RazorCodeGenerationOptions.CreateDefault(),
137+
Options = RazorCodeGenerationOptions.Default,
138138
};
139139
var builder = IntermediateNodeBuilder.Create(irDocument);
140140
var @namespace = new NamespaceDeclarationIntermediateNode() { Content = "SomeNamespace" };
@@ -170,7 +170,7 @@ public void Execute_NoOps_ForDesignTime()
170170
var irDocument = new DocumentIntermediateNode
171171
{
172172
DocumentKind = MvcViewDocumentClassifierPass.MvcViewDocumentKind,
173-
Options = RazorCodeGenerationOptions.CreateDesignTimeDefault(),
173+
Options = RazorCodeGenerationOptions.DesignTimeDefault,
174174
};
175175
var builder = IntermediateNodeBuilder.Create(irDocument);
176176
var @namespace = new NamespaceDeclarationIntermediateNode
@@ -217,7 +217,7 @@ public void Execute_AddsRazorViewAttribute_ToViews()
217217
var irDocument = new DocumentIntermediateNode
218218
{
219219
DocumentKind = MvcViewDocumentClassifierPass.MvcViewDocumentKind,
220-
Options = RazorCodeGenerationOptions.CreateDefault(),
220+
Options = RazorCodeGenerationOptions.Default,
221221
};
222222
var builder = IntermediateNodeBuilder.Create(irDocument);
223223
var @namespace = new NamespaceDeclarationIntermediateNode
@@ -270,7 +270,7 @@ public void Execute_EscapesViewPathWhenAddingAttributeToViews()
270270
var irDocument = new DocumentIntermediateNode
271271
{
272272
DocumentKind = MvcViewDocumentClassifierPass.MvcViewDocumentKind,
273-
Options = RazorCodeGenerationOptions.CreateDefault(),
273+
Options = RazorCodeGenerationOptions.Default,
274274
};
275275
var builder = IntermediateNodeBuilder.Create(irDocument);
276276
var @namespace = new NamespaceDeclarationIntermediateNode
@@ -323,7 +323,7 @@ public void Execute_AddsRazorPagettribute_ToPage()
323323
var irDocument = new DocumentIntermediateNode
324324
{
325325
DocumentKind = RazorPageDocumentClassifierPass.RazorPageDocumentKind,
326-
Options = RazorCodeGenerationOptions.CreateDefault(),
326+
Options = RazorCodeGenerationOptions.Default,
327327
};
328328
var builder = IntermediateNodeBuilder.Create(irDocument);
329329
var pageDirective = new DirectiveIntermediateNode
@@ -383,7 +383,7 @@ public void Execute_EscapesViewPathAndRouteWhenAddingAttributeToPage()
383383
var irDocument = new DocumentIntermediateNode
384384
{
385385
DocumentKind = MvcViewDocumentClassifierPass.MvcViewDocumentKind,
386-
Options = RazorCodeGenerationOptions.CreateDefault(),
386+
Options = RazorCodeGenerationOptions.Default,
387387
};
388388
var builder = IntermediateNodeBuilder.Create(irDocument);
389389
var @namespace = new NamespaceDeclarationIntermediateNode

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public void InstrumentationPass_NoOps_ForDesignTime()
1818
// Arrange
1919
var document = new DocumentIntermediateNode()
2020
{
21-
Options = RazorCodeGenerationOptions.CreateDesignTimeDefault(),
21+
Options = RazorCodeGenerationOptions.DesignTimeDefault,
2222
};
2323

2424
var builder = IntermediateNodeBuilder.Create(document);
@@ -50,7 +50,7 @@ public void InstrumentationPass_InstrumentsHtml()
5050
// Arrange
5151
var document = new DocumentIntermediateNode()
5252
{
53-
Options = RazorCodeGenerationOptions.CreateDefault(),
53+
Options = RazorCodeGenerationOptions.Default,
5454
};
5555

5656
var builder = IntermediateNodeBuilder.Create(document);
@@ -89,7 +89,7 @@ public void InstrumentationPass_SkipsHtml_WithoutLocation()
8989
// Arrange
9090
var document = new DocumentIntermediateNode()
9191
{
92-
Options = RazorCodeGenerationOptions.CreateDefault(),
92+
Options = RazorCodeGenerationOptions.Default,
9393
};
9494

9595
var builder = IntermediateNodeBuilder.Create(document);
@@ -121,7 +121,7 @@ public void InstrumentationPass_InstrumentsCSharpExpression()
121121
// Arrange
122122
var document = new DocumentIntermediateNode()
123123
{
124-
Options = RazorCodeGenerationOptions.CreateDefault(),
124+
Options = RazorCodeGenerationOptions.Default,
125125
};
126126

127127
var builder = IntermediateNodeBuilder.Create(document);
@@ -157,7 +157,7 @@ public void InstrumentationPass_SkipsCSharpExpression_WithoutLocation()
157157
// Arrange
158158
var document = new DocumentIntermediateNode()
159159
{
160-
Options = RazorCodeGenerationOptions.CreateDefault(),
160+
Options = RazorCodeGenerationOptions.Default,
161161
};
162162

163163
var builder = IntermediateNodeBuilder.Create(document);
@@ -188,7 +188,7 @@ public void InstrumentationPass_SkipsCSharpExpression_InsideTagHelperAttribute()
188188
// Arrange
189189
var document = new DocumentIntermediateNode()
190190
{
191-
Options = RazorCodeGenerationOptions.CreateDefault(),
191+
Options = RazorCodeGenerationOptions.Default,
192192
};
193193

194194
var builder = IntermediateNodeBuilder.Create(document);
@@ -239,7 +239,7 @@ public void InstrumentationPass_SkipsCSharpExpression_InsideTagHelperProperty()
239239
// Arrange
240240
var document = new DocumentIntermediateNode()
241241
{
242-
Options = RazorCodeGenerationOptions.CreateDefault(),
242+
Options = RazorCodeGenerationOptions.Default,
243243
};
244244

245245
var builder = IntermediateNodeBuilder.Create(document);
@@ -290,7 +290,7 @@ public void InstrumentationPass_InstrumentsTagHelper()
290290
// Arrange
291291
var document = new DocumentIntermediateNode()
292292
{
293-
Options = RazorCodeGenerationOptions.CreateDefault(),
293+
Options = RazorCodeGenerationOptions.Default,
294294
};
295295

296296
var builder = IntermediateNodeBuilder.Create(document);
@@ -321,7 +321,7 @@ public void InstrumentationPass_SkipsTagHelper_WithoutLocation()
321321
// Arrange
322322
var document = new DocumentIntermediateNode()
323323
{
324-
Options = RazorCodeGenerationOptions.CreateDefault(),
324+
Options = RazorCodeGenerationOptions.Default,
325325
};
326326

327327
var builder = IntermediateNodeBuilder.Create(document);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public class MyService<TModel>
8585
AssertCSharpDocumentMatchesBaseline(compiled.CodeDocument.GetCSharpDocument());
8686

8787
// We expect this test to generate a bunch of errors.
88-
Assert.True(compiled.CodeDocument.GetCSharpDocument().Diagnostics.Count > 0);
88+
Assert.NotEmpty(compiled.CodeDocument.GetCSharpDocument().Diagnostics);
8989
}
9090

9191
[Fact]
@@ -536,7 +536,7 @@ public class MyService<TModel>
536536
AssertSourceMappingsMatchBaseline(compiled.CodeDocument);
537537

538538
// We expect this test to generate a bunch of errors.
539-
Assert.True(compiled.CodeDocument.GetCSharpDocument().Diagnostics.Count > 0);
539+
Assert.NotEmpty(compiled.CodeDocument.GetCSharpDocument().Diagnostics);
540540
}
541541

542542
[Fact]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public class MyService<TModel>
9292
AssertLinePragmas(compiled.CodeDocument, designTime: false);
9393

9494
// We expect this test to generate a bunch of errors.
95-
Assert.True(compiled.CodeDocument.GetCSharpDocument().Diagnostics.Count > 0);
95+
Assert.NotEmpty(compiled.CodeDocument.GetCSharpDocument().Diagnostics);
9696
}
9797

9898
[Fact]
@@ -949,7 +949,7 @@ public class MyService<TModel>
949949
AssertSourceMappingsMatchBaseline(compiled.CodeDocument);
950950

951951
// We expect this test to generate a bunch of errors.
952-
Assert.True(compiled.CodeDocument.GetCSharpDocument().Diagnostics.Count > 0);
952+
Assert.NotEmpty(compiled.CodeDocument.GetCSharpDocument().Diagnostics);
953953
}
954954

955955
[Fact]

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CSharpCodeWriterTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ public void CSharpCodeWriter_RespectTabSetting()
402402
o.IndentSize = 4;
403403
});
404404

405-
using var writer = new CodeWriter(Environment.NewLine, options);
405+
using var writer = new CodeWriter(options);
406406

407407
// Act
408408
writer.BuildClassDeclaration(Array.Empty<string>(), "C", "", Array.Empty<string>(), Array.Empty<TypeParameter>(), context: null);
@@ -428,7 +428,7 @@ public void CSharpCodeWriter_RespectSpaceSetting()
428428
o.IndentSize = 4;
429429
});
430430

431-
using var writer = new CodeWriter(Environment.NewLine, options);
431+
using var writer = new CodeWriter(options);
432432

433433
// Act
434434
writer.BuildClassDeclaration(Array.Empty<string>(), "C", "", Array.Empty<string>(), Array.Empty<TypeParameter>(), context: null);

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/CodeTargetTest.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void CreateDefault_CreatesDefaultCodeTarget()
1515
{
1616
// Arrange
1717
var codeDocument = TestRazorCodeDocument.CreateEmpty();
18-
var options = RazorCodeGenerationOptions.CreateDefault();
18+
var options = RazorCodeGenerationOptions.Default;
1919

2020
// Act
2121
var target = CodeTarget.CreateDefault(codeDocument, options);
@@ -32,7 +32,7 @@ public void CreateDefault_CallsDelegate()
3232
Action<CodeTargetBuilder> @delegate = (b) => { wasCalled = true; };
3333

3434
var codeDocument = TestRazorCodeDocument.CreateEmpty();
35-
var options = RazorCodeGenerationOptions.CreateDefault();
35+
var options = RazorCodeGenerationOptions.Default;
3636

3737
// Act
3838
CodeTarget.CreateDefault(codeDocument, options, @delegate);
@@ -46,7 +46,7 @@ public void CreateDefault_AllowsNullDelegate()
4646
{
4747
// Arrange
4848
var codeDocument = TestRazorCodeDocument.CreateEmpty();
49-
var options = RazorCodeGenerationOptions.CreateDefault();
49+
var options = RazorCodeGenerationOptions.Default;
5050

5151
// Act
5252
CodeTarget.CreateDefault(codeDocument, options, configure: null);
@@ -59,7 +59,7 @@ public void CreateEmpty_AllowsNullDelegate()
5959
{
6060
// Arrange
6161
var codeDocument = TestRazorCodeDocument.CreateEmpty();
62-
var options = RazorCodeGenerationOptions.CreateDefault();
62+
var options = RazorCodeGenerationOptions.Default;
6363

6464
// Act
6565
CodeTarget.CreateDefault(codeDocument, options, configure: null);

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DefaultCodeTargetBuilderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Build_CreatesDefaultCodeTarget()
1414
{
1515
// Arrange
1616
var codeDocument = TestRazorCodeDocument.CreateEmpty();
17-
var options = RazorCodeGenerationOptions.CreateDefault();
17+
var options = RazorCodeGenerationOptions.Default;
1818

1919
var builder = new DefaultCodeTargetBuilder(codeDocument, options);
2020

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/CodeGeneration/DefaultCodeTargetTest.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class DefaultCodeTargetTest
1414
public void Constructor_CreatesDefensiveCopy()
1515
{
1616
// Arrange
17-
var options = RazorCodeGenerationOptions.CreateDefault();
17+
var options = RazorCodeGenerationOptions.Default;
1818

1919
var extensions = new ICodeTargetExtension[]
2020
{
@@ -33,7 +33,7 @@ public void Constructor_CreatesDefensiveCopy()
3333
public void CreateWriter_DesignTime_CreatesDesignTimeNodeWriter()
3434
{
3535
// Arrange
36-
var options = RazorCodeGenerationOptions.CreateDesignTimeDefault();
36+
var options = RazorCodeGenerationOptions.DesignTimeDefault;
3737
var target = new DefaultCodeTarget(options, Enumerable.Empty<ICodeTargetExtension>());
3838

3939
// Act
@@ -47,7 +47,7 @@ public void CreateWriter_DesignTime_CreatesDesignTimeNodeWriter()
4747
public void CreateWriter_Runtime_CreatesRuntimeNodeWriter()
4848
{
4949
// Arrange
50-
var options = RazorCodeGenerationOptions.CreateDefault();
50+
var options = RazorCodeGenerationOptions.Default;
5151
var target = new DefaultCodeTarget(options, Enumerable.Empty<ICodeTargetExtension>());
5252

5353
// Act
@@ -61,7 +61,7 @@ public void CreateWriter_Runtime_CreatesRuntimeNodeWriter()
6161
public void HasExtension_ReturnsTrue_WhenExtensionFound()
6262
{
6363
// Arrange
64-
var options = RazorCodeGenerationOptions.CreateDefault();
64+
var options = RazorCodeGenerationOptions.Default;
6565

6666
var extensions = new ICodeTargetExtension[]
6767
{
@@ -82,7 +82,7 @@ public void HasExtension_ReturnsTrue_WhenExtensionFound()
8282
public void HasExtension_ReturnsFalse_WhenExtensionNotFound()
8383
{
8484
// Arrange
85-
var options = RazorCodeGenerationOptions.CreateDefault();
85+
var options = RazorCodeGenerationOptions.Default;
8686

8787
var extensions = new ICodeTargetExtension[]
8888
{
@@ -103,7 +103,7 @@ public void HasExtension_ReturnsFalse_WhenExtensionNotFound()
103103
public void GetExtension_ReturnsExtension_WhenExtensionFound()
104104
{
105105
// Arrange
106-
var options = RazorCodeGenerationOptions.CreateDefault();
106+
var options = RazorCodeGenerationOptions.Default;
107107

108108
var extensions = new ICodeTargetExtension[]
109109
{
@@ -124,7 +124,7 @@ public void GetExtension_ReturnsExtension_WhenExtensionFound()
124124
public void GetExtension_ReturnsFirstMatch_WhenExtensionFound()
125125
{
126126
// Arrange
127-
var options = RazorCodeGenerationOptions.CreateDefault();
127+
var options = RazorCodeGenerationOptions.Default;
128128

129129
var extensions = new ICodeTargetExtension[]
130130
{
@@ -148,7 +148,7 @@ public void GetExtension_ReturnsFirstMatch_WhenExtensionFound()
148148
public void GetExtension_ReturnsNull_WhenExtensionNotFound()
149149
{
150150
// Arrange
151-
var options = RazorCodeGenerationOptions.CreateDefault();
151+
var options = RazorCodeGenerationOptions.Default;
152152

153153
var extensions = new ICodeTargetExtension[]
154154
{

0 commit comments

Comments
 (0)