Skip to content

Commit 381c80d

Browse files
Clean up FieldDeclarationIntermediateNode
- Enable nullability - Add constructor - Remove unnecessary argument null check - Change `Modifiers` property to return an `ImmutableArray<string>` rather than an `IList<string>` (backed by a `List<string>`). - Change `SupressWarnings` property to return an `ImmutableArray<string>` rather than an `IList<string>` (backed by a `List<string>`). - Make `FieldName`, `FieldType`, and `IsTagHelperField` properties read-only.
1 parent fdcf0c9 commit 381c80d

File tree

8 files changed

+72
-90
lines changed

8 files changed

+72
-90
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,11 @@ public void WriteField_WritesFieldDeclaration()
303303
using var writer = new CodeWriter();
304304

305305
// Act
306-
writer.WriteField(Array.Empty<string>(), new[] { "private" }, "global::System.String", "_myString");
306+
writer.WriteField(
307+
suppressWarnings: [],
308+
modifiers: ["private"],
309+
typeName: "global::System.String",
310+
fieldName: "_myString");
307311

308312
// Assert
309313
var output = writer.GetText().ToString();
@@ -320,7 +324,11 @@ public void WriteField_WithModifiers_WritesFieldDeclaration()
320324
using var writer = new CodeWriter();
321325

322326
// Act
323-
writer.WriteField(Array.Empty<string>(), new[] { "private", "readonly", "static" }, "global::System.String", "_myString");
327+
writer.WriteField(
328+
suppressWarnings: [],
329+
modifiers: ["private", "readonly", "static"],
330+
typeName: "global::System.String",
331+
fieldName: "_myString");
324332

325333
// Assert
326334
var output = writer.GetText().ToString();
@@ -338,10 +346,10 @@ public void WriteField_WithModifiersAndSupressions_WritesFieldDeclaration()
338346

339347
// Act
340348
writer.WriteField(
341-
new[] { "0001", "0002", },
342-
new[] { "private", "readonly", "static" },
343-
"global::System.String",
344-
"_myString");
349+
suppressWarnings: ["0001", "0002"],
350+
modifiers: ["private", "readonly", "static"],
351+
typeName: "global::System.String",
352+
fieldName: "_myString");
345353

346354
// Assert
347355
var output = writer.GetText().ToString();
@@ -403,7 +411,7 @@ public void CSharpCodeWriter_RespectTabSetting()
403411

404412
// Act
405413
context.BuildClassDeclaration(modifiers: [], "C", null, interfaces: [], typeParameters: []);
406-
writer.WriteField(Array.Empty<string>(), Array.Empty<string>(), "int", "f");
414+
writer.WriteField(suppressWarnings: [], modifiers: [], typeName: "int", fieldName: "f");
407415

408416
// Assert
409417
var output = writer.GetText().ToString();
@@ -428,7 +436,7 @@ public void CSharpCodeWriter_RespectSpaceSetting()
428436

429437
// Act
430438
context.BuildClassDeclaration(modifiers: [], "C", null, interfaces: [], typeParameters: []);
431-
writer.WriteField(Array.Empty<string>(), Array.Empty<string>(), "int", "f");
439+
writer.WriteField(suppressWarnings: [], modifiers: [], typeName: "int", fieldName: "f");
432440

433441
// Assert
434442
var output = writer.GetText().ToString();

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,16 +367,10 @@ public void WriteDocument_WritesField()
367367
// Arrange
368368
var document = new DocumentIntermediateNode();
369369
var builder = IntermediateNodeBuilder.Create(document);
370-
builder.Add(new FieldDeclarationIntermediateNode()
371-
{
372-
Modifiers =
373-
{
374-
"internal",
375-
"readonly",
376-
},
377-
FieldName = "_foo",
378-
FieldType = "string",
379-
});
370+
builder.Add(new FieldDeclarationIntermediateNode(
371+
fieldName: "_foo",
372+
fieldType: "string",
373+
modifiers: ["internal", "readonly"]));
380374

381375
var codeDocument = TestRazorCodeDocument.CreateEmpty();
382376
var options = RazorCodeGenerationOptions.Default;

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/CodeGeneration/CodeWriterExtensions.cs

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -304,35 +304,20 @@ public static CodeWriter WriteStartInstanceMethodInvocation(this CodeWriter writ
304304
string.Format(CultureInfo.InvariantCulture, InstanceMethodFormat, instanceName, methodName));
305305
}
306306

