PinguApps.CodeStyle.Analyzers
1.3.0
dotnet add package PinguApps.CodeStyle.Analyzers --version 1.3.0
NuGet\Install-Package PinguApps.CodeStyle.Analyzers -Version 1.3.0
<PackageReference Include="PinguApps.CodeStyle.Analyzers" Version="1.3.0" />
<PackageVersion Include="PinguApps.CodeStyle.Analyzers" Version="1.3.0" />
<PackageReference Include="PinguApps.CodeStyle.Analyzers" />
paket add PinguApps.CodeStyle.Analyzers --version 1.3.0
#r "nuget: PinguApps.CodeStyle.Analyzers, 1.3.0"
#:package PinguApps.CodeStyle.Analyzers@1.3.0
#addin nuget:?package=PinguApps.CodeStyle.Analyzers&version=1.3.0
#tool nuget:?package=PinguApps.CodeStyle.Analyzers&version=1.3.0
PinguApps.CodeStyle.Analyzers
PinguApps.CodeStyle.Analyzers is a small Roslyn analyzer package for PinguApps code style rules that need build-time enforcement.
Installation
Install the package into any project that should enforce the rules:
dotnet add package PinguApps.CodeStyle.Analyzers
NuGet automatically exposes analyzers from the package to the consuming project through PackageReference.
Rules
PINGU0001: classes must not use primary constructors.PINGU0002: conditional expressions must not contain other conditional expressions.PINGU0003: files must contain at most one file-level type; nested types are allowed.PINGU0004: Blazor scoped JavaScript files (*.razor.js) must not be used.PINGU0005: Razor components must use constructor injection in their code-behind.
PINGU0001 Class Primary Constructors
PINGU0001 reports C# classes declared with primary constructors. Use an explicit constructor in classes instead. Records may continue to use primary constructors.
Invalid:
public class Widget(string name)
{
public string Name { get; } = name;
}
Valid:
public class Widget
{
public Widget(string name)
{
Name = name;
}
public string Name { get; }
}
Also valid:
public record Widget(string Name);
PINGU0002 Chained Conditional Expressions
PINGU0002 reports conditional expressions that contain another conditional expression. Use a statement or otherwise simplify the expression.
Invalid:
string value = first ? "one" : second ? "two" : "three";
Valid:
string value = condition ? "one" : "two";
PINGU0003 One Type Per File
PINGU0003 reports each file-level type after the first. Classes, structs, records, enums, interfaces, and delegates all count. Nested types are allowed.
Invalid:
public class Foo
{
}
public record Bar();
Valid:
public class Foo
{
private class Bar
{
}
}
PINGU0004 No Scoped JavaScript
PINGU0004 reports JavaScript files named with Blazor's *.razor.js convention. Keep JavaScript alongside its component if useful, but use a different filename and bundle it as IIFE or ESM instead.
Invalid:
Components/Counter.razor.js
Valid:
Components/Counter.js
PINGU0005 Razor Component Injection
PINGU0005 reports @inject directives and [Inject] attributes in .razor files, and [Inject] attributes in .razor.cs files. Use constructor injection in the code-behind instead.
Invalid:
@inject IWidgetService WidgetService
public partial class Widget
{
[Inject]
public IWidgetService WidgetService { get; set; } = default!;
}
Valid:
public partial class Widget
{
private readonly IWidgetService _widgetService;
public Widget(IWidgetService widgetService)
{
_widgetService = widgetService;
}
}
Learn more about Target Frameworks and .NET Standard.
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.