Skip to content

Commit 9277fd6

Browse files
Clean up NamespaceDeclarationIntermediateNode
- Enable nullability - Add constructor - Make `IsPrimaryNamespace` property read-only - Remove unnecessary argument null check - Lazily create `Children` collection - General clean up
1 parent a2c7019 commit 9277fd6

File tree

5 files changed

+31
-63
lines changed

5 files changed

+31
-63
lines changed

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

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,9 @@ public void Execute_NoOps_IfNamespaceNodeHasEmptyContent()
3333
var documentNode = new DocumentIntermediateNode() { Options = codeDocument.CodeGenerationOptions };
3434

3535
var builder = IntermediateNodeBuilder.Create(documentNode);
36-
var @namespace = new NamespaceDeclarationIntermediateNode()
36+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
3737
{
38-
Content = string.Empty,
39-
IsPrimaryNamespace = true,
38+
Content = string.Empty
4039
};
4140

4241
builder.Push(@namespace);
@@ -77,10 +76,9 @@ public void Execute_NoOps_IfClassNameIsEmpty()
7776

7877
var builder = IntermediateNodeBuilder.Create(documentNode);
7978

80-
var @namespace = new NamespaceDeclarationIntermediateNode()
79+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
8180
{
82-
Content = "SomeNamespace",
83-
IsPrimaryNamespace = true,
81+
Content = "SomeNamespace"
8482
};
8583

8684
builder.Push(@namespace);
@@ -143,10 +141,9 @@ public void Execute_NoOps_ForDesignTime()
143141
};
144142

145143
var builder = IntermediateNodeBuilder.Create(documentNode);
146-
var @namespace = new NamespaceDeclarationIntermediateNode
144+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
147145
{
148-
Content = "SomeNamespace",
149-
IsPrimaryNamespace = true,
146+
Content = "SomeNamespace"
150147
};
151148

152149
builder.Push(@namespace);
@@ -184,10 +181,9 @@ public void Execute_AddsRazorViewAttribute_ToViews()
184181

185182
var builder = IntermediateNodeBuilder.Create(documentNode);
186183

187-
var @namespace = new NamespaceDeclarationIntermediateNode
184+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
188185
{
189-
Content = "SomeNamespace",
190-
IsPrimaryNamespace = true,
186+
Content = "SomeNamespace"
191187
};
192188

193189
builder.Push(@namespace);
@@ -231,10 +227,9 @@ public void Execute_EscapesViewPathWhenAddingAttributeToViews()
231227

232228
var builder = IntermediateNodeBuilder.Create(documentNode);
233229

234-
var @namespace = new NamespaceDeclarationIntermediateNode
230+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
235231
{
236-
Content = "SomeNamespace",
237-
IsPrimaryNamespace = true,
232+
Content = "SomeNamespace"
238233
};
239234

240235
builder.Push(@namespace);
@@ -286,10 +281,9 @@ public void Execute_AddsRazorPagettribute_ToPage()
286281

287282
builder.Add(pageDirective);
288283

289-
var @namespace = new NamespaceDeclarationIntermediateNode
284+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
290285
{
291-
Content = "SomeNamespace",
292-
IsPrimaryNamespace = true,
286+
Content = "SomeNamespace"
293287
};
294288

295289
builder.Push(@namespace);
@@ -335,10 +329,9 @@ public void Execute_EscapesViewPathAndRouteWhenAddingAttributeToPage()
335329

336330
var builder = IntermediateNodeBuilder.Create(documentNode);
337331

338-
var @namespace = new NamespaceDeclarationIntermediateNode
332+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
339333
{
340-
Content = "SomeNamespace",
341-
IsPrimaryNamespace = true,
334+
Content = "SomeNamespace"
342335
};
343336
builder.Push(@namespace);
344337

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Extensions/MetadataAttributePassTest.cs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ public void Execute_NoNamespaceSet_Noops()
101101
};
102102

103103
var builder = IntermediateNodeBuilder.Create(documentNode);
104-
var @namespace = new NamespaceDeclarationIntermediateNode
105-
{
106-
IsPrimaryNamespace = true,
107-
};
104+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true);
108105

109106
builder.Push(@namespace);
110107

@@ -150,9 +147,8 @@ public void Execute_NoClassNameSet_Noops()
150147
};
151148

152149
var builder = IntermediateNodeBuilder.Create(documentNode);
153-
var @namespace = new NamespaceDeclarationIntermediateNode
150+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
154151
{
155-
IsPrimaryNamespace = true,
156152
Content = "Some.Namespace"
157153
};
158154

@@ -182,9 +178,8 @@ public void Execute_NoDocumentKind_Noops()
182178
var documentNode = new DocumentIntermediateNode();
183179

