Skip to content

Commit 399b014

Browse files
committed
fix: address StyleCop code quality issues
1 parent dceed97 commit 399b014

File tree

8 files changed

+132
-64
lines changed

8 files changed

+132
-64
lines changed

.editorconfig

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,58 @@
11
root = true
22

3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
11+
[*.{json,yml,yaml}]
12+
indent_size = 2
13+
14+
[*.md]
15+
trim_trailing_whitespace = false
16+
317
[*.cs]
4-
# Disable specific rules
5-
dotnet_diagnostic.SA1633.severity = none # File header
6-
dotnet_diagnostic.SA1200.severity = none # Using directive placement
7-
dotnet_diagnostic.SA1101.severity = none # Prefix local calls with this
8-
9-
# Enable all other StyleCop rules
10-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.DocumentationRules.severity = warning
11-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.LayoutRules.severity = warning
12-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.MaintainabilityRules.severity = warning
13-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.NamingRules.severity = warning
14-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.OrderingRules.severity = warning
15-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.ReadabilityRules.severity = warning
16-
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.SpacingRules.severity = warning
18+
# SA1309: Field names should not begin with underscore
19+
dotnet_diagnostic.SA1309.severity = none
20+
21+
# SA1413: Use trailing comma in multi-line initializers
22+
dotnet_diagnostic.SA1413.severity = warning
23+
24+
# SA1000: Keywords should be spaced correctly
25+
dotnet_diagnostic.SA1000.severity = warning
26+
27+
# SA1513: Closing brace should be followed by blank line
28+
dotnet_diagnostic.SA1513.severity = warning
29+
30+
# SA1210: Using directives should be ordered alphabetically by namespace
31+
dotnet_diagnostic.SA1210.severity = warning
32+
33+
# SA1400: Access modifier must be declared
34+
dotnet_diagnostic.SA1400.severity = warning
35+
36+
# SA1201: Elements should appear in the correct order
37+
dotnet_diagnostic.SA1201.severity = warning
38+
39+
# SA1202: Elements should be ordered by access
40+
dotnet_diagnostic.SA1202.severity = warning
41+
42+
# SA1204: Static elements should appear before instance elements
43+
dotnet_diagnostic.SA1204.severity = warning
44+
45+
# SA1128: Put constructor initializers on their own line
46+
dotnet_diagnostic.SA1128.severity = warning
47+
48+
# SA1117: Parameters should be on same line or each on own line
49+
dotnet_diagnostic.SA1117.severity = warning
50+
51+
# SA1503: Braces should not be omitted
52+
dotnet_diagnostic.SA1503.severity = warning
53+
54+
# SA1122: Use string.Empty for empty strings
55+
dotnet_diagnostic.SA1122.severity = warning
56+
57+
# SA1116: Split parameters should start on line after declaration
58+
dotnet_diagnostic.SA1116.severity = warning

