Skip to content

Commit 1468dab

Browse files
Change Write(Auto)PropertyDeclaration extension to target CodeRenderingContext
Because `CodeRenderingContext` owns the `CodeWriter` instance used for code gen, and the `WritePropertyDeclaration` and `WriteAutoPropertyDeclaration` extension methods target `CodeWriter` and also take a `CodeRenderingContext`, we can change them can target `CodeRenderingContext`.
1 parent c1d8b55 commit 1468dab

File tree

10 files changed

+170
-129
lines changed

10 files changed

+170
-129
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,13 @@ public void WriteField_WithModifiersAndSupressions_WritesFieldDeclaration()
368368
public void WriteAutoPropertyDeclaration_WritesPropertyDeclaration()
369369
{
370370
// Arrange
371-
using var writer = new CodeWriter();
371+
using var context = TestCodeRenderingContext.Create();
372372

373373
// Act
374-
writer.WriteAutoPropertyDeclaration(new[] { "public" }, "global::System.String", "MyString");
374+
context.WriteAutoPropertyDeclaration(["public"], "global::System.String", "MyString");
375375

376376
// Assert
377-
var output = writer.GetText().ToString();
377+
var output = context.CodeWriter.GetText().ToString();
378378
Assert.Equal("""
379379
public global::System.String MyString { get; set; }
380380
@@ -385,13 +385,13 @@ public void WriteAutoPropertyDeclaration_WritesPropertyDeclaration()
385385
public void WriteAutoPropertyDeclaration_WithModifiers_WritesPropertyDeclaration()
386386
{
387387
// Arrange
388-
using var writer = new CodeWriter();
388+
using var context = TestCodeRenderingContext.Create();
389389

390390
// Act
391-
writer.WriteAutoPropertyDeclaration(new[] { "public", "static" }, "global::System.String", "MyString");
391+
context.WriteAutoPropertyDeclaration(["public", "static"], "global::System.String", "MyString");
392392

393393
// Assert
394-
var output = writer.GetText().ToString();
394+
var output = context.CodeWriter.GetText().ToString();
395395
Assert.Equal("""
396396
public static global::System.String MyString { get; set; }
397397

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

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,85 @@ static void WriteWithPragma(CodeWriter writer, string content, CodeRenderingCont
194194
}
195195
}
196196
}
197+
198+
public static void WritePropertyDeclaration(
199+
this CodeRenderingContext context,
200+
ImmutableArray<string> modifiers,
201+
IntermediateToken type,
202+
string propertyName,
203+
string propertyExpression)
204+
{
205+
context.WritePropertyDeclarationPreamble(modifiers, type.Content, propertyName, type.Source, propertySpan: null);
206+
207+
var writer = context.CodeWriter;
208+
writer.Write(" => ");
209+
writer.Write(propertyExpression);
210+
writer.WriteLine(";");
211+
}
212+
213+
public static void WriteAutoPropertyDeclaration(
214+
this CodeRenderingContext context,
215+
ImmutableArray<string> modifiers,
216+
string typeName,
217+
string propertyName,
218+
SourceSpan? typeSpan = null,
219+
SourceSpan? propertySpan = null,
220+
bool privateSetter = false,
221+
bool defaultValue = false)
222+
{
223+
context.WritePropertyDeclarationPreamble(modifiers, typeName, propertyName, typeSpan, propertySpan);
224+
225+
var writer = context.CodeWriter;
226+
227+
writer.Write(" { get;");
228+
229+
if (privateSetter)
230+
{
231+
writer.Write(" private");
232+
}
233+
234+
writer.Write(" set; }");
235+
writer.WriteLine();
236+
237+
if (defaultValue && context?.Options.SuppressNullabilityEnforcement == false && context?.Options.DesignTime == false)
238+
{
239+
writer.WriteLine(" = default!;");
240+
}
241+
}
242+
243+
private static void WritePropertyDeclarationPreamble(
244+
this CodeRenderingContext context,
245+
ImmutableArray<string> modifiers,
246+
string typeName,
247+
string propertyName,
248+
SourceSpan? typeSpan,
249+
SourceSpan? propertySpan)
250+
{
251+
var writer = context.CodeWriter;
252+
253+
foreach (var modifier in modifiers)
254+
{
255+
writer.Write(modifier);
256+
writer.Write(" ");
257+
}
258+
259+
WriteToken(context, typeName, typeSpan);
260+
writer.Write(" ");
261+
WriteToken(context, propertyName, propertySpan);
262+
263+
static void WriteToken(CodeRenderingContext context, string content, SourceSpan? span)
264+
{
265+
if (span is not null && context.Options.DesignTime == false)
266+
{
267+
using (context.CodeWriter.BuildEnhancedLinePragma(span, context))
268+
{
269+
context.CodeWriter.Write(content);
270+
}
271+
}
272+
else
273+
{
274+
context.CodeWriter.Write(content);
275+
}
276+
}
277+
}
197278
}

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

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -351,67 +351,6 @@ public static CodeWriter WriteMethodInvocation(this CodeWriter writer, string me
351351
.WriteEndMethodInvocation(endLine);
352352
}
353353

