Skip to content

Commit d8c74d8

Browse files
committed
refactor
1 parent cf77323 commit d8c74d8

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/Smdn.Reflection.ReverseGenerating/Smdn.Reflection.ReverseGenerating/CSharpTypeFormatter.FormatTypeNameWithNullabilityAnnotation.cs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ namespace Smdn.Reflection.ReverseGenerating;
1515
#pragma warning disable IDE0040
1616
static partial class CSharpFormatter {
1717
#pragma warning restore IDE0040
18-
private static readonly string NullableAnnotationSyntaxString = "?";
19-
20-
private static string GetNullabilityAnnotation(NullabilityInfo target)
21-
=> target.ReadState == NullabilityState.Nullable || target.WriteState == NullabilityState.Nullable
22-
? NullableAnnotationSyntaxString
23-
: string.Empty;
24-
2518
private static StringBuilder FormatTypeNameWithNullabilityAnnotation(
2619
NullabilityInfo target,
2720
StringBuilder builder,
2821
FormatTypeNameOptions options
2922
)
3023
{
24+
const string NullableAnnotationSyntaxString = "?";
25+
26+
static string GetNullabilityAnnotation(NullabilityInfo target)
27+
=> target.ReadState == NullabilityState.Nullable || target.WriteState == NullabilityState.Nullable
28+
? NullableAnnotationSyntaxString
29+
: string.Empty;
30+
3131
Type? byRefParameterType = null;
3232

3333
if (target.Type.IsByRef && options.AttributeProvider is ParameterInfo p) {
@@ -62,7 +62,8 @@ FormatTypeNameOptions options
6262
return builder.Append(FormatTypeNameCore(targetType, options));
6363

6464
// special case for value tuples (ValueTuple<>)
65-
return FormatValueTuple(target, builder, options);
65+
return FormatValueTupleType(target, builder, options)
66+
.Append(GetNullabilityAnnotation(target));
6667
}
6768

6869
var isGenericTypeClosedOrDefinition =
@@ -79,7 +80,9 @@ FormatTypeNameOptions options
7980
// nullable value types (Nullable<>)
8081
if (IsValueTupleType(nullableUnderlyingType)) {
8182
// special case for nullable value tuples (Nullable<ValueTuple<>>)
82-
return FormatValueTuple(target, builder, options).Append(nullabilityAnnotationForByRefParameter);
83+
return FormatValueTupleType(target, builder, options)
84+
.Append(GetNullabilityAnnotation(target))
85+
.Append(nullabilityAnnotationForByRefParameter);
8386
}
8487
else if (nullableUnderlyingType.IsGenericType) {
8588
// case for nullable generic value types (Nullable<GenericValueType<>>)
@@ -92,11 +95,15 @@ FormatTypeNameOptions options
9295
}
9396
else if (isGenericTypeClosedOrDefinition) {
9497
// other generic types
95-
if (targetType == byRefParameterType)
98+
if (targetType == byRefParameterType) {
9699
// TODO: cannot get NullabilityInfo of generic type arguments from by-ref parameter type
97100
return builder.Append(FormatTypeNameCore(targetType, options));
98-
else
99-
return FormatGenericTypeClosedOrDefinition(target, builder, options);
101+
}
102+
else {
103+
return FormatClosedGenericTypeOrGenericTypeDefinition(target, builder, options)
104+
.Append(GetNullabilityAnnotation(target))
105+
.Append(nullabilityAnnotationForByRefParameter);
106+
}
100107
}
101108

102109
if (options.TranslateLanguagePrimitiveType && IsLanguagePrimitiveType(targetType, out var n)) {
@@ -124,7 +131,7 @@ FormatTypeNameOptions options
124131
.Append(nullabilityAnnotationForByRefParameter);
125132
}
126133

127-
private static StringBuilder FormatGenericTypeClosedOrDefinition(
134+
private static StringBuilder FormatClosedGenericTypeOrGenericTypeDefinition(
128135
NullabilityInfo target,
129136
StringBuilder builder,
130137
FormatTypeNameOptions options
@@ -167,10 +174,10 @@ FormatTypeNameOptions options
167174
builder.Append('>');
168175
}
169176

170-
return builder.Append(GetNullabilityAnnotation(target));
177+
return builder;
171178
}
172179

173-
private static StringBuilder FormatValueTuple(
180+
private static StringBuilder FormatValueTupleType(
174181
NullabilityInfo target,
175182
StringBuilder builder,
176183
FormatTypeNameOptions options
@@ -203,7 +210,7 @@ FormatTypeNameOptions options
203210
builder.Append(' ').Append(tupleItemNames[i].Value);
204211
}
205212

206-
return builder.Append(')').Append(GetNullabilityAnnotation(target));
213+
return builder.Append(')');
207214
}
208215

209216
private static StringBuilder FormatNullableGenericValueType(
@@ -216,7 +223,12 @@ FormatTypeNameOptions options
216223
builder.Append(target.Type.Namespace).Append('.');
217224

218225
builder
219-
.Append(target.Type.GenericTypeArguments[0].GetGenericTypeName()) // the name of GenericValueType of Nullable<GenericValueType<>>
226+
.Append(
227+
GetTypeName(
228+
target.Type.GenericTypeArguments[0], // the type of GenericValueType of Nullable<GenericValueType<>>
229+
options
230+
)
231+
)
220232
.Append('<');
221233

222234
for (var i = 0; i < target.GenericTypeArguments.Length; i++) {

0 commit comments

Comments
 (0)