src/DotNetApiDiff/AssemblyLoading/AssemblyLoader.cs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ namespace DotNetApiDiff.AssemblyLoading;
1111
/// </summary>
1212
public class AssemblyLoader : IAssemblyLoader, IDisposable
1313
{
14-
private readonly ILogger<AssemblyLoader> _logger;
15-
private readonly Dictionary<string, Assembly> _loadedAssemblies = new();
16-
private readonly Dictionary<string, IsolatedAssemblyLoadContext> _loadContexts = new();
14+
private readonly ILogger<AssemblyLoader> logger;
15+
private readonly Dictionary<string, Assembly> loadedAssemblies = new Dictionary<string, Assembly>();
16+
private readonly Dictionary<string, IsolatedAssemblyLoadContext> loadContexts = new Dictionary<string, IsolatedAssemblyLoadContext>();
1717

1818
/// <summary>
1919
/// Creates a new assembly loader with the specified logger
2020
/// </summary>
2121
/// <param name="logger">Logger for diagnostic information</param>
2222
public AssemblyLoader(ILogger<AssemblyLoader> logger)
2323
{
24-
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
24+
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
2525
}
2626

2727
/// <summary>
@@ -39,33 +39,33 @@ public Assembly LoadAssembly(string assemblyPath)
3939
{
4040
if (string.IsNullOrWhiteSpace(assemblyPath))
4141
{
42-
_logger.LogError("Assembly path cannot be null or empty");
42+
this.logger.LogError("Assembly path cannot be null or empty");
4343
throw new ArgumentException("Assembly path cannot be null or empty", nameof(assemblyPath));
4444
}
4545

4646
// Normalize the path to ensure consistent dictionary keys
4747
assemblyPath = Path.GetFullPath(assemblyPath);
4848

4949
// Check if we've already loaded this assembly
50-
if (_loadedAssemblies.TryGetValue(assemblyPath, out var loadedAssembly))
50+
if (this.loadedAssemblies.TryGetValue(assemblyPath, out var loadedAssembly))
5151
{
52-
_logger.LogDebug("Returning previously loaded assembly from {Path}", assemblyPath);
52+
this.logger.LogDebug("Returning previously loaded assembly from {Path}", assemblyPath);
5353
return loadedAssembly;
5454
}
5555

56-
_logger.LogInformation("Loading assembly from {Path}", assemblyPath);
56+
this.logger.LogInformation("Loading assembly from {Path}", assemblyPath);
5757

5858
try
5959
{
6060
// Verify the file exists
6161
if (!File.Exists(assemblyPath))
6262
{
63-
_logger.LogError("Assembly file not found: {Path}", assemblyPath);
63+
this.logger.LogError("Assembly file not found: {Path}", assemblyPath);
6464
throw new FileNotFoundException($"Assembly file not found: {assemblyPath}", assemblyPath);
6565
}
6666

6767
// Create a new isolated load context for this assembly
68-
var loadContext = new IsolatedAssemblyLoadContext(assemblyPath, _logger);
68+
var loadContext = new IsolatedAssemblyLoadContext(assemblyPath, this.logger);
6969

7070
// Add the directory of the assembly as a search path
7171
var assemblyDirectory = Path.GetDirectoryName(assemblyPath);
@@ -78,37 +78,39 @@ public Assembly LoadAssembly(string assemblyPath)
7878
var assembly = loadContext.LoadFromAssemblyPath(assemblyPath);
7979

8080
// Store the assembly and load context for later use
81-
_loadedAssemblies[assemblyPath] = assembly;
82-
_loadContexts[assemblyPath] = loadContext;
81+
this.loadedAssemblies[assemblyPath] = assembly;
82+
this.loadContexts[assemblyPath] = loadContext;
8383

84-
_logger.LogInformation("Successfully loaded assembly: {AssemblyName} from {Path}",
85-
assembly.GetName().Name, assemblyPath);
84+
this.logger.LogInformation(
85+
"Successfully loaded assembly: {AssemblyName} from {Path}",
86+
assembly.GetName().Name,
87+
assemblyPath);
8688

8789
return assembly;
8890
}
8991
catch (FileNotFoundException ex)
9092
{
91-
_logger.LogError(ex, "Assembly file not found: {Path}", assemblyPath);
93+
this.logger.LogError(ex, "Assembly file not found: {Path}", assemblyPath);
9294
throw;
9395
}
9496
catch (BadImageFormatException ex)
9597
{
96-
_logger.LogError(ex, "Invalid assembly format: {Path}", assemblyPath);
98+
this.logger.LogError(ex, "Invalid assembly format: {Path}", assemblyPath);
9799
throw;
98100
}
99101
catch (SecurityException ex)
100102
{
101-
_logger.LogError(ex, "Security exception loading assembly: {Path}", assemblyPath);
103+
this.logger.LogError(ex, "Security exception loading assembly: {Path}", assemblyPath);
102104
throw;
103105
}
104106
catch (PathTooLongException ex)
105107
{
106-
_logger.LogError(ex, "Path too long for assembly: {Path}", assemblyPath);
108+
this.logger.LogError(ex, "Path too long for assembly: {Path}", assemblyPath);
107109
throw;
108110
}
109111
catch (ReflectionTypeLoadException ex)
110112
{
111-
_logger.LogError(ex, "Failed to load types from assembly: {Path}", assemblyPath);
113+
this.logger.LogError(ex, "Failed to load types from assembly: {Path}", assemblyPath);
112114

113115
// Log the loader exceptions for more detailed diagnostics
114116
if (ex.LoaderExceptions != null)
@@ -117,7 +119,7 @@ public Assembly LoadAssembly(string assemblyPath)
117119
{
118120
if (loaderEx != null)
119121
{
120-
_logger.LogError(loaderEx, "Loader exception: {Message}", loaderEx.Message);
122+
this.logger.LogError(loaderEx, "Loader exception: {Message}", loaderEx.Message);
121123
}
122124
}
123125
}
@@ -126,7 +128,7 @@ public Assembly LoadAssembly(string assemblyPath)
126128
}
127129
catch (Exception ex)
128130
{
129-
_logger.LogError(ex, "Unexpected error loading assembly: {Path}", assemblyPath);
131+
this.logger.LogError(ex, "Unexpected error loading assembly: {Path}", assemblyPath);
130132
throw;
131133
}
132134
}
@@ -140,7 +142,7 @@ public bool IsValidAssembly(string assemblyPath)
140142
{
141143
if (string.IsNullOrWhiteSpace(assemblyPath))
142144
{
143-
_logger.LogDebug("Assembly path is null or empty");
145+
this.logger.LogDebug("Assembly path is null or empty");
144146
return false;
145147
}
146148

@@ -149,7 +151,7 @@ public bool IsValidAssembly(string assemblyPath)
149151
// Check if the file exists
150152
if (!File.Exists(assemblyPath))
151153
{
152-
_logger.LogDebug("Assembly file does not exist: {Path}", assemblyPath);
154+
this.logger.LogDebug("Assembly file does not exist: {Path}", assemblyPath);
153155
return false;
154156
}
155157

@@ -160,7 +162,7 @@ public bool IsValidAssembly(string assemblyPath)
160162
var assembly = tempContext.LoadFromAssemblyPath(assemblyPath);
161163

162164
// If we got here, the assembly is valid
163-
_logger.LogDebug("Successfully validated assembly: {Path}", assemblyPath);
165+
this.logger.LogDebug("Successfully validated assembly: {Path}", assemblyPath);
164166
return true;
165167
}
166168
finally
@@ -171,7 +173,7 @@ public bool IsValidAssembly(string assemblyPath)
171173
}
172174
catch (Exception ex)
173175
{
174-
_logger.LogDebug(ex, "Assembly validation failed for {Path}: {Message}", assemblyPath, ex.Message);
176+
this.logger.LogDebug(ex, "Assembly validation failed for {Path}: {Message}", assemblyPath, ex.Message);
175177
return false;
176178
}
177179
}
@@ -181,30 +183,30 @@ public bool IsValidAssembly(string assemblyPath)
181183
/// </summary>
182184
public void UnloadAll()
183185
{
184-
_logger.LogInformation("Unloading all assemblies");
186+
this.logger.LogInformation("Unloading all assemblies");
185187

186-
foreach (var context in _loadContexts.Values)
188+
foreach (var context in this.loadContexts.Values)
187189
{
188190
try
189191
{
190192
context.Unload();
191193
}
192194
catch (Exception ex)
193195
{
194-
_logger.LogWarning(ex, "Error unloading assembly context");
196+
this.logger.LogWarning(ex, "Error unloading assembly context");
195197
}
196198
}
197199

198-
_loadContexts.Clear();
199-
_loadedAssemblies.Clear();
200+
this.loadContexts.Clear();
201+
this.loadedAssemblies.Clear();
200202
}
201203

