Skip to content

Commit ccf33c9

Browse files
Merge branch 'main' into use-expressions-based-properties
2 parents a46e649 + 7eab515 commit ccf33c9

File tree

46 files changed

+372
-237
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+372
-237
lines changed

Source/Csla.Analyzers/Csla.Analyzers/AsynchronousBusinessRuleInheritingFromBusinessRuleAnalyzer.cs

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
4747
if (!methodNode.ContainsDiagnostics)
4848
{
4949
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
50+
if (methodSymbol is null)
51+
{
52+
return;
53+
}
54+
5055
var typeSymbol = methodSymbol.ContainingType;
5156

5257
if(typeSymbol.IsBusinessRule() && methodSymbol.Name == "Execute" && methodSymbol.IsAsync)

Source/Csla.Analyzers/Csla.Analyzers/AsynchronousBusinessRuleInheritingFromBusinessRuleChangeToBusinessRuleAsyncCodeFix.cs

+19-5
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,31 @@ public sealed class AsynchronousBusinessRuleInheritingFromBusinessRuleChangeToBu
3636
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3737
{
3838
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
39-
39+
if (root is null)
40+
{
41+
return;
42+
}
4043
context.CancellationToken.ThrowIfCancellationRequested();
4144

4245
var diagnostic = context.Diagnostics.First();
4346
var methodNode = root.FindNode(diagnostic.Location.SourceSpan) as MethodDeclarationSyntax;
47+
if (methodNode is null)
48+
{
49+
return;
50+
}
4451

4552
context.CancellationToken.ThrowIfCancellationRequested();
4653
await AddCodeFixAsync(context, root, diagnostic, methodNode);
4754
}
4855

49-
private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root,
50-
Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
56+
private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root, Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
5157
{
5258
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken);
5359
var methodSymbol = model.GetDeclaredSymbol(methodNode);
60+
if (methodSymbol is null)
61+
{
62+
return;
63+
}
5464
var typeSymbol = methodSymbol.ContainingType;
5565

5666
var newRoot = root;
@@ -91,13 +101,17 @@ private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode roo
91101
AsynchronousBusinessRuleInheritingFromBusinessRuleChangeToBusinessRuleAsyncCodeFixConstants.UpdateToAsyncEquivalentsDescription), diagnostic);
92102
}
93103

