Skip to content

Commit e0c3b42

Browse files
committed
add support for output #nullable directives according to the configurations of TypeDeclarationOptions and MemberDeclarationOptions
1 parent 5c7fe0f commit e0c3b42

File tree

2 files changed

+50
-3
lines changed

2 files changed

+50
-3
lines changed

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriter.cs

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ public void WriteAssemblyInfoHeader()
3232
BaseWriter.WriteLine($"// InformationalVersion: {assembly.GetAssemblyMetadataAttributeValue<AssemblyInformationalVersionAttribute, string>()}");
3333
BaseWriter.WriteLine($"// TargetFramework: {assembly.GetAssemblyMetadataAttributeValue<TargetFrameworkAttribute, string>()}");
3434
BaseWriter.WriteLine($"// Configuration: {assembly.GetAssemblyMetadataAttributeValue<AssemblyConfigurationAttribute, string>()}");
35-
BaseWriter.WriteLine();
3635
}
3736

3837
public void WriteExportedTypes()
@@ -82,6 +81,29 @@ static int OrderOfRootNamespace(string ns)
8281
.OrderBy(OrderOfRootNamespace)
8382
.ThenBy(static ns => ns, StringComparer.Ordinal);
8483

84+
if (options.Writer.WriteNullableAnnotationDirective) {
85+
if (
86+
options.TypeDeclaration.NullabilityInfoContext is not null &&
87+
options.MemberDeclaration.NullabilityInfoContext is not null
88+
) {
89+
BaseWriter.WriteLine("#nullable enable annotations");
90+
BaseWriter.WriteLine();
91+
}
92+
else if (
93+
options.TypeDeclaration.NullabilityInfoContext is null &&
94+
options.MemberDeclaration.NullabilityInfoContext is null
95+
) {
96+
BaseWriter.WriteLine("#nullable disable annotations");
97+
BaseWriter.WriteLine();
98+
}
99+
else {
100+
BaseWriter.WriteLine();
101+
}
102+
}
103+
else {
104+
BaseWriter.WriteLine();
105+
}
106+
85107
foreach (var ns in orderedReferencingNamespaces) {
86108
BaseWriter.WriteLine($"using {ns};");
87109
}
@@ -116,6 +138,15 @@ static int OrderOfType(Type t)
116138
.OrderBy(OrderOfType)
117139
.ThenBy(static type => type.FullName, StringComparer.Ordinal);
118140

141+
var enableNullableAnnotationsOnlyOnTypes =
142+
options.Writer.WriteNullableAnnotationDirective &&
143+
options.TypeDeclaration.NullabilityInfoContext is not null &&
144+
options.MemberDeclaration.NullabilityInfoContext is null;
145+
var enableNullableAnnotationsOnlyOnMembers =
146+
options.Writer.WriteNullableAnnotationDirective &&
147+
options.TypeDeclaration.NullabilityInfoContext is null &&
148+
options.MemberDeclaration.NullabilityInfoContext is not null;
149+
119150
foreach (var type in orderedTypes) {
120151
var isDelegate = type.IsDelegate();
121152

@@ -129,7 +160,9 @@ static int OrderOfType(Type t)
129160
assm,
130161
type,
131162
referencingNamespaces,
132-
options
163+
options,
164+
enableNullableAnnotationsOnlyOnTypes,
165+
enableNullableAnnotationsOnlyOnMembers
133166
)
134167
);
135168
}
@@ -149,7 +182,9 @@ private static string GenerateTypeAndMemberDeclarations(
149182
Assembly assm,
150183
Type t,
151184
ISet<string> referencingNamespaces,
152-
ApiListWriterOptions options
185+
ApiListWriterOptions options,
186+
bool enableNullableAnnotationsOnlyOnTypes,
187+
bool enableNullableAnnotationsOnlyOnMembers
153188
)
154189
{
155190
if (options == null)
@@ -183,6 +218,9 @@ assemblyNameOfTypeForwardedFrom is not null &&
183218
.AppendLine(attr);
184219
}
185220

221+
if (enableNullableAnnotationsOnlyOnTypes)
222+
ret.AppendLine("#nullable enable annotations");
223+
186224
var typeDeclarationLines = Generator.GenerateTypeDeclarationWithExplicitBaseTypeAndInterfaces(t, referencingNamespaces, options).ToList();
187225

188226
for (var index = 0; index < typeDeclarationLines.Count; index++) {
@@ -200,6 +238,11 @@ assemblyNameOfTypeForwardedFrom is not null &&
200238
else
201239
ret.AppendLine(" {");
202240

241+
if (enableNullableAnnotationsOnlyOnTypes)
242+
ret.AppendLine("#nullable restore annotations");
243+
if (enableNullableAnnotationsOnlyOnMembers)
244+
ret.AppendLine("#nullable enable annotations");
245+
203246
ret.Append(
204247
GenerateTypeContentDeclarations(
205248
nestLevel + 1,
@@ -210,6 +253,9 @@ assemblyNameOfTypeForwardedFrom is not null &&
210253
)
211254
);
212255

256+
if (enableNullableAnnotationsOnlyOnMembers)
257+
ret.AppendLine("#nullable restore annotations");
258+
213259
ret.Append(indent).AppendLine("}");
214260
}
215261
else {

src/Smdn.Reflection.ReverseGenerating.ListApi.Core/Smdn.Reflection.ReverseGenerating.ListApi/ApiListWriterOptions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ public class ApiListWriterOptions : GeneratorOptions {
77

88
public class WriterOptions {
99
public bool OrderStaticMembersFirst { get; set; } = false;
10+
public bool WriteNullableAnnotationDirective { get; set; } = true;
1011
}
1112
}

0 commit comments

Comments
 (0)