354-
public static CodeWriter WritePropertyDeclaration(this CodeWriter writer, IList<string> modifiers, IntermediateToken type, string propertyName, string propertyExpression, CodeRenderingContext context)
355-
{
356-
WritePropertyDeclarationPreamble(writer, modifiers, type.Content, propertyName, type.Source, propertySpan: null, context);
357-
writer.Write(" => ");
358-
writer.Write(propertyExpression);
359-
writer.WriteLine(";");
360-
return writer;
361-
}
362-
363-
public static CodeWriter WriteAutoPropertyDeclaration(this CodeWriter writer, IList<string> modifiers, string typeName, string propertyName, SourceSpan? typeSpan = null, SourceSpan? propertySpan = null, CodeRenderingContext context = null, bool privateSetter = false, bool defaultValue = false)
364-
{
365-
ArgHelper.ThrowIfNull(modifiers);
366-
ArgHelper.ThrowIfNull(typeName);
367-
ArgHelper.ThrowIfNull(propertyName);
368-
369-
WritePropertyDeclarationPreamble(writer, modifiers, typeName, propertyName, typeSpan, propertySpan, context);
370-
371-
writer.Write(" { get;");
372-
if (privateSetter)
373-
{
374-
writer.Write(" private");
375-
}
376-
writer.Write(" set; }");
377-
writer.WriteLine();
378-
379-
if (defaultValue && context?.Options.SuppressNullabilityEnforcement == false && context?.Options.DesignTime == false)
380-
{
381-
writer.WriteLine(" = default!;");
382-
}
383-
384-
return writer;
385-
}
386-
387-
private static void WritePropertyDeclarationPreamble(CodeWriter writer, IList<string> modifiers, string typeName, string propertyName, SourceSpan? typeSpan, SourceSpan? propertySpan, CodeRenderingContext context)
388-
{
389-
for (var i = 0; i < modifiers.Count; i++)
390-
{
391-
writer.Write(modifiers[i]);
392-
writer.Write(" ");
393-
}
394-
395-
WriteToken(writer, typeName, typeSpan, context);
396-
writer.Write(" ");
397-
WriteToken(writer, propertyName, propertySpan, context);
398-
399-
static void WriteToken(CodeWriter writer, string content, SourceSpan? span, CodeRenderingContext context)
400-
{
401-
if (span is not null && context?.Options.DesignTime == false)
402-
{
403-
using (writer.BuildEnhancedLinePragma(span, context))
404-
{
405-
writer.Write(content);
406-
}
407-
}
408-
else
409-
{
410-
writer.Write(content);
411-
}
412-
}
413-
}
414-
415354
/// <summary>
416355
/// Writes an "@" character if the provided identifier needs escaping in c#
417356
/// </summary>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ public override void VisitFieldDeclaration(FieldDeclarationIntermediateNode node
201201

202202
public override void VisitPropertyDeclaration(PropertyDeclarationIntermediateNode node)
203203
{
204-
CodeWriter.WritePropertyDeclaration(node.Modifiers, node.PropertyType, node.PropertyName, node.PropertyExpression, _context);
204+
_context.WritePropertyDeclaration(node.Modifiers, node.PropertyType, node.PropertyName, node.PropertyExpression);
205205
}
206206

207207
public override void VisitExtension(ExtensionIntermediateNode node)

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/Components/ComponentInjectIntermediateNode.cs

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

66
using System;
7-
using System.Collections.Generic;
7+
using System.Collections.Immutable;
88
using Microsoft.AspNetCore.Razor.Language.CodeGeneration;
99
using Microsoft.AspNetCore.Razor.Language.Extensions;
1010
using Microsoft.AspNetCore.Razor.Language.Intermediate;
@@ -13,11 +13,11 @@ namespace Microsoft.AspNetCore.Razor.Language.Components;
1313

