From 91e04d1eff08e12fb404ec3181ba17211a2f100c Mon Sep 17 00:00:00 2001 From: Owen Smith Date: Mon, 4 Jul 2022 09:12:46 -0400 Subject: [PATCH] use new GetTypesByMetadataName in ImmutabilityContext.Factory Closes: https://github.com/Brightspace/D2L.CodeStyle/issues/851 --- .../ImmutabilityContext.Factory.cs | 52 +++++++------------ 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/src/D2L.CodeStyle.Analyzers/Immutability/ImmutabilityContext.Factory.cs b/src/D2L.CodeStyle.Analyzers/Immutability/ImmutabilityContext.Factory.cs index 11d0f46e..88c2efb7 100644 --- a/src/D2L.CodeStyle.Analyzers/Immutability/ImmutabilityContext.Factory.cs +++ b/src/D2L.CodeStyle.Analyzers/Immutability/ImmutabilityContext.Factory.cs @@ -98,13 +98,11 @@ internal static ImmutabilityContext Create( additionalImmutableTypes = ImmutableHashSet.Empty; } - ImmutableDictionary compilationAssemblies = GetCompilationAssemblies( compilation ); - // Generate a dictionary of types that we have specifically determined // should be considered Immutable by the Analyzer. var extraImmutableTypesBuilder = ImmutableDictionary.CreateBuilder( SymbolEqualityComparer.Default ); foreach( ( string typeName, string qualifiedAssembly ) in DefaultExtraTypes ) { - INamedTypeSymbol type = GetTypeSymbol( compilationAssemblies, compilation, qualifiedAssembly, typeName ); + INamedTypeSymbol type = GetTypeSymbol( compilation, qualifiedAssembly, typeName ); if( type == null ) { continue; @@ -119,7 +117,7 @@ internal static ImmutabilityContext Create( } foreach( string typeName in additionalImmutableTypes ) { - INamedTypeSymbol type = GetTypeSymbol( compilationAssemblies, compilation, qualifiedAssembly: default, typeName ); + INamedTypeSymbol type = GetTypeSymbol( compilation, qualifiedAssembly: default, typeName ); if( type == null ) { continue; @@ -141,7 +139,7 @@ internal static ImmutabilityContext Create( // have a return value which should be considered Immutable by the Analyzer. var knownImmutableReturnsBuilder = ImmutableHashSet.CreateBuilder( SymbolEqualityComparer.Default ); foreach( ( string typeName, string methodName, string qualifiedAssembly ) in KnownImmutableReturningMethods ) { - INamedTypeSymbol type = GetTypeSymbol( compilationAssemblies, compilation, qualifiedAssembly, typeName ); + INamedTypeSymbol type = GetTypeSymbol( compilation, qualifiedAssembly, typeName ); if( type == null ) { continue; @@ -168,45 +166,35 @@ internal static ImmutabilityContext Create( ); } - private static ImmutableDictionary GetCompilationAssemblies( Compilation compilation ) { - var builder = ImmutableDictionary.CreateBuilder(); - - IAssemblySymbol compilationAssmebly = compilation.Assembly; - - builder.Add( compilationAssmebly.Name, compilationAssmebly ); - - foreach( IModuleSymbol module in compilationAssmebly.Modules ) { - foreach( IAssemblySymbol assembly in module.ReferencedAssemblySymbols ) { - builder.Add( assembly.Name, assembly ); - } - } - - return builder.ToImmutable(); - } - private static INamedTypeSymbol GetTypeSymbol( - ImmutableDictionary compilationAssemblies, Compilation compilation, string qualifiedAssembly, string typeName ) { - INamedTypeSymbol type; - if( string.IsNullOrEmpty( qualifiedAssembly ) ) { - type = compilation.GetTypeByMetadataName( typeName ); - } else { - if( !compilationAssemblies.TryGetValue( qualifiedAssembly, out IAssemblySymbol assembly ) ) { - return null; + ImmutableArray types = compilation.GetTypesByMetadataName( typeName ); + + if( types.IsEmpty ) { + return null; + } + + if( qualifiedAssembly == default ) { + if( types.Length > 1 ) { + throw new InvalidOperationException( + $"Found multiple {typeName} with no {nameof( qualifiedAssembly )} specified when building ImmutabilityContext." + ); } - type = assembly.GetTypeByMetadataName( typeName ); + return types[ 0 ]; } - if( type == null || type.Kind == SymbolKind.ErrorType ) { - return null; + foreach( INamedTypeSymbol type in types ) { + if( type.ContainingAssembly.Name.Equals( qualifiedAssembly, StringComparison.Ordinal ) ) { + return type; + } } - return type; + return null; } }