Skip to content

Commit e259bb9

Browse files
authored
fix: Update code style rules and improve SARIF logging (#121)
* fix: Update code style rules and improve SARIF logging - Increased max line length to 200 for XML/JSON/config files in .editorconfig - Added naming rule for static readonly fields (PascalCase, error) - Disabled style rules for .received.*, .verified.*, and migration files - Updated .csharpierignore to ignore NuGet config and test output files - Renamed _newLineSeparator to NewLineSeparator in ProjectFactory.cs - Enhanced SARIF result logging: only log on errors/warnings and when logger is enabled, with CA1873 suppression * refactor: Rename variables for improved clarity and consistency
1 parent 8be981e commit e259bb9

8 files changed

Lines changed: 42 additions & 37 deletions

File tree

.csharpierignore

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
**/nuget.config
2-
**/_snapshots/
3-
**/_snapshot/
1+
**/[Nn]u[Gg]et.config
2+
**/*.verified.*
3+
**/*.received.*

.editorconfig

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,22 @@ generated_code = true
4444
# XML project files
4545
[*.{slnx,csproj,vbproj,vcxproj,vcxproj.filters,proj,projitems,shproj,nativeproj,locproj}]
4646
indent_size = 2
47-
max_line_length = 160
47+
max_line_length = 200
4848

4949
# Xml build files
5050
[*.builds]
5151
indent_size = 2
52+
max_line_length = 200
5253

5354
# Xml files
5455
[*.{xml,stylecop,resx,ruleset}]
5556
indent_size = 2
57+
max_line_length = 200
5658

5759
# XML config files
5860
[*.{props,targets,ruleset,config,nuspec,vsixmanifest,vsct}]
5961
indent_size = 2
62+
max_line_length = 200
6063

6164
# JSON files
6265
[*.json]
@@ -87,10 +90,6 @@ insert_final_newline = false
8790
[*.sln]
8891
indent_style = tab
8992

90-
[*.{received,verified}.txt]
91-
insert_final_newline = false
92-
trim_trailing_whitespace = false
93-
9493
[*.{cs,csx,vb,vbx}]
9594
# .NET Code Style Settings
9695
# See https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
@@ -135,6 +134,13 @@ dotnet_naming_rule.all_const.severity = error
135134
dotnet_naming_rule.all_const.style = all_elements
136135
dotnet_naming_rule.all_const.symbols = all_const
137136

137+
dotnet_naming_style.all_static_readonly.capitalization = pascal_case
138+
dotnet_naming_symbols.all_static_readonly.applicable_kinds = field
139+
dotnet_naming_symbols.all_static_readonly.required_modifiers = static, readonly
140+
dotnet_naming_rule.all_static_readonly.severity = error
141+
dotnet_naming_rule.all_static_readonly.style = all_static_readonly
142+
dotnet_naming_rule.all_static_readonly.symbols = all_static_readonly
143+
138144
dotnet_naming_style.all_fields.required_prefix = _
139145
dotnet_naming_style.all_fields.capitalization = camel_case
140146
dotnet_naming_symbols.all_fields.applicable_kinds = field
@@ -267,19 +273,18 @@ dotnet_diagnostic.IDE0290.severity = sugges
267273
# [CSharpier] Incompatible rules deactivated
268274
# https://csharpier.com/docs/IntegratingWithLinters#code-analysis-rules
269275
dotnet_diagnostic.IDE0055.severity = none
270-
dotnet_diagnostic.SA1000.severity = none
271-
dotnet_diagnostic.SA1009.severity = none
272-
dotnet_diagnostic.SA1111.severity = none
273-
dotnet_diagnostic.SA1118.severity = none
274-
dotnet_diagnostic.SA1137.severity = none
275-
dotnet_diagnostic.SA1413.severity = none
276-
dotnet_diagnostic.SA1500.severity = none
277-
dotnet_diagnostic.SA1501.severity = none
278-
dotnet_diagnostic.SA1502.severity = none
279-
dotnet_diagnostic.SA1504.severity = none
280-
dotnet_diagnostic.SA1515.severity = none
281-
dotnet_diagnostic.SA1516.severity = none
282276

283277
# Support for NetEvolve.Arguments Methods
284278
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1062#null-check-validation-methods
285279
dotnet_code_quality.CA1062.null_check_validation_methods = M:NetEvolve.Arguments.Argument.ThrowIfNull(System.Object,System.String)|M:NetEvolve.Arguments.Argument.ThrowIfNull(System.Void*,System.String)|M:NetEvolve.Arguments.Argument.ThrowIfNullOrEmpty(System.String,System.String)|M:NetEvolve.Arguments.Argument.ThrowIfNullOrEmpty``1(System.Collections.Generic.IEnumerable{``0},System.String)|M:NetEvolve.Arguments.Argument.ThrowIfNullOrWhiteSpace(System.String,System.String)
280+
281+
# Disable all style rules for generated code
282+
[*.{received,verified}.*]
283+
generated_code = true
284+
# Disable all style rules for migrations
285+
dotnet_analyzer_diagnostic.severity = none
286+
287+
[**/Migrations/*.{cs,csx,vb,vbx}]
288+
generated_code = true
289+
# Disable all style rules for migrations
290+
dotnet_analyzer_diagnostic.severity = none

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<CentralPackageTransitivePinningEnabled>true</CentralPackageTransitivePinningEnabled>
55
</PropertyGroup>
66
<ItemGroup>
7+
<GlobalPackageReference Include="CSharpier.MSBuild" Version="1.2.6" />
78
<GlobalPackageReference Include="Microsoft.CodeAnalysis.BannedApiAnalyzers" Version="4.14.0" />
89
<GlobalPackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="10.0.201" />
910
<GlobalPackageReference Include="Microsoft.SourceLink.GitHub" Version="10.0.201" />

ProjectBuilders.slnx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
<Folder Name="/tests/">
2929
<Project Path="tests/NetEvolve.ProjectBuilders.Tests.Unit/NetEvolve.ProjectBuilders.Tests.Unit.csproj" />
3030
<Project Path="tests/NetEvolve.ProjectBuilders.TUnit.Tests.Integration/NetEvolve.ProjectBuilders.TUnit.Tests.Integration.csproj" />
31-
<Project
32-
Path="tests/NetEvolve.ProjectBuilders.XUnit.Tests.Integration/NetEvolve.ProjectBuilders.XUnit.Tests.Integration.csproj"
33-
Id="594f97b7-f15b-43c9-9067-996a157539ec"
34-
/>
31+
<Project Path="tests/NetEvolve.ProjectBuilders.XUnit.Tests.Integration/NetEvolve.ProjectBuilders.XUnit.Tests.Integration.csproj" Id="594f97b7-f15b-43c9-9067-996a157539ec" />
3532
</Folder>
3633
</Solution>

src/NetEvolve.ProjectBuilders/Builders/TestPackageBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ internal sealed class TestPackageBuilder : ITestPackageBuilder
3232
private TemporaryDirectoryBuilder? _nugetFolder;
3333
private readonly ISubdirectoryBuilder _directoy;
3434
private bool _isInitialized;
35-
private static readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
35+
private static readonly SemaphoreSlim Lock = new SemaphoreSlim(1, 1);
3636
private readonly HashSet<string> _packagePaths = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
3737

3838
/// <inheritdoc cref="IObjectBuilder.FullPath"/>
@@ -49,7 +49,7 @@ internal sealed class TestPackageBuilder : ITestPackageBuilder
4949
/// <inheritdoc cref="IObjectBuilder.CreateAsync(CancellationToken)"/>
5050
public async ValueTask CreateAsync(CancellationToken cancellationToken = default)
5151
{
52-
await _lock.WaitAsync(cancellationToken).ConfigureAwait(false);
52+
await Lock.WaitAsync(cancellationToken).ConfigureAwait(false);
5353

5454
try
5555
{
@@ -81,7 +81,7 @@ public async ValueTask CreateAsync(CancellationToken cancellationToken = default
8181
}
8282
finally
8383
{
84-
_ = _lock.Release();
84+
_ = Lock.Release();
8585
}
8686
}
8787

src/NetEvolve.ProjectBuilders/Helpers/SharedHttpClient.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
/// </remarks>
3939
internal static class SharedHttpClient
4040
{
41-
private static readonly Lazy<HttpClient> _instance = new(
41+
private static readonly Lazy<HttpClient> InstanceLazy = new(
4242
CreateHttpClient,
4343
LazyThreadSafetyMode.ExecutionAndPublication
4444
);
@@ -53,7 +53,7 @@ internal static class SharedHttpClient
5353
/// </para>
5454
/// </remarks>
5555
/// <value>The shared HttpClient instance configured with retry logic and connection pooling.</value>
56-
public static HttpClient Instance { get; } = _instance.Value;
56+
public static HttpClient Instance { get; } = InstanceLazy.Value;
5757

5858
[SuppressMessage("Reliability", "CA2000:Dispose objects before losing scope", Justification = "As designed.")]
5959
private static HttpClient CreateHttpClient()

src/NetEvolve.ProjectBuilders/Models/TargetFramework.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ private TargetFramework(string name, string value, TargetPlatform? platform)
8484
Platform = platform;
8585
}
8686

87-
private static readonly HashSet<TargetFramework> _values = [];
88-
internal static TargetFramework[] Values => [.. _values];
87+
private static readonly HashSet<TargetFramework> ValuesSet = [];
88+
internal static TargetFramework[] Values => [.. ValuesSet];
8989

9090
/// <summary>
9191
/// Creates a custom target framework instance.
@@ -130,7 +130,7 @@ public static TargetFramework Create(string name, string value, TargetPlatform?
130130

131131
var targetFramework = new TargetFramework(name, value, platform);
132132

133-
if (!_values.Add(targetFramework))
133+
if (!ValuesSet.Add(targetFramework))
134134
{
135135
throw new ArgumentException("TargetFramework already registered.", nameof(name));
136136
}

src/NetEvolve.ProjectBuilders/ProjectFactory.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public sealed partial class ProjectFactory : IProjectFactory
7272
/// <inheritdoc cref="IProjectFactory.DirectoryBuilder" />
7373
public ISubdirectoryBuilder DirectoryBuilder => _tempDirectory;
7474

75-
private static readonly string[] _newLineSeparator = ["\r\n", "\r", "\n"];
75+
private static readonly string[] NewLineSeparator = ["\r\n", "\r", "\n"];
7676

7777
internal ProjectFactory(
7878
ITestPackageBuilder? testPackageBuilder,
@@ -205,9 +205,11 @@ await Parallel
205205

206206
EnrichSarifResults(sarif);
207207

208-
if (!sarif.HasNoErrorsOrWarnings())
208+
if (!sarif.HasNoErrorsOrWarnings() && _logger.IsEnabled(LogLevel.Information))
209209
{
210+
#pragma warning disable CA1873 // Avoid potentially expensive logging
210211
LogSarifResult(string.Join('\n', sarif.Results.Select(x => x.ToString())));
212+
#pragma warning restore CA1873 // Avoid potentially expensive logging
211213
}
212214

213215
return sarif;
@@ -277,9 +279,9 @@ await Cli.Wrap("dotnet")
277279
[GeneratedRegex(RulesFilterPattern, RegexOptions.Compiled)]
278280
public static partial Regex RulesFilter();
279281
#else
280-
public static Regex RulesFilter() => _rulesFilter;
282+
public static Regex RulesFilter() => RulesFilterField;
281283

282-
private static readonly Regex _rulesFilter = new Regex(RulesFilterPattern, RegexOptions.Compiled);
284+
private static readonly Regex RulesFilterField = new Regex(RulesFilterPattern, RegexOptions.Compiled);
283285
#endif
284286

285287
private void EnrichSarifResults(OutputFile? sarif)
@@ -291,7 +293,7 @@ private void EnrichSarifResults(OutputFile? sarif)
291293

292294
var outputLines = _output
293295
.SelectMany(x =>
294-
x.Split(_newLineSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
296+
x.Split(NewLineSeparator, StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries)
295297
)
296298
.ToArray();
297299
var filteredResults = new List<OutputRunResult>();

0 commit comments

Comments
 (0)