202204
/// <summary>
203205
/// Disposes the assembly loader and unloads all assemblies
204206
/// </summary>
205207
public void Dispose()
206208
{
207-
_logger.LogDebug("Disposing AssemblyLoader");
209+
this.logger.LogDebug("Disposing AssemblyLoader");
208210
UnloadAll();
209211
GC.SuppressFinalize(this);
210212
}

src/DotNetApiDiff/Models/ApiChange.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class ApiChange
3636
/// <summary>
3737
/// Additional details about the change
3838
/// </summary>
39-
public List<string> Details { get; set; } = new();
39+
public List<string> Details { get; set; } = new List<string>();
4040

4141
/// <summary>
4242
/// Validates the ApiChange instance
@@ -46,24 +46,34 @@ public bool IsValid()
4646
{
4747
// Must have a description
4848
if (string.IsNullOrWhiteSpace(Description))
49+
{
4950
return false;
51+
}
5052

5153
// Must have at least one member for most change types
5254
if (Type != ChangeType.Added && Type != ChangeType.Removed &&
5355
SourceMember == null && TargetMember == null)
56+
{
5457
return false;
58+
}
5559

5660
// Added changes should have target member
5761
if (Type == ChangeType.Added && TargetMember == null)
62+
{
5863
return false;
64+
}
5965

6066
// Removed changes should have source member
6167
if (Type == ChangeType.Removed && SourceMember == null)
68+
{
6269
return false;
70+
}
6371

6472
// Modified changes should have both members
6573
if (Type == ChangeType.Modified && (SourceMember == null || TargetMember == null))
74+
{
6675
return false;
76+
}
6777

6878
return true;
6979
}
@@ -73,7 +83,7 @@ public bool IsValid()
7383
/// </summary>
7484
public string GetMemberName()
7585
{
76-
return TargetMember?.FullName ?? SourceMember?.FullName ?? "Unknown";
86+
return TargetMember?.FullName ?? SourceMember?.FullName ?? string.Empty;
7787
}
7888

