-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve detection of IStructId namespace from compilation
The existing approach of using analyzer config options breaks if there are transitive project references involved, since the options will contain potentially the wrong namespace (the one for the project being built rather than the one where IStructId actually exists). This changes the approach to looking up the types by their metadata name plus a codegen attribute which we assume users won't be using for their own code (even if they do happen to use `IStructId` for some other purpose).
- Loading branch information
Showing
12 changed files
with
89 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,32 @@ | ||
using Microsoft.CodeAnalysis; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
|
||
namespace StructId; | ||
|
||
/// <summary> | ||
/// Provides access to some common types and properties used in the compilation. | ||
/// </summary> | ||
/// <param name="Compilation">The compilation used to resolve the known types.</param> | ||
/// <param name="StructIdNamespace">The namespace for StructId types.</param> | ||
public record KnownTypes(Compilation Compilation, string StructIdNamespace) | ||
public record KnownTypes(Compilation Compilation) | ||
{ | ||
public string StructIdNamespace => IStructId?.ContainingNamespace.ToFullName() ?? "StructId"; | ||
|
||
/// <summary> | ||
/// System.String | ||
/// </summary> | ||
public INamedTypeSymbol String { get; } = Compilation.GetTypeByMetadataName("System.String")!; | ||
|
||
/// <summary> | ||
/// StructId.IStructId | ||
/// </summary> | ||
public INamedTypeSymbol? IStructId { get; } = Compilation.GetTypeByMetadataName($"{StructIdNamespace}.IStructId"); | ||
public INamedTypeSymbol? IStructId { get; } = Compilation | ||
.GetAllTypes(true) | ||
.FirstOrDefault(x => x.MetadataName == "IStructId" && x.IsGeneratedByStructId()); | ||
|
||
/// <summary> | ||
/// StructId.IStructId{T} | ||
/// </summary> | ||
public INamedTypeSymbol? IStructIdT { get; } = Compilation.GetTypeByMetadataName($"{StructIdNamespace}.IStructId`1"); | ||
/// <summary> | ||
/// StructId.TStructIdAttribute | ||
/// </summary> | ||
public INamedTypeSymbol? TStructId { get; } = Compilation.GetTypeByMetadataName($"{StructIdNamespace}.TStructIdAttribute"); | ||
public INamedTypeSymbol? IStructIdT { get; } = Compilation | ||
.GetAllTypes(true) | ||
.FirstOrDefault(x => x.MetadataName == "IStructId`1" && x.IsGeneratedByStructId()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,3 @@ | ||
<Project> | ||
|
||
<ItemGroup> | ||
<CompilerVisibleProperty Include="StructIdNamespace" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters