Skip to content

Commit

Permalink
feat: Turn on warning as errors for layer libs and update analyzers.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanplevesque committed Oct 3, 2023
1 parent 761ad0b commit 47f6f6f
Show file tree
Hide file tree
Showing 34 changed files with 136 additions and 54 deletions.
80 changes: 70 additions & 10 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
root = true
root = true

#############################
# Core EditorConfig Options #
Expand Down Expand Up @@ -42,7 +42,7 @@ dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = false

# Use C#10 file-scoped namespaces
csharp_style_namespace_declarations=file_scoped:suggestion
csharp_style_namespace_declarations = file_scoped:suggestion

# Avoid the this. keyword
dotnet_style_qualification_for_field = false:suggestion
Expand All @@ -67,6 +67,9 @@ dotnet_style_prefer_auto_properties = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:suggestion
dotnet_style_prefer_conditional_expression_over_return = true:suggestion

# Operator placement
dotnet_style_operator_placement_when_wrapping = beginning_of_line

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:suggestion
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:suggestion
Expand All @@ -87,15 +90,15 @@ dotnet_naming_style.pascal_case_style.capitalization = pascal_case
# Constant fields are PascalCase
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = non_private_static_field_style
dotnet_naming_symbols.constant_fields.applicable_kinds = field
dotnet_naming_symbols.constant_fields.applicable_accessibilities = *
dotnet_naming_symbols.constant_fields.required_modifiers = const

# Non-private readonly fields are PascalCase
dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.symbols = non_private_readonly_fields
dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.style = non_private_readonly_field_style
dotnet_naming_rule.non_private_readonly_fields_should_be_pascal_case.style = non_private_static_field_style
dotnet_naming_symbols.non_private_readonly_fields.applicable_kinds = field
dotnet_naming_symbols.non_private_readonly_fields.applicable_accessibilities = public, protected, internal, protected internal, private protected
dotnet_naming_symbols.non_private_readonly_fields.required_modifiers = readonly
Expand All @@ -113,7 +116,7 @@ dotnet_naming_style.non_private_static_field_style.capitalization = pascal_case
# Private static fields are camelCase
dotnet_naming_rule.static_fields_should_be_camel_case.severity = suggestion
dotnet_naming_rule.static_fields_should_be_camel_case.symbols = static_fields
dotnet_naming_rule.static_fields_should_be_camel_case.style = static_field_style
dotnet_naming_rule.static_fields_should_be_camel_case.style = instance_field_style
dotnet_naming_symbols.static_fields.applicable_kinds = field
dotnet_naming_symbols.static_fields.required_modifiers = static
dotnet_naming_style.static_field_style.capitalization = camel_case
Expand All @@ -137,21 +140,51 @@ dotnet_naming_style.camel_case_style.capitalization = camel_case
# Local functions are PascalCase
dotnet_naming_rule.local_functions_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.local_functions_should_be_pascal_case.symbols = local_functions
dotnet_naming_rule.local_functions_should_be_pascal_case.style = local_function_style
dotnet_naming_rule.local_functions_should_be_pascal_case.style = non_private_static_field_style
dotnet_naming_symbols.local_functions.applicable_kinds = local_function
dotnet_naming_style.local_function_style.capitalization = pascal_case

# By default, name items with PascalCase
dotnet_naming_rule.members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.members_should_be_pascal_case.symbols = all_members
dotnet_naming_rule.members_should_be_pascal_case.style = pascal_case_style
dotnet_naming_rule.members_should_be_pascal_case.style = non_private_static_field_style
dotnet_naming_symbols.all_members.applicable_kinds = *
dotnet_naming_style.pascal_case_style.capitalization = pascal_case

#######################
# C# Code Style Rules #
#######################

csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:suggestion
csharp_style_prefer_method_group_conversion = true:silent
csharp_style_prefer_top_level_statements = true:silent
csharp_style_throw_expression = true:suggestion
csharp_style_prefer_null_check_over_type_check = true:suggestion
csharp_style_prefer_local_over_anonymous_function = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_prefer_utf8_string_literals = true:suggestion
csharp_style_prefer_tuple_swap = true:suggestion
csharp_style_unused_value_assignment_preference = discard_variable:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent
csharp_prefer_static_local_function = true:suggestion
csharp_style_prefer_readonly_struct = true:suggestion
csharp_style_prefer_readonly_struct_member = true:suggestion
csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
csharp_style_allow_blank_lines_between_consecutive_braces_experimental = true:silent
csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent
csharp_style_allow_blank_line_after_token_in_conditional_expression_experimental = true:silent
csharp_style_allow_blank_line_after_token_in_arrow_expression_clause_experimental = true:silent
csharp_style_conditional_delegate_call = true:suggestion
csharp_style_prefer_switch_expression = true:suggestion
csharp_style_prefer_pattern_matching = true:suggestion
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_prefer_not_pattern = true:suggestion
csharp_style_prefer_extended_property_pattern = true:suggestion

# Indentation preferences
csharp_indent_block_contents = true
csharp_indent_braces = false
Expand All @@ -167,11 +200,13 @@ csharp_style_var_elsewhere = true:suggestion

# Prefer property-like constructs to have an expression-body
csharp_style_expression_bodied_properties = true:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_accessors = true:suggestion
csharp_style_expression_bodied_indexers = when_on_single_line:suggestion
csharp_style_expression_bodied_accessors = when_on_single_line:suggestion
csharp_style_expression_bodied_methods = false:none
csharp_style_expression_bodied_constructors = false:none
csharp_style_expression_bodied_operators = false:none
csharp_style_expression_bodied_local_functions = false:silent
csharp_style_expression_bodied_lambdas = when_on_single_line:silent

# Newline preferences
csharp_new_line_before_open_brace = all
Expand Down Expand Up @@ -200,6 +235,17 @@ csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_after_comma = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_around_declaration_statements = false
csharp_space_before_open_square_brackets = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_square_brackets = false
csharp_space_after_dot = false
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_semicolon_in_for_statement = false

# Blocks are allowed
csharp_prefer_braces = true:suggestion
Expand Down Expand Up @@ -266,7 +312,7 @@ dotnet_diagnostic.SA1202.severity = suggestion
# SA1600:Elements should be documented
dotnet_diagnostic.SA1600.severity = none
# SA1601:Elements should be documented
dotnet_diagnostic.SA1601.severity = none
dotnet_diagnostic.SA1601.severity = suggestion
# SA1116:Split parameters should start on line after declaration
dotnet_diagnostic.SA1116.severity = none
# SA1111:Closing parenthesis should be on line of last parameter
Expand Down Expand Up @@ -331,3 +377,17 @@ dotnet_diagnostic.CA1024.severity = none
dotnet_diagnostic.SA1615.severity = suggestion
# CA5394: Do not use insecure randomness
dotnet_diagnostic.CA5394.severity = suggestion
# SA1623: Property summary documentation should match accessors
dotnet_diagnostic.SA1623.severity = suggestion
# SA1502: Element should not be on a single line
dotnet_diagnostic.SA1502.severity = suggestion
# SA1602: Enumeration items should be documented
dotnet_diagnostic.SA1602.severity = suggestion
# CA1032: Implement standard exception constructors
dotnet_diagnostic.CA1032.severity = suggestion
# SA1210: Using directives must be ordered alphabetically by namespace
dotnet_diagnostic.SA1210.severity = suggestion
# CA1308: Normalize strings to uppercase
dotnet_diagnostic.CA1308.severity = none
# CS1587: XML comment is not placed on a valid language element
dotnet_diagnostic.CS1587.severity = none
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)

Prefix your items with `(Template)` if the change is about the template and not the resulting application.

## 2.1.X
- Enable `TreatWarningsAsErrors` for the Access, Business, and Presentation projects.
- Update analyzers packages and severity of rules.

