Skip to content

Commit 4e5deb8

Browse files
Clean up PropertyDeclarationIntermediateNode
- 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>`). - Make `PropertyName`, `ProeprtyType`, and `PropertyExpression` properties read-only.
1 parent 381c80d commit 4e5deb8

File tree

3 files changed

+24
-38
lines changed

3 files changed

+24
-38
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -401,17 +401,11 @@ public void WriteDocument_WritesProperty()
401401
// Arrange
402402
var document = new DocumentIntermediateNode();
403403
var builder = IntermediateNodeBuilder.Create(document);
404-
builder.Add(new PropertyDeclarationIntermediateNode()
405-
{
406-
Modifiers =
407-
{
408-
"internal",
409-
"virtual",
410-
},
411-
PropertyName = "Foo",
412-
PropertyType = IntermediateToken.CreateCSharpToken("string"),
413-
PropertyExpression = "default"
414-
});
404+
builder.Add(new PropertyDeclarationIntermediateNode(
405+
propertyName: "Foo",
406+
propertyType: IntermediateToken.CreateCSharpToken("string"),
407+
propertyExpression: "default",
408+
modifiers: ["internal", "virtual"]));
415409

416410
var codeDocument = TestRazorCodeDocument.CreateEmpty();
417411
var options = RazorCodeGenerationOptions.Default;
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,24 @@
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 PropertyDeclarationIntermediateNode : MemberDeclarationIntermediateNode
8+
public sealed class PropertyDeclarationIntermediateNode(
9+
string propertyName,
10+
IntermediateToken propertyType,
11+
string propertyExpression,
12+
ImmutableArray<string> modifiers) : MemberDeclarationIntermediateNode
1213
{
13-
public override IntermediateNodeCollection Children => IntermediateNodeCollection.ReadOnly;
14-
15-
public IList<string> Modifiers { get; } = new List<string>();
14+
public string PropertyName { get; } = propertyName;
15+
public IntermediateToken PropertyType { get; } = propertyType;
16+
public string PropertyExpression { get; } = propertyExpression;
1617

17-
public string PropertyName { get; set; }
18+
public ImmutableArray<string> Modifiers { get; } = modifiers.NullToEmpty();
1819

19-
public IntermediateToken PropertyType { get; set; }
20-
21-
public string PropertyExpression { get; set; }
20+
public override IntermediateNodeCollection Children => IntermediateNodeCollection.ReadOnly;
2221

2322
public override void Accept(IntermediateNodeVisitor visitor)
24-
{
25-
if (visitor == null)
26-
{
27-
throw new ArgumentNullException(nameof(visitor));
28-
}
29-
30-
visitor.VisitPropertyDeclaration(this);
31-
}
23+
=> visitor.VisitPropertyDeclaration(this);
3224
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentInte
4848
}
4949
else
5050
{
51-
@class.Children.Add(new PropertyDeclarationIntermediateNode()
52-
{
53-
Modifiers = { "public" },
54-
PropertyName = "Model",
55-
PropertyType = modelType,
56-
PropertyExpression = "ViewData.Model"
57-
});
51+
var propertyNode = new PropertyDeclarationIntermediateNode(
52+
propertyName: "Model",
53+
propertyType: modelType,
54+
propertyExpression: "ViewData.Model",
55+
modifiers: ["public"]);
56+
57+
@class.Children.Add(propertyNode);
5858
}
5959

6060
static string nullableEnable(bool nullableEnabled, string code)

0 commit comments

Comments
 (0)