94-
private static BaseListSyntax GetBaseTypes(TypeDeclarationSyntax typeNode)
104+
private static BaseListSyntax? GetBaseTypes(TypeDeclarationSyntax typeNode)
95105
{
96106
var currentBaseList = typeNode.BaseList;
107+
if (currentBaseList is null)
108+
{
109+
return null;
110+
}
97111

98112
var list = new SeparatedSyntaxList<BaseTypeSyntax>();
99113

100-
foreach (var baseTypeNode in typeNode.BaseList.DescendantNodes().OfType<SimpleBaseTypeSyntax>())
114+
foreach (var baseTypeNode in typeNode.BaseList!.DescendantNodes().OfType<SimpleBaseTypeSyntax>())
101115
{
102116
var baseTypeNodeIdentifier = baseTypeNode.DescendantNodes().OfType<IdentifierNameSyntax>().Single();
103117

Source/Csla.Analyzers/Csla.Analyzers/BusinessRuleDoesNotUseAddMethodsOnContextAnalyzer.cs

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
4747
if (!methodNode.ContainsDiagnostics)
4848
{
4949
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
50+
if (methodSymbol is null)
51+
{
52+
return;
53+
}
5054
var typeSymbol = methodSymbol.ContainingType;
5155

5256
if (typeSymbol.IsBusinessRule() &&

Source/Csla.Analyzers/Csla.Analyzers/CheckConstructorsAnalyzer.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
5656
var classNode = (ClassDeclarationSyntax)context.Node;
5757
var classSymbol = context.SemanticModel.GetDeclaredSymbol(classNode);
5858

59-
if (classSymbol.IsStereotype() && !(classSymbol?.IsAbstract).Value)
59+
if (classSymbol.IsStereotype() && !classSymbol.IsAbstract)
6060
{
61-
foreach (var constructor in classSymbol?.Constructors)
61+
foreach (var constructor in classSymbol.Constructors)
6262
{
6363
if (!constructor.IsStatic)
6464
{
@@ -86,7 +86,7 @@ private static void AnalyzeClassDeclaration(SyntaxNodeAnalysisContext context)
8686

8787
if (!hasPublicNoArgumentConstructor)
8888
{
89-
var properties = new Dictionary<string, string>
89+
var properties = new Dictionary<string, string?>
9090
{
9191
[PublicNoArgumentConstructorIsMissingConstants.HasNonPublicNoArgumentConstructor] = hasNonPublicNoArgumentConstructor.ToString()
9292
}.ToImmutableDictionary();

Source/Csla.Analyzers/Csla.Analyzers/CheckConstructorsAnalyzerPublicConstructorCodeFix.cs

+14-5
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,18 @@ public sealed class CheckConstructorsAnalyzerPublicConstructorCodeFix
3535
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3636
{
3737
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
38-
38+
if (root is null)
39+
{
40+
return;
41+
}
3942
context.CancellationToken.ThrowIfCancellationRequested();
4043

4144
var diagnostic = context.Diagnostics.First();
4245
var classNode = root.FindNode(diagnostic.Location.SourceSpan) as ClassDeclarationSyntax;
46+
if (classNode is null)
47+
{
48+
return;
49+
}
4350

4451
context.CancellationToken.ThrowIfCancellationRequested();
4552

@@ -51,14 +58,16 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5158
if (context.Document.SupportsSemanticModel)
5259
{
5360
var model = await context.Document.GetSemanticModelAsync(context.CancellationToken).ConfigureAwait(false);
54-
AddCodeFixWithUpdatingNonPublicConstructor(
55-
context, root, diagnostic, classNode, model);
61+
if (model is null)
62+
{
63+
return;
64+
}
65+
AddCodeFixWithUpdatingNonPublicConstructor(context, root, diagnostic, classNode, model);
5666
}
5767
}
5868
else
5969
{
60-
AddCodeFixWithNewPublicConstructor(
61-
context, root, diagnostic, classNode);
70+
AddCodeFixWithNewPublicConstructor(context, root, diagnostic, classNode);
6271
}
6372
}
6473

Source/Csla.Analyzers/Csla.Analyzers/Csla.Analyzers.csproj

+6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,18 @@
99
<BaseOutputPath>..\..\..\Bin</BaseOutputPath>
1010
<EnforceExtendedAnalyzerRules>true</EnforceExtendedAnalyzerRules>
1111
<IsRoslynComponent>true</IsRoslynComponent>
12+
<Nullable>enable</Nullable>
13+
<WarningsAsErrors>nullable</WarningsAsErrors>
1214
</PropertyGroup>
1315

1416
<ItemGroup>
1517
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" PrivateAssets="all" />
1618
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.10.0" PrivateAssets="all" />
1719
<PackageReference Update="NETStandard.Library" PrivateAssets="all" />
20+
<PackageReference Include="Polyfill" Version="7.4.0">
21+
<PrivateAssets>all</PrivateAssets>
22+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
23+
</PackageReference>
1824
</ItemGroup>
1925

2026
<ItemGroup>

Source/Csla.Analyzers/Csla.Analyzers/DoesChildOperationHaveRunLocalAnalyzer.cs

+5
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
4242
var methodNode = (MethodDeclarationSyntax)context.Node;
4343

4444
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
45+
if (methodSymbol is null)
46+
{
47+
return;
48+
}
49+
4550
var typeSymbol = methodSymbol.ContainingType;
4651

4752
if (typeSymbol.IsStereotype() && methodSymbol.IsChildDataPortalOperation() &&

Source/Csla.Analyzers/Csla.Analyzers/DoesChildOperationHaveRunLocalRemoveAttributeCodeFix.cs

+22-2
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,25 @@ public sealed class DoesChildOperationHaveRunLocalRemoveAttributeCodeFix
3333
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3434
{
3535
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
36+
if (root is null)
37+
{
38+
return;
39+
}
3640

3741
context.CancellationToken.ThrowIfCancellationRequested();
3842

3943
var diagnostic = context.Diagnostics.First();
4044
var methodNode = root.FindNode(diagnostic.Location.SourceSpan) as MethodDeclarationSyntax;
45+
if (methodNode is null)
46+
{
47+
return;
48+
}
4149

4250
context.CancellationToken.ThrowIfCancellationRequested();
4351
await AddCodeFixAsync(context, root, diagnostic, methodNode);
4452
}
4553

46-
private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root,
47-
Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
54+
private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode root, Diagnostic diagnostic, MethodDeclarationSyntax methodNode)
4855
{
4956
var newRoot = root;
5057

@@ -55,7 +62,16 @@ private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode roo
5562
{
5663
foreach (var attribute in methodSymbol.GetAttributes().Where(_ => _.AttributeClass.IsRunLocalAttribute()))
5764
{
65+
if (attribute.ApplicationSyntaxReference is null)
66+
{
67+
continue;
68+
}
69+
5870
newRoot = newRoot.RemoveNode(await attribute.ApplicationSyntaxReference.GetSyntaxAsync(), SyntaxRemoveOptions.KeepNoTrivia);
71+
if (newRoot is null)
72+
{
73+
return;
74+
}
5975
}
6076
}
6177

@@ -72,6 +88,10 @@ private static async Task AddCodeFixAsync(CodeFixContext context, SyntaxNode roo
7288
foreach(var attributeListToRemove in attributeListsToRemove)
7389
{
7490
newRoot = newRoot.RemoveNode(attributeListToRemove, SyntaxRemoveOptions.KeepEndOfLine);
91+
if (newRoot is null)
92+
{
93+
return;
94+
}
7595
}
7696

7797
context.RegisterCodeFix(

Source/Csla.Analyzers/Csla.Analyzers/DoesOperationHaveAttributeAddAttributeCodeFix.cs

+8
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,19 @@ public sealed class DoesOperationHaveAttributeAddAttributeCodeFix
5050
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
5151
{
5252
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
53+
if (root is null)
54+
{
55+
return;
56+
}
5357

5458
context.CancellationToken.ThrowIfCancellationRequested();
5559

5660
var diagnostic = context.Diagnostics.First();
5761
var methodNode = root.FindNode(diagnostic.Location.SourceSpan) as MethodDeclarationSyntax;
62+
if (methodNode is null)
63+
{
64+
return;
65+
}
5866

5967
context.CancellationToken.ThrowIfCancellationRequested();
6068

Source/Csla.Analyzers/Csla.Analyzers/DoesOperationHaveAttributeAnalyzer.cs

+4
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
4242
var methodNode = (MethodDeclarationSyntax)context.Node;
4343

4444
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
45+
if (methodSymbol is null)
46+
{
47+
return;
48+
}
4549
var typeSymbol = methodSymbol.ContainingType;
4650

4751
if (typeSymbol.IsStereotype())

Source/Csla.Analyzers/Csla.Analyzers/EvaluateManagedBackingFieldsAnalyzer.cs

+5-10
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,8 @@ private static void AnalyzeFieldDeclaration(SyntaxNodeAnalysisContext context)
5858
{
5959
foreach (var classMember in classSymbol.GetMembers())
6060
{
61-
if (classMember.Kind == SymbolKind.Property)
61+
if (classMember.Kind == SymbolKind.Property && classMember is IPropertySymbol classProperty)
6262
{
63-
var classProperty = classMember as IPropertySymbol;
64-
6563
if (!classProperty.IsIndexer)
6664
{
6765
if (DetermineIfPropertyUsesField(context, fieldSymbol, classProperty))
@@ -93,9 +91,7 @@ private static void CheckForDiagnostics(SyntaxNodeAnalysisContext context, Field
9391
}
9492
}
9593

96-
private static bool DetermineIfPropertyUsesField(SyntaxNodeAnalysisContext context,
97-
IFieldSymbol fieldSymbol, IPropertySymbol classProperty,
98-
Func<PropertyDeclarationSyntax, SyntaxNode> propertyBody)
94+
private static bool DetermineIfPropertyUsesField(SyntaxNodeAnalysisContext context, IFieldSymbol fieldSymbol, IPropertySymbol classProperty, Func<PropertyDeclarationSyntax, SyntaxNode?> propertyBody)
9995
{
10096
var root = context.Node.SyntaxTree.GetRoot();
10197
var rootSpan = root.FullSpan;
@@ -117,23 +113,22 @@ private static bool DetermineIfPropertyUsesField(SyntaxNodeAnalysisContext conte
117113
return false;
118114
}
119115

120-
private static bool DetermineIfPropertyUsesField(SyntaxNodeAnalysisContext context,
121-
IFieldSymbol fieldSymbol, IPropertySymbol classProperty)
116+
private static bool DetermineIfPropertyUsesField(SyntaxNodeAnalysisContext context, IFieldSymbol fieldSymbol, IPropertySymbol classProperty)
122117
{
123118
if (classProperty.GetMethod != null)
124119
{
125120
return DetermineIfPropertyUsesField(
126121
context, fieldSymbol, classProperty,
127122
propertyNode => propertyNode.ExpressionBody as SyntaxNode ??
128-
propertyNode.AccessorList.Accessors.Single(
123+
propertyNode.AccessorList?.Accessors.Single(
129124
_ => _.IsKind(SyntaxKind.GetAccessorDeclaration)));
130125
}
131126

132127
if (classProperty.SetMethod != null)
133128
{
134129
return DetermineIfPropertyUsesField(
135130
context, fieldSymbol, classProperty,
136-
propertyNode => propertyNode.AccessorList.Accessors.Single(
131+
propertyNode => propertyNode.AccessorList?.Accessors.Single(
137132
_ => _.IsKind(SyntaxKind.SetAccessorDeclaration)));
138133
}
139134

Source/Csla.Analyzers/Csla.Analyzers/EvaluateManagedBackingFieldsCodeFix.cs

+4
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ public sealed class EvaluateManagedBackingFieldsCodeFix
3131
public override async Task RegisterCodeFixesAsync(CodeFixContext context)
3232
{
3333
var root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
34+
if (root is null)
35+
{
36+
return;
37+
}
3438

3539
context.CancellationToken.ThrowIfCancellationRequested();
3640

Source/Csla.Analyzers/Csla.Analyzers/EvaluateManagedBackingFieldsWalker.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Csla.Analyzers
88
internal sealed class EvaluateManagedBackingFieldsWalker
99
: CSharpSyntaxWalker
1010
{
11-
internal EvaluateManagedBackingFieldsWalker(SyntaxNode node, SemanticModel model, IFieldSymbol fieldSymbol)
11+
internal EvaluateManagedBackingFieldsWalker(SyntaxNode? node, SemanticModel model, IFieldSymbol fieldSymbol)
1212
{
1313
(FieldSymbol, Model) = (fieldSymbol, model);
1414
Visit(node);
@@ -17,6 +17,10 @@ internal EvaluateManagedBackingFieldsWalker(SyntaxNode node, SemanticModel model
1717
public override void VisitInvocationExpression(InvocationExpressionSyntax node)
1818
{
1919
var invocationSymbol = Model.GetSymbolInfo(node).Symbol as IMethodSymbol;
20+
if (invocationSymbol is null)
21+
{
22+
return;
23+
}
2024

2125
if (invocationSymbol.IsPropertyInfoManagementMethod())
2226
{

Source/Csla.Analyzers/Csla.Analyzers/EvaluateOperationAttributeUsageAnalyzer.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,25 @@ public override void Initialize(AnalysisContext context)
4040
private static void AnalyzerAttributeDeclaration(SyntaxNodeAnalysisContext context)
4141
{
4242
var attributeNode = (AttributeSyntax)context.Node;
43-
var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeNode).Symbol.ContainingSymbol as ITypeSymbol;
43+
var attributeSymbol = context.SemanticModel.GetSymbolInfo(attributeNode).Symbol?.ContainingSymbol as ITypeSymbol;
4444

4545
if (attributeSymbol.IsDataPortalOperationAttribute())
4646
{
4747
var methodNode = attributeNode.FindParent<MethodDeclarationSyntax>();
48+
if (methodNode is null)
49+
{
50+
return;
51+
}
4852
var methodSymbol = context.SemanticModel.GetDeclaredSymbol(methodNode);
53+
if (methodSymbol is null)
54+
{
55+
return;
56+
}
4957
var typeSymbol = methodSymbol.ContainingType;
5058

5159
if (!typeSymbol.IsStereotype() || methodSymbol.IsStatic)
5260
{
53-
context.ReportDiagnostic(Diagnostic.Create(
54-
operationUsageRule, attributeNode.GetLocation()));
61+
context.ReportDiagnostic(Diagnostic.Create(operationUsageRule, attributeNode.GetLocation()));
5562
}
5663
}
5764
}

0 commit comments

Comments
 (0)