Skip to content

Commit

Permalink
Update NimbleBlazor NI.CSharp.Analyzers to v2.0.21 (with code fixes) (#…
Browse files Browse the repository at this point in the history
…1867)

# Pull Request

## 🤨 Rationale

- Update NimbleBlazor NI.CSharp.Analyzers to v2.0.21 (as Renovate
started in [!1696](#1696) )
- Add an `.editorconfig` that allows us to further customize
rules/standards ([Microsoft
docs](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options)).
Most of the rules I have in this one were added based on the specific
failed rules/ errors triggered for the Nimble Blazor code

## 👩‍💻 Implementation

Most of the file diffs are from standardizing on file-scoped namespace
declarations (as previously discussed with Meyer).

I opted to disable these rules for the same of this PR, to keep the
number of changes to review smaller:
- Rules requiring pattern matching
- Rules for [expression-bodied
members](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members)
- Rules covering ['var'
preferences](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/ide0007-ide0008)

There were some additional spurious errors/warnings being triggered for
our .razor.cs files, for which I included comments in the editorconfig.

For reference, [the .editorconfig from the ASW repo
root](https://dev.azure.com/ni/DevCentral/_git/ASW?path=%2F.editorconfig&_a=contents&version=GBmain)

## 🧪 Testing

PR build, glanced over Blazor example app in Storybook.  
Release build in Visual Studio

## ✅ Checklist

<!--- Review the list and put an x in the boxes that apply or ~~strike
through~~ around items that don't (along with an explanation). -->

- [x] I have updated the project documentation to reflect my changes or
determined no changes are needed.

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
  • Loading branch information
msmithNI and renovate[bot] authored Feb 26, 2024
1 parent 1739946 commit c60a533
Show file tree
Hide file tree
Showing 40 changed files with 763 additions and 727 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Uptake new code analyzer rules, minor code cleanup",
"packageName": "@ni/nimble-blazor",
"email": "20709258+msmithNI@users.noreply.github.com",
"dependentChangeType": "patch"
}
56 changes: 56 additions & 0 deletions packages/nimble-blazor/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
[*.cs]

csharp_style_namespace_declarations = file_scoped:warning
csharp_prefer_simple_using_statement = false
dotnet_style_prefer_conditional_expression_over_return = false

# Disable: Remove unnecessary expression value (_/discard not required)
dotnet_diagnostic.IDE0058.severity = none

# Rules for the use of 'var':
dotnet_diagnostic.IDE0007.severity = none
dotnet_diagnostic.IDE0008.severity = none
# Note: ASW generally uses the settings below, may opt in in the future.
# csharp_style_var_for_built_in_types = false
# csharp_style_var_when_type_is_apparent = true
# csharp_style_var_elsewhere = false

# Expression-bodied members
# Note: May switch to when_on_single_line for methods/properties/accessors
# in the future
# csharp_style_expression_bodied_methods
dotnet_diagnostic.IDE0022.severity = none
# csharp_style_expression_bodied_properties
dotnet_diagnostic.IDE0025.severity = none
# csharp_style_expression_bodied_indexers
dotnet_diagnostic.IDE0026.severity = none
# csharp_style_expression_bodied_accessors
dotnet_diagnostic.IDE0027.severity = none

# Use 'switch' expression
dotnet_diagnostic.IDE0066.severity = none

# Pattern matching
# Disable general pattern matching rule for now, may opt in in the future
dotnet_diagnostic.IDE0078.severity = none

[*.razor.cs]
# Spurious 'unused' warnings when properties/methods are only used from Razor files
dotnet_diagnostic.IDE0051.severity = none
dotnet_diagnostic.IDE0052.severity = none
# 'Make field readonly' incorrectly triggered for component _refs
dotnet_diagnostic.IDE0044.severity = none

[Program.cs]
# Spurious 'unnecessary using' warnings
dotnet_diagnostic.IDE0005.severity = none

[Tests/NimbleBlazor.Tests.Acceptance/Tests/*.cs]
dotnet_style_namespace_match_folder = false
# Disable 'Namespace does not match folder structure'
dotnet_diagnostic.IDE0130.severity = none

[Examples/Demo.Shared/Shared/*.cs]
dotnet_style_namespace_match_folder = false
# Disable 'Namespace does not match folder structure'
dotnet_diagnostic.IDE0130.severity = none
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<ItemGroup>
<AdditionalFiles Include="..\..\CodeAnalysisDictionary.xml" Link="CodeAnalysisDictionary.xml" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.4" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.21" />
</ItemGroup>

<ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion packages/nimble-blazor/Examples/Demo.Client/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#pragma warning disable CA1812
using Demo.Shared;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
Expand Down
17 changes: 8 additions & 9 deletions packages/nimble-blazor/Examples/Demo.Hybrid/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Demo.Hybrid
{
using System.Windows;
using System.Windows;

namespace Demo.Hybrid;

/// <summary>
/// Interaction logic for App.xaml.
/// </summary>
public partial class App : Application
{
}
/// <summary>
/// Interaction logic for App.xaml.
/// </summary>
public partial class App : Application
{
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<ItemGroup>
<AdditionalFiles Include="..\..\CodeAnalysisDictionary.xml" Link="CodeAnalysisDictionary.xml" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.4" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.21" />
</ItemGroup>

<ItemGroup>
Expand Down
55 changes: 26 additions & 29 deletions packages/nimble-blazor/Examples/Demo.Hybrid/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,41 +1,38 @@
namespace Demo.Hybrid
{
using System.Windows;
using Microsoft.Extensions.DependencyInjection;
using System.Windows;
using Microsoft.Extensions.DependencyInjection;

namespace Demo.Hybrid;

/// <summary>
/// Interaction logic for MainWindow.xaml.
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// Interaction logic for MainWindow.xaml.
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public partial class MainWindow : Window
public MainWindow()
{
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow"/> class.
/// </summary>
public MainWindow()
{
this.InitializeComponent();
InitializeComponent();
#if RELEASE
this.Loaded += this.MainWindow_Loaded;
Loaded += MainWindow_Loaded;
#endif
var serviceCollection = new ServiceCollection();
serviceCollection.AddWpfBlazorWebView();
serviceCollection.AddBlazorWebViewDeveloperTools();
#pragma warning disable NI1004
this.Resources.Add("services", serviceCollection.BuildServiceProvider());
#pragma warning restore NI1004
}
var serviceCollection = new ServiceCollection();
serviceCollection.AddWpfBlazorWebView();
serviceCollection.AddBlazorWebViewDeveloperTools();
Resources.Add("services", serviceCollection.BuildServiceProvider());
}

#if RELEASE
#pragma warning disable VSTHRD100 // Avoid async void methods
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
#pragma warning restore VSTHRD100 // Avoid async void methods
{
// We recommend for Release versions to turn off browser accelerator keys
// for applications that are meant to operate more like a native desktop app.
await this.blazorWebView.WebView.EnsureCoreWebView2Async().ConfigureAwait(true);
var settings = this.blazorWebView.WebView.CoreWebView2.Settings;
settings.AreBrowserAcceleratorKeysEnabled = false;
}
#endif
{
// We recommend for Release versions to turn off browser accelerator keys
// for applications that are meant to operate more like a native desktop app.
await blazorWebView.WebView.EnsureCoreWebView2Async().ConfigureAwait(true);
var settings = blazorWebView.WebView.CoreWebView2.Settings;
settings.AreBrowserAcceleratorKeysEnabled = false;
}
#endif
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

<ItemGroup>
<AdditionalFiles Include="..\..\CodeAnalysisDictionary.xml" Link="CodeAnalysisDictionary.xml" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.4" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.21" />
</ItemGroup>

<ItemGroup>
Expand Down
30 changes: 13 additions & 17 deletions packages/nimble-blazor/Examples/Demo.Server/Pages/Error.cshtml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,22 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace Demo.Server.Pages
{
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
private readonly ILogger<ErrorModel> _logger;
namespace Demo.Server.Pages;

public string? RequestId { get; set; }
[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
[IgnoreAntiforgeryToken]
public class ErrorModel : PageModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);

public ErrorModel(ILogger<ErrorModel> logger)
{
_logger = logger;
}
public ErrorModel()
{
}

public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
public void OnGet()
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

<ItemGroup>
<AdditionalFiles Include="..\..\CodeAnalysisDictionary.xml" Link="CodeAnalysisDictionary.xml" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.4" />
<PackageReference Include="NI.CSharp.Analyzers" Version="2.0.21" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit c60a533

Please sign in to comment.