307-
public static CodeWriter WriteField(this CodeWriter writer, IList<string> suppressWarnings, IList<string> modifiers, string typeName, string fieldName)
307+
public static CodeWriter WriteField(
308+
this CodeWriter writer,
309+
ImmutableArray<string> suppressWarnings,
310+
ImmutableArray<string> modifiers,
311+
string typeName,
312+
string fieldName)
308313
{
309-
if (suppressWarnings == null)
310-
{
311-
throw new ArgumentNullException(nameof(suppressWarnings));
312-
}
313-
314-
if (modifiers == null)
315-
{
316-
throw new ArgumentNullException(nameof(modifiers));
317-
}
318-
319-
if (typeName == null)
320-
{
321-
throw new ArgumentNullException(nameof(typeName));
322-
}
323-
324-
if (fieldName == null)
325-
{
326-
throw new ArgumentNullException(nameof(fieldName));
327-
}
328-
329-
for (var i = 0; i < suppressWarnings.Count; i++)
314+
for (var i = 0; i < suppressWarnings.Length; i++)
330315
{
331316
writer.Write("#pragma warning disable ");
332317
writer.WriteLine(suppressWarnings[i]);
333318
}
334319

335-
for (var i = 0; i < modifiers.Count; i++)
320+
for (var i = 0; i < modifiers.Length; i++)
336321
{
337322
writer.Write(modifiers[i]);
338323
writer.Write(" ");
@@ -344,7 +329,7 @@ public static CodeWriter WriteField(this CodeWriter writer, IList<string> suppre
344329
writer.Write(";");
345330
writer.WriteLine();
346331

347-
for (var i = suppressWarnings.Count - 1; i >= 0; i--)
332+
for (var i = suppressWarnings.Length - 1; i >= 0; i--)
348333
{
349334
writer.Write("#pragma warning restore ");
350335
writer.WriteLine(suppressWarnings[i]);

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/DefaultTagHelperOptimizationPass.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -191,16 +191,13 @@ private void AddField(Context context, TagHelperDescriptor tagHelper)
191191
i++;
192192
}
193193

194-
context.Class.Children.Insert(i, new FieldDeclarationIntermediateNode()
195-
{
196-
IsTagHelperField = true,
197-
Modifiers =
198-
{
199-
"private",
200-
},
201-
FieldName = context.GetFieldName(tagHelper),
202-
FieldType = "global::" + tagHelper.GetTypeName(),
203-
});
194+
var fieldNode = new FieldDeclarationIntermediateNode(
195+
fieldName: context.GetFieldName(tagHelper),
196+
fieldType: "global::" + tagHelper.GetTypeName(),
197+
modifiers: ["private"],
198+
isTagHelperField: true);
199+
200+
context.Class.Children.Insert(i, fieldNode);
204201
}
205202

206203
private bool IsTagHelperRuntimeNode(TagHelperIntermediateNode node)

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Extensions/DefaultTagHelperTargetExtension.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using System;
7+
using System.Collections.Immutable;
78
using System.Diagnostics;
89
using System.Globalization;
910
using System.Linq;
@@ -15,11 +16,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Extensions;
1516

1617
internal sealed class DefaultTagHelperTargetExtension : IDefaultTagHelperTargetExtension
1718
{
18-
private static readonly string[] FieldUnintializedModifiers = { "0649", };
19+
private static readonly ImmutableArray<string> s_fieldUninitializedWarnings = ["0649"];
1920

20-
private static readonly string[] FieldUnusedModifiers = { "0169", };
21+
private static readonly ImmutableArray<string> s_fieldUnusedWarnings = ["0169"];
2122

22-
private static readonly string[] PrivateModifiers = new string[] { "private" };
23+
private static readonly ImmutableArray<string> s_privateModifiers = ["private"];
2324

2425
public string RunnerVariableName { get; set; } = "__tagHelperRunner";
2526

@@ -102,7 +103,7 @@ public void WriteTagHelperBody(CodeRenderingContext context, DefaultTagHelperBod
102103
// Assign a unique ID for this instance of the source HTML tag. This must be unique
103104
// per call site, e.g. if the tag is on the view twice, there should be two IDs.
104105
var uniqueId = GetDeterministicId(context);
105-
106+
106107
context.CodeWriter.WriteStringLiteral(node.TagName)
107108
.WriteParameterSeparator()
108109
.Write(TagModeTypeName)
@@ -474,7 +475,7 @@ public void WriteTagHelperProperty(CodeRenderingContext context, DefaultTagHelpe
474475
public void WriteTagHelperRuntime(CodeRenderingContext context, DefaultTagHelperRuntimeIntermediateNode node)
475476
{
476477
context.CodeWriter.WriteLine("#line hidden");
477-
context.CodeWriter.WriteField(FieldUnintializedModifiers, PrivateModifiers, ExecutionContextTypeName, ExecutionContextVariableName);
478+
context.CodeWriter.WriteField(s_fieldUninitializedWarnings, s_privateModifiers, ExecutionContextTypeName, ExecutionContextVariableName);
478479

479480
context.CodeWriter
480481
.Write("private ")
@@ -487,7 +488,7 @@ public void WriteTagHelperRuntime(CodeRenderingContext context, DefaultTagHelper
487488

488489
if (!context.Options.DesignTime)
489490
{
490-
context.CodeWriter.WriteField(FieldUnusedModifiers, PrivateModifiers, "string", StringValueBufferVariableName);
491+
context.CodeWriter.WriteField(s_fieldUnusedWarnings, s_privateModifiers, "string", StringValueBufferVariableName);
491492

492493
var backedScopeManageVariableName = "__backed" + ScopeManagerVariableName;
493494
context.CodeWriter

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
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-
using System.Collections.Generic;
4+
using System.Collections.Immutable;
85

96
namespace Microsoft.AspNetCore.Razor.Language.Intermediate;
107

11-
public sealed class FieldDeclarationIntermediateNode : MemberDeclarationIntermediateNode
8+
public sealed class FieldDeclarationIntermediateNode(
9+
string fieldName,
10+
string fieldType,
11+
ImmutableArray<string> modifiers,
12+
ImmutableArray<string> suppressWarnings,
13+
bool isTagHelperField = false) : MemberDeclarationIntermediateNode
1214
{
13-
public override IntermediateNodeCollection Children => IntermediateNodeCollection.ReadOnly;
14-
15-
public IList<string> Modifiers { get; } = new List<string>();
15+
public string FieldName { get; } = fieldName;
16+
public string FieldType { get; } = fieldType;
1617

17-
public IList<string> SuppressWarnings { get; } = new List<string>();
18+
public bool IsTagHelperField { get; } = isTagHelperField;
1819

19-
public string FieldName { get; set; }
20+
public ImmutableArray<string> Modifiers { get; } = modifiers.NullToEmpty();
21+
public ImmutableArray<string> SuppressWarnings { get; } = suppressWarnings.NullToEmpty();
2022

21-
public string FieldType { get; set; }
22-
23-
public bool IsTagHelperField { get; set; }
23+
public override IntermediateNodeCollection Children => IntermediateNodeCollection.ReadOnly;
2424

25-
public override void Accept(IntermediateNodeVisitor visitor)
25+
public FieldDeclarationIntermediateNode(
26+
string fieldName,
27+
string fieldType,
28+
ImmutableArray<string> modifiers,
29+
bool isTagHelperField = false)
30+
: this(fieldName, fieldType, modifiers, suppressWarnings: [], isTagHelperField)
2631
{
27-
if (visitor == null)
28-
{
29-
throw new ArgumentNullException(nameof(visitor));
30-
}
31-
32-
visitor.VisitFieldDeclaration(this);
3332
}
3433

34+
public override void Accept(IntermediateNodeVisitor visitor)
35+
=> visitor.VisitFieldDeclaration(this);
36+
3537
public override void FormatNode(IntermediateNodeFormatter formatter)
3638
{
3739
formatter.WriteContent(FieldName);

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

Lines changed: 0 additions & 2 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
namespace Microsoft.AspNetCore.Razor.Language.Intermediate;
75

86
public abstract class MemberDeclarationIntermediateNode : IntermediateNode

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Mvc/ViewComponentTagHelperPass.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,13 @@ private void AddField(Context context, TagHelperDescriptor tagHelper)
123123
i++;
124124
}
125125

126-
context.Class.Children.Insert(i, new FieldDeclarationIntermediateNode()
127-
{
128-
IsTagHelperField = true,
129-
Modifiers =
130-
{
131-
"private",
132-
},
133-
FieldName = context.GetFieldName(tagHelper),
134-
FieldType = "global::" + context.GetFullyQualifiedName(tagHelper),
135-
});
126+
var fieldNode = new FieldDeclarationIntermediateNode(
127+
fieldName: context.GetFieldName(tagHelper),
128+
fieldType: "global::" + context.GetFullyQualifiedName(tagHelper),
129+
modifiers: ["private"],
130+
isTagHelperField: true);
131+
132+
context.Class.Children.Insert(i, fieldNode);
136133
}
137134

138135
private void AddTagHelperClass(Context context, TagHelperDescriptor tagHelper)

0 commit comments

Comments
 (0)