184180
var builder = IntermediateNodeBuilder.Create(documentNode);
185-
var @namespace = new NamespaceDeclarationIntermediateNode
181+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
186182
{
187-
IsPrimaryNamespace = true,
188183
Content = "Some.Namespace"
189184
};
190185

@@ -219,9 +214,8 @@ public void Execute_NoIdentifier_Noops()
219214
};
220215

221216
var builder = IntermediateNodeBuilder.Create(documentNode);
222-
var @namespace = new NamespaceDeclarationIntermediateNode
217+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
223218
{
224-
IsPrimaryNamespace = true,
225219
Content = "Some.Namespace"
226220
};
227221

@@ -256,9 +250,8 @@ public void Execute_HasRequiredInfo_AddsItemAndSourceChecksum()
256250
};
257251

258252
var builder = IntermediateNodeBuilder.Create(documentNode);
259-
var @namespace = new NamespaceDeclarationIntermediateNode
253+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
260254
{
261-
IsPrimaryNamespace = true,
262255
Content = "Some.Namespace"
263256
};
264257

@@ -304,9 +297,8 @@ public void Execute_HasRequiredInfo_AndImport_AddsItemAndSourceChecksum()
304297
};
305298

306299
var builder = IntermediateNodeBuilder.Create(documentNode);
307-
var @namespace = new NamespaceDeclarationIntermediateNode
300+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
308301
{
309-
IsPrimaryNamespace = true,
310302
Content = "Some.Namespace"
311303
};
312304

@@ -364,9 +356,8 @@ public void Execute_SuppressMetadataSourceChecksumAttributes_DoesNotGenerateSour
364356
};
365357

366358
var builder = IntermediateNodeBuilder.Create(documentNode);
367-
var @namespace = new NamespaceDeclarationIntermediateNode
359+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true)
368360
{
369-
IsPrimaryNamespace = true,
370361
Content = "Some.Namespace"
371362
};
372363

src/Compiler/Microsoft.AspNetCore.Razor.Language/test/Intermediate/DocumentIntermediateNodeExtensionsTest.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
64
using Xunit;
75

86
namespace Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -54,10 +52,7 @@ public void FindPrimaryNamespace_FindsNamespaceWithAnnotation()
5452
{
5553
// Arrange
5654
var document = new DocumentIntermediateNode();
57-
var @namespace = new NamespaceDeclarationIntermediateNode
58-
{
59-
IsPrimaryNamespace = true
60-
};
55+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true);
6156

6257
var builder = IntermediateNodeBuilder.Create(document);
6358
builder.Add(@namespace);

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,7 @@ private void Rewrite(RazorCodeDocument codeDocument, DocumentIntermediateNode do
5353
var children = new List<IntermediateNode>(documentNode.Children);
5454
documentNode.Children.Clear();
5555

56-
var @namespace = new NamespaceDeclarationIntermediateNode
57-
{
58-
IsPrimaryNamespace = true
59-
};
56+
var @namespace = new NamespaceDeclarationIntermediateNode(isPrimaryNamespace: true);
6057

6158
var @class = new ClassDeclarationIntermediateNode
6259
{

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Intermediate/NamespaceDeclarationIntermediateNode.cs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,23 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
#nullable disable
5-
6-
using System;
7-
84
namespace Microsoft.AspNetCore.Razor.Language.Intermediate;
95

10-
public sealed class NamespaceDeclarationIntermediateNode : IntermediateNode
6+
public sealed class NamespaceDeclarationIntermediateNode(bool isPrimaryNamespace = false) : IntermediateNode
117
{
12-
public override IntermediateNodeCollection Children { get; } = new IntermediateNodeCollection();
8+
private IntermediateNodeCollection? _children;
139

14-
public string Content { get; set; }
10+
public bool IsPrimaryNamespace { get; } = isPrimaryNamespace;
1511

16-
public bool IsPrimaryNamespace { get; set; }
12+
public string? Content { get; set; }
1713

1814
public bool IsGenericTyped { get; set; }
1915

20-
public override void Accept(IntermediateNodeVisitor visitor)
21-
{
22-
if (visitor == null)
23-
{
24-
throw new ArgumentNullException(nameof(visitor));
25-
}
16+
public override IntermediateNodeCollection Children
17+
=> _children ??= [];
2618

27-
visitor.VisitNamespaceDeclaration(this);
28-
}
19+
public override void Accept(IntermediateNodeVisitor visitor)
20+
=> visitor.VisitNamespaceDeclaration(this);
2921

3022
public override void FormatNode(IntermediateNodeFormatter formatter)
3123
{

0 commit comments

Comments
 (0)