## 2.0.X
- Renamed the classes providing data to use the `Repository` suffix instead of `Endpoint` or `Service`.
- Renamed the Client library to Access.
Expand Down
4 changes: 2 additions & 2 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<Project>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.435">
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.0">
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>net7.0</TargetFramework>
<LangVersion>11.0</LangVersion>
<RootNamespace>ApplicationTemplate.DataAccess</RootNamespace>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors Condition="'$(Configuration)'=='Release'">true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
2 changes: 0 additions & 2 deletions src/app/ApplicationTemplate.Access/Framework/BaseMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public BaseMock(JsonSerializerOptions serializerOptions)
/// <returns>Deserialized value</returns>
/// <remarks>
/// If left empty, the <paramref name="resourceName" /> will implicitly be treated as "{callerTypeName}.{callerMemberName}.json".
/// If <paramref name="serializer" /> is left empty, the serializer defined in ctor. will be used".
/// Note that this will deserialize the first embedded resource whose name ends with the specified <paramref name="resourceName" />.
/// </remarks>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Globalization", "CA1307:Specify StringComparison", Justification = "Not available for Desktop")]
Expand Down Expand Up @@ -62,7 +61,6 @@ protected T GetFromEmbeddedResource<T>(
/// </summary>
/// <remarks>
/// If left empty, the <paramref name="resourceName" /> will implicitly be treated as "{callerTypeName}.{callerMemberName}.json".
/// If <paramref name="serializer" /> is left empty, the serializer defined in ctor. will be used".
/// Note that this will deserialize the first embedded resource whose name ends with the specified <paramref name="resourceName" />.
/// </remarks>
/// <typeparam name="T">Type of object</typeparam>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class ApplicationTemplateConfigurationExtensions
{
/// <summary>
/// Returns the <see cref="IConfigurationSection"/> for the options represented by <typeparamref name="T"/>.
/// The section name is either the options type name (minus the -Options prefix) or <paramref name="key"/>, if provided.
/// The section name is either the options type name (minus the -Options suffix) or <paramref name="key"/>, if provided.
/// <see cref="DefaultOptionsName{T}"/> as well.
/// </summary>
/// <typeparam name="T">The options type.</typeparam>
Expand All @@ -28,7 +28,7 @@ public static IConfigurationSection GetSectionForOptions<T>(

/// <summary>
/// Reads the current value for options.
/// The section name is either the options type name (minus the -Options prefix) or <paramref name="key"/>, if provided.
/// The section name is either the options type name (minus the -Options suffix) or <paramref name="key"/>, if provided.
/// <see cref="DefaultOptionsName{T}"/> as well.
/// </summary>
/// <typeparam name="T">The options type.</typeparam>
Expand All @@ -44,19 +44,21 @@ public static T ReadOptions<T>(this IConfiguration configuration, string key = n

/// <summary>
/// Gets the default name for options of type <paramref name="optionsType"/>.
/// Removes the -Options prefix if any.
/// Removes the -Options suffix if any.
/// </summary>
/// <param name="optionsType">The type from which to extract a default options name.</param>
public static string DefaultOptionsName(Type optionsType) => DefaultOptionsName(optionsType.Name);

/// <summary>
/// Gets the default name for options of the specified <paramref name="optionsTypeName"/>.
/// Removes the -Options prefix if any.
/// Removes the -Options suffix if any.
/// </summary>
/// <param name="optionsTypeName">The name of the type from which to extract a default options name.</param>
public static string DefaultOptionsName(string optionsTypeName) => Regex.Replace(optionsTypeName, @"Options$", string.Empty);

/// <summary>
/// Gets the default name for options of type <typeparamref name="T"/>.
/// Removes the -Options prefix if any.
/// Removes the -Options suffix if any.
/// </summary>
/// <typeparam name="T">The Options type.</typeparam>
public static string DefaultOptionsName<T>() => DefaultOptionsName(typeof(T));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static class ApplicationTemplateServiceCollectionExtensions
{
/// <summary>
/// Registers <typeparamref name="T"/> as an option bound to the <paramref name="configuration"/>
/// using the typename as key (minus the -Options prefix).
/// using the type name as key (minus the -Options suffix).
/// The validation, based on Data Annotations, happens when options are retrieved from DI,
/// not at the time of registration.
/// </summary>
Expand All @@ -26,8 +26,8 @@ public static class ApplicationTemplateServiceCollectionExtensions
/// <param name="configuration">The <see cref="IConfiguration"/>.</param>
/// <param name="key">
/// The configuration section key name to use.
/// If not provided, it will be the <typeparamref name="T"/> type name without the -Options prefix.
/// (see <see cref="ConfigurationExtensions.DefaultOptionsName(Type)"/>.
/// If not provided, it will be the <typeparamref name="T"/> type name without the -Options suffix.
/// (see <see cref="ApplicationTemplateConfigurationExtensions.DefaultOptionsName(Type)"/>.
/// </param>
/// <returns>The <see cref="IServiceCollection"/> with services registered.</returns>
public static IServiceCollection BindOptionsToConfiguration<T>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage
ResponseVersion = response.Version,
ResponseHeaders = response.Headers,
ResponseContentHeaders = response.Content.Headers,
ResponseContent = await response.Content.ReadAsStringAsync(),
ResponseContent = await response.Content.ReadAsStringAsync(CancellationToken.None),
});

return response;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public class JwtData<TPayload>
private TPayload _payload;

/// <summary>
/// Initialize a new JWT.
/// Initializes a new instance of the <see cref="JwtData{TPayload}"/> class.
/// </summary>
/// <remarks>
/// Header & Payload will be deserialized only if a JSON serializer is supplied.
/// Header and Payload will be deserialized only if a JSON serializer is supplied.
/// </remarks>
/// <param name="token">The raw token.</param>
/// <param name="jsonSerializerOptions">Should be a JSON serializer. Using a serializer for another format won't be RFC 7519 compliant.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>11.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors Condition="'$(Configuration)'=='Release'">true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions src/app/ApplicationTemplate.Business/Posts/IPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public interface IPostService
/// </summary>
/// <param name="ct"><see cref="CancellationToken"/></param>
/// <param name="postId">Post id</param>
/// <param name="post"><see cref="PostData"/></param>
/// <returns>Updated <see cref="PostData"/></returns>
/// <param name="post"><see cref="Post"/></param>
/// <returns>Updated <see cref="Post"/></returns>
Task<Post> Update(CancellationToken ct, long postId, Post post);

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<LangVersion>11.0</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<TreatWarningsAsErrors Condition="'$(Configuration)'=='Release'">true</TreatWarningsAsErrors>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using ApplicationTemplate;
using ApplicationTemplate.Business;
using ApplicationTemplate.DataAccess;
using MallardMessageHandlers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public static IHostBuilder AddConfiguration(this IHostBuilder hostBuilder, strin
/// </summary>
/// <param name="configurationBuilder">The configuration builder.</param>
/// <param name="folderPath">The folder containing the configuration override files.</param>
/// <param name="environmentManager">The environment manager.</param>
private static IConfigurationBuilder AddReadOnlyConfiguration(this IConfigurationBuilder configurationBuilder, string folderPath, IEnvironmentManager environmentManager)
{
return configurationBuilder.AddInMemoryCollection(GetCodeConfiguration(folderPath));
Expand Down Expand Up @@ -91,6 +92,7 @@ private static IConfigurationBuilder AddBaseConfiguration(this IConfigurationBui
/// The environment can be overriden by the user.
/// </summary>
/// <param name="configurationBuilder">The configuration builder.</param>
/// <param name="environmentManager">The environment manager.</param>
private static IConfigurationBuilder AddEnvironmentConfiguration(this IConfigurationBuilder configurationBuilder, IEnvironmentManager environmentManager)
{
var currentEnvironment = environmentManager.Current;
Expand Down Expand Up @@ -119,6 +121,7 @@ private static IConfigurationBuilder AddUserOverrideConfiguration(this IConfigur
/// Registers the <see cref="IConfiguration"/> as a singleton.
/// </summary>
/// <param name="hostBuilder">The host builder.</param>
/// <param name="environmentManager">The environment manager.</param>
private static IHostBuilder AddConfiguration(this IHostBuilder hostBuilder, IEnvironmentManager environmentManager)
{
if (hostBuilder is null)
Expand Down Expand Up @@ -190,7 +193,7 @@ public static AppSettingsFile[] GetAll()

_appSettingsFiles = executingAssembly
.GetManifestResourceNames()
.Where(fileName => fileName.ToUpperInvariant().Contains(AppSettingsFileName.ToUpperInvariant()))
.Where(fileName => fileName.ToUpperInvariant().Contains(AppSettingsFileName.ToUpperInvariant(), StringComparison.Ordinal))
.Select(fileName => new AppSettingsFile(fileName, executingAssembly))
.ToArray();
}
Expand Down
Loading

0 comments on commit 47f6f6f

Please sign in to comment.