Skip to content

Commit 117c8df

Browse files
authored
VS-113: Analyze POCOs that are used by LINQ or Builders at verbosity medium setting (#51)
1 parent 53c8f40 commit 117c8df

File tree

3 files changed

+69
-10
lines changed

3 files changed

+69
-10
lines changed

src/MongoDB.Analyzer/Core/Poco/PocoExpressionProcessor.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ private static bool IsValidBsonAttribute(MongoAnalysisContext context, Attribute
8484
private static bool PreanalyzeClassDeclaration(MongoAnalysisContext context, INamedTypeSymbol classSymbol) =>
8585
context.Settings.PocoAnalysisVerbosity == PocoAnalysisVerbosity.All ||
8686
(classSymbol != null &&
87-
(ContainsBsonAttributes(context, classSymbol) ||
88-
ContainsPropertiesWithBsonAttributes(context, classSymbol) ||
89-
ContainsFieldsWithBsonAttributes(context, classSymbol)));
87+
(context.TypesProcessor.IsUserTypeProcessed(classSymbol) ||
88+
ContainsBsonAttributes(context, classSymbol) ||
89+
ContainsPropertiesWithBsonAttributes(context, classSymbol) ||
90+
ContainsFieldsWithBsonAttributes(context, classSymbol)));
9091
}

src/MongoDB.Analyzer/Core/TypesProcessor.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,15 @@ public MemberDeclarationSyntax GetTypeSymbolToMemberDeclarationMapping(ITypeSymb
4545
return null;
4646
}
4747

48-
public string GetTypeSymbolToGeneratedTypeMapping(ITypeSymbol typeSymbol) => GetGeneratedTypeMapping(typeSymbol).RemappedName;
48+
public bool IsUserTypeProcessed(ITypeSymbol typeSymbol) =>
49+
GetGeneratedTypeMapping(typeSymbol, true).RemappedName != null;
50+
51+
public string GetTypeSymbolToGeneratedTypeMapping(ITypeSymbol typeSymbol) =>
52+
GetGeneratedTypeMapping(typeSymbol, false).RemappedName;
4953

5054
public string ProcessTypeSymbol(ITypeSymbol typeSymbol)
5155
{
52-
var (remappedName, fullTypeName) = GetGeneratedTypeMapping(typeSymbol);
56+
var (remappedName, fullTypeName) = GetGeneratedTypeMapping(typeSymbol, false);
5357
if (fullTypeName == null)
5458
{
5559
return null;
@@ -74,15 +78,15 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol)
7478
return remappedName;
7579
}
7680

77-
private (string RemappedName, string FullTypeName) GetGeneratedTypeMapping(ITypeSymbol typeSymbol)
81+
private (string RemappedName, string FullTypeName) GetGeneratedTypeMapping(ITypeSymbol typeSymbol, bool userOnlyTypes)
7882
{
7983
if (typeSymbol == null)
8084
{
8185
return default;
8286
}
8387

8488
var fullTypeName = GetFullName(typeSymbol);
85-
if (typeSymbol.IsSupportedBsonType(fullTypeName))
89+
if (!userOnlyTypes && typeSymbol.IsSupportedBsonType(fullTypeName))
8690
{
8791
return (typeSymbol.Name, fullTypeName);
8892
}
@@ -92,7 +96,7 @@ public string ProcessTypeSymbol(ITypeSymbol typeSymbol)
9296
return (result.NewName, fullTypeName);
9397
}
9498

95-
if (typeSymbol.IsSupportedSystemType(fullTypeName))
99+
if (!userOnlyTypes && typeSymbol.IsSupportedSystemType(fullTypeName))
96100
{
97101
return (fullTypeName, fullTypeName);
98102
}

tests/MongoDB.Analyzer.Tests.Common.TestCases/Poco/PocoVerbosity.cs

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,45 @@
1313
// limitations under the License.
1414

1515
using MongoDB.Bson.Serialization.Attributes;
16+
using MongoDB.Driver;
1617

1718
namespace MongoDB.Analyzer.Tests.Common.TestCases.Poco
1819
{
1920
public sealed class PocoVerbosity : TestCasesBase
2021
{
22+
[NoDiagnostics(pocoAnalysisVerbosity: PocoAnalysisVerbosity.None)]
23+
public void Airline_not_used_in_expression1()
24+
{
25+
}
26+
2127
[NoDiagnostics(pocoAnalysisVerbosity: PocoAnalysisVerbosity.Medium)]
22-
public void Airline()
28+
public void Airline_not_used_in_expression2()
29+
{
30+
}
31+
32+
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", PocoAnalysisVerbosity.All)]
33+
public void Airline_not_used_in_expression3()
2334
{
2435
}
2536

37+
[NoDiagnostics(pocoAnalysisVerbosity: PocoAnalysisVerbosity.None)]
38+
public void Airline_used_in_expression1()
39+
{
40+
_ = Builders<TestClasses.Airline_used_in_expression1>.Filter.Eq(u => u.AirlineName, "Lufthansa");
41+
}
42+
43+
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", PocoAnalysisVerbosity.Medium)]
44+
public void Airline_used_in_expression2()
45+
{
46+
_ = Builders<TestClasses.Airline_used_in_expression2>.Filter.Eq(u => u.AirlineName, "Lufthansa");
47+
}
48+
49+
[PocoJson("{ \"AirlineName\" : \"Radiant Skies Aviation\" }", PocoAnalysisVerbosity.All)]
50+
public void Airline_used_in_expression3()
51+
{
52+
_ = Builders<TestClasses.Airline_used_in_expression3>.Filter.Eq(u => u.AirlineName, "Lufthansa");
53+
}
54+
2655
[PocoJson("{ \"StringProperty\" : \"StringProperty_val\" }", pocoAnalysisVerbosity: PocoAnalysisVerbosity.Medium)]
2756
public void ClassWithBsonAttributes()
2857
{
@@ -65,7 +94,32 @@ public void ClassWithPropertyAndFieldAttributes2()
6594

6695
public class TestClasses
6796
{
68-
public class Airline
97+
public class Airline_not_used_in_expression1
98+
{
99+
public string AirlineName { get; set; }
100+
}
101+
102+
public class Airline_not_used_in_expression2
103+
{
104+
public string AirlineName { get; set; }
105+
}
106+
107+
public class Airline_not_used_in_expression3
108+
{
109+
public string AirlineName { get; set; }
110+
}
111+
112+
public class Airline_used_in_expression1
113+
{
114+
public string AirlineName { get; set; }
115+
}
116+
117+
public class Airline_used_in_expression2
118+
{
119+
public string AirlineName { get; set; }
120+
}
121+
122+
public class Airline_used_in_expression3
69123
{
70124
public string AirlineName { get; set; }
71125
}

0 commit comments

Comments
 (0)