1414
internal class ComponentInjectIntermediateNode : ExtensionIntermediateNode
1515
{
16-
private static readonly IList<string> _injectedPropertyModifiers = new[]
17-
{
18-
$"[global::{ComponentsApi.InjectAttribute.FullTypeName}]",
19-
"private" // Encapsulation is the default
20-
};
16+
private static readonly ImmutableArray<string> s_injectedPropertyModifiers =
17+
[
18+
$"[global::{ComponentsApi.InjectAttribute.FullTypeName}]",
19+
"private" // Encapsulation is the default
20+
];
2121

2222
public ComponentInjectIntermediateNode(string typeName, string memberName, SourceSpan? typeSpan, SourceSpan? memberSpan, bool isMalformed)
2323
{
@@ -73,13 +73,12 @@ public override void WriteNode(CodeTarget target, CodeRenderingContext context)
7373

7474
if (!context.Options.DesignTime || !IsMalformed)
7575
{
76-
context.CodeWriter.WriteAutoPropertyDeclaration(
77-
_injectedPropertyModifiers,
76+
context.WriteAutoPropertyDeclaration(
77+
s_injectedPropertyModifiers,
7878
TypeName,
7979
memberName,
8080
TypeSpan,
8181
MemberSpan,
82-
context,
8382
defaultValue: true);
8483
}
8584
}

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Mvc.Version1_X/ViewComponentTagHelperTargetExtension.cs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,23 +78,26 @@ public void WriteViewComponentTagHelper(CodeRenderingContext context, ViewCompon
7878
value: null);
7979

8080
// Add constructor.
81-
WriteConstructorString(context.CodeWriter, node.ClassName);
81+
WriteConstructorString(context, node.ClassName);
8282

8383
// Add attributes.
84-
WriteAttributeDeclarations(context.CodeWriter, node.TagHelper);
84+
WriteAttributeDeclarations(context, node.TagHelper);
8585

8686
// Add process method.
87-
WriteProcessMethodString(context.CodeWriter, node.TagHelper);
87+
WriteProcessMethodString(context, node.TagHelper);
8888
}
8989
}
9090

91-
private void WriteConstructorString(CodeWriter writer, string className)
91+
private void WriteConstructorString(CodeRenderingContext context, string className)
9292
{
93+
var writer = context.CodeWriter;
94+
9395
writer.Write("public ")
9496
.Write(className)
9597
.Write("(")
9698
.Write($"{ViewComponentHelperTypeName} helper")
9799
.WriteLine(")");
100+
98101
using (writer.BuildScope())
99102
{
100103
writer.WriteStartAssignment(ViewComponentHelperVariableName)
@@ -103,22 +106,24 @@ private void WriteConstructorString(CodeWriter writer, string className)
103106
}
104107
}
105108

106-
private void WriteAttributeDeclarations(CodeWriter writer, TagHelperDescriptor tagHelper)
109+
private void WriteAttributeDeclarations(CodeRenderingContext context, TagHelperDescriptor tagHelper)
107110
{
111+
var writer = context.CodeWriter;
112+
108113
writer.Write("[")
109114
.Write(HtmlAttributeNotBoundAttributeTypeName)
110115
.WriteParameterSeparator()
111116
.Write(ViewContextAttributeTypeName)
112117
.WriteLine("]");
113118

114-
writer.WriteAutoPropertyDeclaration(
119+
context.WriteAutoPropertyDeclaration(
115120
s_modifiers,
116121
ViewContextTypeName,
117122
ViewContextPropertyName);
118123

119124
foreach (var attribute in tagHelper.BoundAttributes)
120125
{
121-
writer.WriteAutoPropertyDeclaration(
126+
context.WriteAutoPropertyDeclaration(
122127
s_modifiers,
123128
attribute.TypeName,
124129
attribute.GetPropertyName());
@@ -132,17 +137,19 @@ private void WriteAttributeDeclarations(CodeWriter writer, TagHelperDescriptor t
132137
}
133138
}
134139

135-
private void WriteProcessMethodString(CodeWriter writer, TagHelperDescriptor tagHelper)
140+
private void WriteProcessMethodString(CodeRenderingContext context, TagHelperDescriptor tagHelper)
136141
{
142+
var writer = context.CodeWriter;
143+
137144
using (writer.BuildMethodDeclaration(
138-
$"public override async",
139-
$"global::{typeof(Task).FullName}",
140-
TagHelperProcessMethodName,
141-
new Dictionary<string, string>()
142-
{
143-
{ TagHelperContextTypeName, TagHelperContextVariableName },
144-
{ TagHelperOutputTypeName, TagHelperOutputVariableName }
145-
}))
145+
$"public override async",
146+
$"global::{typeof(Task).FullName}",
147+
TagHelperProcessMethodName,
148+
new Dictionary<string, string>()
149+
{
150+
{ TagHelperContextTypeName, TagHelperContextVariableName },
151+
{ TagHelperOutputTypeName, TagHelperOutputVariableName }
152+
}))
146153
{
147154
writer.WriteInstanceMethodInvocation(
148155
$"({ViewComponentHelperVariableName} as {IViewContextAwareTypeName})?",

0 commit comments

Comments
 (0)