7989
/// <summary>

src/DotNetApiDiff/Models/ApiComparison.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,27 @@ public class ApiComparison
88
/// <summary>
99
/// List of API members that were added
1010
/// </summary>
11-
public List<ApiChange> Additions { get; set; } = new();
11+
public List<ApiChange> Additions { get; set; } = new List<ApiChange>();
1212

1313
/// <summary>
1414
/// List of API members that were removed
1515
/// </summary>
16-
public List<ApiChange> Removals { get; set; } = new();
16+
public List<ApiChange> Removals { get; set; } = new List<ApiChange>();
1717

1818
/// <summary>
1919
/// List of API members that were modified
2020
/// </summary>
21-
public List<ApiChange> Modifications { get; set; } = new();
21+
public List<ApiChange> Modifications { get; set; } = new List<ApiChange>();
2222

2323
/// <summary>
2424
/// List of API members that were intentionally excluded
2525
/// </summary>
26-
public List<ApiChange> Excluded { get; set; } = new();
26+
public List<ApiChange> Excluded { get; set; } = new List<ApiChange>();
2727

2828
/// <summary>
2929
/// Summary statistics of the comparison
3030
/// </summary>
31-
public ComparisonSummary Summary { get; set; } = new();
31+
public ComparisonSummary Summary { get; set; } = new ComparisonSummary();
3232

3333
/// <summary>
3434
/// Gets all changes as a single collection
@@ -65,23 +65,33 @@ public bool IsValid()
6565
// Validate all changes
6666
var allChanges = AllChanges.ToList();
6767
if (allChanges.Any(c => !c.IsValid()))
68+
{
6869
return false;
70+
}
6971

7072
// Validate that additions only contain Added changes
7173
if (Additions.Any(c => c.Type != ChangeType.Added))
74+
{
7275
return false;
76+
}
7377

7478
// Validate that removals only contain Removed changes
7579
if (Removals.Any(c => c.Type != ChangeType.Removed))
80+
{
7681
return false;
82+
}
7783

7884
// Validate that modifications only contain Modified changes
7985
if (Modifications.Any(c => c.Type != ChangeType.Modified))
86+
{
8087
return false;
88+
}
8189

8290
// Validate that excluded only contain Excluded changes
8391
if (Excluded.Any(c => c.Type != ChangeType.Excluded))
92+
{
8493
return false;
94+
}
8595

8696
return true;
8797
}

0 commit comments

Comments
 (0)