Skip to content

Commit a7358fc

Browse files
committed
Address latest batch of feedback: Rename the wrongly named CSharpAssemblyVisitor class, remove its interface. Move the static methods from SymbolFilterFactory to CompositeSymbolFilter, and delete SymbolFilterFactory. Move TryGetRecordConstructor static extension method to a higher assembly. Move the NullableAttributes.cs dependency to the same higher assembly. Move ImplicitSymbolFilter to a higher assembly. Adjust the tests accordingly.
1 parent b385c9c commit a7358fc

14 files changed

+248
-281
lines changed

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/CSharpAssemblyVisitor.cs renamed to src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/CSharpAssemblyDocumentGenerator.cs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@
2020
namespace Microsoft.DotNet.GenAPI;
2121

2222
/// <summary>
23-
/// A class that visits a collection of specified assemblies and generates the corresponding C# document and syntax trees.
23+
/// A class that generates the C# document and syntax trees of a specified collection of assemblies.
2424
/// </summary>
25-
public class CSharpAssemblyVisitor : IAssemblyVisitor
25+
public sealed class CSharpAssemblyDocumentGenerator
2626
{
2727
private readonly ILog _logger;
2828
private readonly IAssemblySymbolLoader _loader;
@@ -37,7 +37,7 @@ public class CSharpAssemblyVisitor : IAssemblyVisitor
3737
private readonly bool _hideImplicitDefaultConstructors;
3838

3939
/// <summary>
40-
/// Initializes a new instance of the <see cref="CSharpAssemblyVisitor"/> class.
40+
/// Initializes a new instance of the <see cref="CSharpAssemblyDocumentGenerator"/> class.
4141
/// </summary>
4242
/// <param name="logger">The logger to use.</param>
4343
/// <param name="loader">The assembly symbol loader to use.</param>
@@ -48,7 +48,7 @@ public class CSharpAssemblyVisitor : IAssemblyVisitor
4848
/// <param name="metadataReferences">The metadata references to use. The default value is <see langword="null"/>.</param>
4949
/// <param name="addPartialModifier">Whether to add the partial modifier or not. The default value is <see langword="true"/>.</param>
5050
/// <param name="hideImplicitDefaultConstructors">Whether to hide implicit default constructors or not. The default value is <see langword="true"/>.</param>
51-
public CSharpAssemblyVisitor(ILog logger,
51+
public CSharpAssemblyDocumentGenerator(ILog logger,
5252
IAssemblySymbolLoader loader,
5353
ISymbolFilter symbolFilter,
5454
ISymbolFilter attributeDataSymbolFilter,
@@ -71,7 +71,11 @@ public CSharpAssemblyVisitor(ILog logger,
7171
_hideImplicitDefaultConstructors = hideImplicitDefaultConstructors;
7272
}
7373

74-
/// <inheritdoc />
74+
/// <summary>
75+
/// Returns the configured source code document for the specified assembly symbol.
76+
/// </summary>
77+
/// <param name="assemblySymbol">The assembly symbol that represents the loaded assembly.</param>
78+
/// <returns>The source code document instance of the specified assembly symbol.</returns>
7579
public Document GetDocumentForAssembly(IAssemblySymbol assemblySymbol)
7680
{
7781
CSharpCompilationOptions compilationOptions = new(OutputKind.DynamicallyLinkedLibrary,
@@ -114,7 +118,11 @@ public Document GetDocumentForAssembly(IAssemblySymbol assemblySymbol)
114118
return document;
115119
}
116120

117-
/// <inheritdoc />
121+
/// <summary>
122+
/// Returns the formatted root syntax node for the specified document.
123+
/// </summary>
124+
/// <param name="document">A source code document instance.</param>
125+
/// <returns>The root syntax node of the specified document.</returns>
118126
public SyntaxNode GetFormattedRootNodeForDocument(Document document) => document.GetSyntaxRootAsync().Result!.Rewrite(new SingleLineStatementCSharpSyntaxRewriter());
119127

120128
private SyntaxNode? Visit(INamespaceSymbol namespaceSymbol)

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/CSharpFileBuilder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public sealed class CSharpFileBuilder : IAssemblySymbolWriter
1818
{
1919
private readonly TextWriter _textWriter;
2020
private readonly string? _header;
21-
private readonly IAssemblyVisitor _assemblyVisitor;
21+
private readonly CSharpAssemblyDocumentGenerator _docGenerator;
2222

2323
public CSharpFileBuilder(ILog logger,
2424
TextWriter textWriter,
@@ -32,15 +32,15 @@ public CSharpFileBuilder(ILog logger,
3232
{
3333
_textWriter = textWriter;
3434
_header = header;
35-
_assemblyVisitor = new CSharpAssemblyVisitor(logger, loader, symbolFilter, attributeDataSymbolFilter, exceptionMessage, includeAssemblyAttributes, metadataReferences);
35+
_docGenerator = new CSharpAssemblyDocumentGenerator(logger, loader, symbolFilter, attributeDataSymbolFilter, exceptionMessage, includeAssemblyAttributes, metadataReferences);
3636
}
3737

3838
/// <inheritdoc />
3939
public void WriteAssembly(IAssemblySymbol assemblySymbol)
4040
{
4141
_textWriter.Write(GetFormattedHeader(_header));
42-
Document document = _assemblyVisitor.GetDocumentForAssembly(assemblySymbol);
43-
_assemblyVisitor.GetFormattedRootNodeForDocument(document).WriteTo(_textWriter);
42+
Document document = _docGenerator.GetDocumentForAssembly(assemblySymbol);
43+
_docGenerator.GetFormattedRootNodeForDocument(document).WriteTo(_textWriter);
4444
}
4545

4646
private static string GetFormattedHeader(string? customHeader)

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/Filtering/ImplicitSymbolFilter.cs

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/GenAPIApp.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#endif
77
using Microsoft.CodeAnalysis;
88
using Microsoft.DotNet.ApiSymbolExtensions;
9+
using Microsoft.DotNet.ApiSymbolExtensions.Filtering;
910
using Microsoft.DotNet.ApiSymbolExtensions.Logging;
1011

1112
namespace Microsoft.DotNet.GenAPI
@@ -38,8 +39,8 @@ public static void Run(ILog logger,
3839
CSharpFileBuilder writer = new(logger,
3940
textWriter,
4041
loader,
41-
SymbolFilterFactory.GetSymbolFilterFromFiles(excludeApiFiles, respectInternals),
42-
SymbolFilterFactory.GetAttributeFilterFromPaths(excludeAttributesFiles, respectInternals),
42+
CompositeSymbolFilter.GetSymbolFilterFromFiles(excludeApiFiles, respectInternals),
43+
CompositeSymbolFilter.GetAttributeFilterFromPaths(excludeAttributesFiles, respectInternals),
4344
headerFile,
4445
exceptionMessage,
4546
includeAssemblyAttributes);

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/IAssemblyVisitor.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/INamedTypeSymbolExtensions.cs

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +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-
using System.Diagnostics.CodeAnalysis;
54
using Microsoft.CodeAnalysis;
65
using Microsoft.CodeAnalysis.CSharp;
76
using Microsoft.CodeAnalysis.CSharp.Syntax;
@@ -70,14 +69,14 @@ private static SyntaxList<AttributeListSyntax> FromAttributeData(IEnumerable<Att
7069
}
7170

7271
// Build dummy field from a type, field name, and attribute list.
73-
private static SyntaxNode CreateDummyField(string typ, string fieldName, SyntaxList<AttributeListSyntax> attrs, bool isReadonly)
72+
private static SyntaxNode CreateDummyField(string type, string fieldName, SyntaxList<AttributeListSyntax> attrs, bool isReadonly)
7473
{
7574
List<SyntaxToken> modifiers = new() { SyntaxFactory.Token(SyntaxKind.PrivateKeyword) };
7675
if (isReadonly)
7776
modifiers.Add(SyntaxFactory.Token(SyntaxKind.ReadOnlyKeyword));
7877
SyntaxNode declaration = SyntaxFactory.FieldDeclaration(
7978
SyntaxFactory.VariableDeclaration(
80-
SyntaxFactory.ParseTypeName(typ))
79+
SyntaxFactory.ParseTypeName(type))
8180
.WithVariables(
8281
SyntaxFactory.SingletonSeparatedList<VariableDeclaratorSyntax>(
8382
SyntaxFactory.VariableDeclarator(
@@ -224,7 +223,7 @@ static bool IncludeInternalSymbols(ISymbolFilter filter) =>
224223
yield return constructor;
225224
}
226225

227-
// Synthesize a base class initializer.
226+
// Synthesize a base class initializer.
228227
public static ConstructorInitializerSyntax GenerateBaseConstructorInitializer(this IMethodSymbol baseTypeConstructor)
229228
{
230229
return SyntaxFactory.ConstructorInitializer(SyntaxKind.BaseConstructorInitializer, baseTypeConstructor.CreateDefaultArgumentList());
@@ -248,33 +247,5 @@ public static ArgumentListSyntax CreateDefaultArgumentList(this IMethodSymbol me
248247

249248
return argumentList;
250249
}
251-
252-
// Locates constructor generated by the compiler for `record Foo(...)` syntax
253-
// If the type is a record and the compiler generated constructor is found it will be returned, otherwise null.
254-
// The compiler will not generate a constructor in the case where the user defined it themself without using an argument list
255-
// in the record declaration, or if the record has no parameters.
256-
public static bool TryGetRecordConstructor(this INamedTypeSymbol type, [NotNullWhen(true)] out IMethodSymbol? recordConstructor)
257-
{
258-
if (!type.IsRecord)
259-
{
260-
recordConstructor = null;
261-
return false;
262-
}
263-
264-
// Locate the compiler generated Deconstruct method.
265-
var deconstructMethod = (IMethodSymbol?)type.GetMembers("Deconstruct")
266-
.FirstOrDefault(m => m is IMethodSymbol && m.IsCompilerGenerated());
267-
268-
// Locate the compiler generated constructor by matching parameters to Deconstruct - since we cannot locate it with an attribute.
269-
recordConstructor = (IMethodSymbol?)type.GetMembers(".ctor")
270-
.FirstOrDefault(m => m is IMethodSymbol method &&
271-
method.MethodKind == MethodKind.Constructor &&
272-
(deconstructMethod == null ?
273-
method.Parameters.IsEmpty :
274-
method.Parameters.Select(p => p.Type).SequenceEqual(
275-
deconstructMethod.Parameters.Select(p => p.Type), SymbolEqualityComparer.Default)));
276-
277-
return recordConstructor != null;
278-
}
279250
}
280251
}

src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/Microsoft.DotNet.GenAPI.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
<TargetFrameworks>$(NetToolMinimum);$(NetFrameworkToolCurrent)</TargetFrameworks>
55
</PropertyGroup>
66

7-
<ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
8-
<Compile Include="$(RepoRoot)src\Common\NullableAttributes.cs" LinkBase="Common" />
9-
</ItemGroup>
10-
117
<ItemGroup>
128
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" />
139
<ProjectReference Include="..\..\Microsoft.DotNet.ApiSymbolExtensions\Microsoft.DotNet.ApiSymbolExtensions.csproj" />

0 commit comments

Comments
 (0)