@@ -16,6 +16,11 @@ namespace MongoDB.Analyzer.Core;
1616
1717internal static class SymbolExtensions
1818{
19+ private const string AssemblyMongoDBDriver = "MongoDB.Driver" ;
20+ private const string NamespaceMongoDBDriver = "MongoDB.Driver" ;
21+ private const string NamespaceMongoDBLinq = "MongoDB.Driver.Linq" ;
22+ private const string NamespaceSystemLinq = "System.Linq" ;
23+
1924 private static readonly HashSet < string > s_supportedCollections = new ( )
2025 {
2126 "System.Collections.Generic.List<T>" ,
@@ -41,9 +46,16 @@ public static bool IsContainedInLambdaOrQueryParameter(this ISymbol symbol, Synt
4146 _ => symbol . IsContainedInLambda ( parentNode )
4247 } ;
4348
44- public static bool IsDefinedInMongoLinq ( this ISymbol symbol ) =>
45- symbol ? . ContainingModule ? . Name ? . ToLowerInvariant ( ) == "mongodb.driver.dll" &&
46- symbol ? . ContainingNamespace ? . Name == "Linq" ;
49+ public static bool IsDefinedInMongoLinqOrSystemLinq ( this ISymbol symbol )
50+ {
51+ var containingNamespace = symbol ? . ContainingNamespace ? . ToDisplayString ( ) ;
52+
53+ // In case of system linq, the containing module is not validated for simplicity,
54+ // as it can differ in different .net frameworks.
55+ return containingNamespace == NamespaceSystemLinq ||
56+ containingNamespace == NamespaceMongoDBLinq &&
57+ symbol ? . ContainingAssembly . Name == AssemblyMongoDBDriver ;
58+ }
4759
4860 public static bool IsBuilder ( this ITypeSymbol typeSymbol ) =>
4961 typeSymbol ? . Name switch
@@ -92,16 +104,16 @@ public static bool IsFindFluentMethod(this IMethodSymbol methodSymbol) =>
92104
93105 public static bool IsFindOptions ( this ITypeSymbol namedTypeSymbol ) =>
94106 namedTypeSymbol ? . Name == "FindOptions" &&
95- namedTypeSymbol ? . ContainingAssembly . Name == "MongoDB.Driver" ;
107+ namedTypeSymbol ? . ContainingAssembly . Name == AssemblyMongoDBDriver ;
96108
97- public static bool IsIMongoCollection ( this ITypeSymbol typeSymbol ) => ImplementsOrIsInterface ( typeSymbol , "MongoDB.Driver" , "IMongoCollection" ) ;
109+ public static bool IsIMongoCollection ( this ITypeSymbol typeSymbol ) => ImplementsOrIsInterface ( typeSymbol , NamespaceMongoDBDriver , "IMongoCollection" ) ;
98110
99111 public static bool IsIMongoQueryable ( this ITypeSymbol typeSymbol ) =>
100- ImplementsOrIsInterface ( typeSymbol , "MongoDB.Driver.Linq" , "IMongoQueryable" ) ||
101- ImplementsOrIsInterface ( typeSymbol , "MongoDB.Driver.Linq" , "IOrderedMongoQueryable" ) ;
112+ ImplementsOrIsInterface ( typeSymbol , NamespaceMongoDBLinq , "IMongoQueryable" ) ||
113+ ImplementsOrIsInterface ( typeSymbol , NamespaceMongoDBLinq , "IOrderedMongoQueryable" ) ;
102114
103- public static bool IsLinqEnumerable ( this ITypeSymbol typeSymbol ) =>
104- typeSymbol ? . Name == "Enumerable" && typeSymbol . ContainingNamespace . Name == "Linq" ;
115+ public static bool IsIQueryable ( this ITypeSymbol typeSymbol ) =>
116+ ImplementsOrIsInterface ( typeSymbol , NamespaceSystemLinq , nameof ( IQueryable ) ) ;
105117
106118 public static bool IsMongoQueryable ( this ITypeSymbol typeSymbol ) =>
107119 typeSymbol ? . Name == "MongoQueryable" ;
@@ -132,11 +144,16 @@ typeSymbol is INamedTypeSymbol namedType &&
132144 namedType . TypeArguments . Length == 1 &&
133145 namedType . TypeArguments [ 0 ] . IsSupportedMongoCollectionType ( ) ;
134146
135- private static bool ImplementsOrIsInterface ( this ITypeSymbol typeSymbol , string @namespace , string interfaceName ) =>
136- typeSymbol ? . TypeKind switch
147+ private static bool ImplementsOrIsInterface ( this ITypeSymbol typeSymbol , string @namespace , string interfaceName )
148+ {
149+ if ( typeSymbol == null )
137150 {
138- TypeKind . Class => typeSymbol . Interfaces . Any ( i => i . Name == interfaceName && i . ContainingNamespace . ToDisplayString ( ) == @namespace ) ,
139- TypeKind . Interface => typeSymbol . Name == interfaceName && typeSymbol . ContainingNamespace . ToDisplayString ( ) == @namespace ,
140- _ => false
141- } ;
151+ return false ;
152+ }
153+
154+ return IsType ( typeSymbol ) || typeSymbol . Interfaces . Any ( IsType ) ;
155+
156+ bool IsType ( ITypeSymbol typeSymbol ) =>
157+ typeSymbol . Name == interfaceName && typeSymbol . ContainingNamespace . ToDisplayString ( ) == @namespace ;
158+ }
142159}
0 commit comments