Skip to content

Commit

Permalink
Fix/resharper warnings (#3790)
Browse files Browse the repository at this point in the history
* Fix some resharper warnings

* fix resharper warnings and introduced namespacing for internals in the low level client

* fixes

* addressed resharper warnings in Elasticsearch.Net

* started to tackle NEST warnings

* fix obsolete warning from forward port of #3770

(cherry picked from commit 7c4996f)

* tackled NEST warnings further

* split requests/descriptors

* tackled more warnings

* tackled more warnings
  • Loading branch information
Mpdreamz authored Jun 2, 2019
1 parent 7c364be commit c330114
Show file tree
Hide file tree
Showing 1,821 changed files with 26,109 additions and 25,164 deletions.
137 changes: 79 additions & 58 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,85 +1,111 @@
root=true

[*.cs]
trim_trailing_whitespace=true
insert_final_newline=true

[*]
indent_style = tab
indent_size = 4

[*.cshtml]
indent_style = tab
indent_size = 4

[*.{fs,fsx,yml}]
indent_style = space
indent_size = 4

[*.{md,markdown,json,js,csproj,fsproj,targets,targets,props}]
indent_style = space
indent_size = 2

# Dotnet code style settings:
[*.{cs,vb}]

# ---
# naming conventions https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-naming-conventions
# currently not supported in Rider/Resharper so not using these for now
# ---

# ---
# langugage conventions https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#language-conventions

# Sort using and Import directives with System.* appearing first
dotnet_sort_system_directives_first = true

# Prefer this.X except for _fields
# TODO can we force _ for private fields?
# TODO elevate severity after code cleanup to warning minimum
# TODO use language latest
dotnet_style_qualification_for_field = false:suggestion
dotnet_style_qualification_for_property = false:suggestion
dotnet_style_qualification_for_method = false:suggestion
dotnet_style_qualification_for_event = false:suggestion
dotnet_style_qualification_for_field = false:error
dotnet_style_qualification_for_property = false:error
dotnet_style_qualification_for_method = false:error
dotnet_style_qualification_for_event = false:error

# Use language keywords instead of framework type names for type references
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
dotnet_style_predefined_type_for_member_access = true:suggestion
dotnet_style_predefined_type_for_locals_parameters_members = true:error
dotnet_style_predefined_type_for_member_access = true:error

# Suggest more modern language features when available
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_object_initializer = true:error
dotnet_style_collection_initializer = true:error
dotnet_style_explicit_tuple_names = true:error
dotnet_style_prefer_inferred_anonymous_type_member_names = true:error
dotnet_style_prefer_inferred_tuple_names = true:error
dotnet_style_coalesce_expression = true:error
dotnet_style_null_propagation = true:error

dotnet_style_require_accessibility_modifiers = for_non_interface_members:suggestion
dotnet_style_readonly_field = true:suggestion
dotnet_style_require_accessibility_modifiers = for_non_interface_members:error
dotnet_style_readonly_field = true:error

# CSharp code style settings:
[*.cs]
# Prefer "var" everywhere
csharp_style_var_for_built_in_types = true:suggestion
csharp_style_var_when_type_is_apparent = true:suggestion
csharp_style_var_elsewhere = true:suggestion
csharp_style_var_for_built_in_types = true:error
csharp_style_var_when_type_is_apparent = true:error
csharp_style_var_elsewhere = true:error

csharp_style_expression_bodied_methods = true:suggestion
csharp_style_expression_bodied_constructors = true:suggestion
csharp_style_expression_bodied_operators = true:suggestion
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_methods = true:error
csharp_style_expression_bodied_constructors = true:error
csharp_style_expression_bodied_operators = true:error
csharp_style_expression_bodied_properties = true:error
csharp_style_expression_bodied_indexers = true:error
csharp_style_expression_bodied_accessors = true:error

# Suggest more modern language features when available
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_conditional_delegate_call = true:suggestion
csharp_style_pattern_matching_over_is_with_cast_check = true:error
csharp_style_pattern_matching_over_as_with_null_check = true:error
csharp_style_inlined_variable_declaration = true:error
csharp_style_deconstructed_variable_declaration = true:error
csharp_style_pattern_local_over_anonymous_function = true:error
csharp_style_throw_expression = true:error
csharp_style_conditional_delegate_call = true:error

csharp_prefer_braces = false:suggestion
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:suggestion
csharp_prefer_braces = false:warning
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:error

# ---
# formatting conventions https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference#formatting-conventions

# Newline settings (Allman yo!)
csharp_new_line_before_open_brace = all:suggestion
csharp_new_line_before_else = true:suggestion
csharp_new_line_before_catch = true:suggestion
csharp_new_line_before_finally = true:suggestion
csharp_new_line_before_open_brace = all:error
csharp_new_line_before_else = true:error
csharp_new_line_before_catch = true:error
csharp_new_line_before_finally = true:error
csharp_new_line_before_members_in_object_initializers = true
# just a suggestion do to our JSON tests that use anonymous types to
# represent json quite a bit (makes copy paste easier).
csharp_new_line_before_members_in_anonymous_types = true:suggestion
csharp_new_line_between_query_expression_clauses = true:suggestion
csharp_new_line_between_query_expression_clauses = true:error

# Indent
csharp_indent_case_contents = true:suggestion
csharp_indent_switch_labels = true:suggestion
csharp_space_after_cast = false:suggestion
csharp_space_after_keywords_in_control_flow_statements = true:suggestion
csharp_space_between_method_declaration_parameter_list_parentheses = false:suggestion
csharp_space_between_method_call_parameter_list_parentheses = false:suggestion
csharp_indent_case_contents = true:error
csharp_indent_switch_labels = true:error
csharp_space_after_cast = false:error
csharp_space_after_keywords_in_control_flow_statements = true:error
csharp_space_between_method_declaration_parameter_list_parentheses = false:error
csharp_space_between_method_call_parameter_list_parentheses = false:error

#Wrap
csharp_preserve_single_line_statements = false:suggestion
csharp_preserve_single_line_blocks = true:suggestion
csharp_preserve_single_line_statements = false:error
csharp_preserve_single_line_blocks = true:error

# Resharper
resharper_csharp_braces_for_lock=required_for_complex
Expand All @@ -92,11 +118,6 @@ resharper_csharp_braces_for_ifelse=required_for_complex

resharper_csharp_accessor_owner_body=expression_body

# Override source included files
[SynchronizedCollection.cs]
csharp_style_var_for_built_in_types = false:none
csharp_style_var_when_type_is_apparent = false:none
csharp_style_var_elsewhere = false:none
dotnet_style_predefined_type_for_locals_parameters_members = false:none
dotnet_style_predefined_type_for_member_access = false:none
csharp_prefer_braces = false:none

resharper_redundant_case_label_highlighting=do_not_show

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class GeneratorLocations

public static readonly Assembly Assembly = typeof(Generator.ApiGenerator).Assembly;

private static string _root = null;
private static string _root;
public static string Root
{
get
Expand Down
1 change: 0 additions & 1 deletion src/CodeGeneration/ApiGenerator/Domain/Code/CsharpNames.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using ApiGenerator.Configuration;
using ApiGenerator.Generator;
using CsQuery.ExtensionMethods.Internal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ private List<UrlPart> CreateDescriptorArgs(IReadOnlyCollection<UrlPart> parts)

public string DescriptorArguments()
{
string codeArgs = null;
string codeArgs;
if (CodeConfiguration.DescriptorConstructors.TryGetValue(CsharpNames.DescriptorName, out codeArgs))
codeArgs += ",";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ string generic
ctors.AddRange(from path in paths.Where(path => path.HasResolvableArguments)
let baseArgs = path.AutoResolveBaseArguments(generic)
let constructorArgs = path.AutoResolveConstructorArguments
let baseOrThis = inheritsFromPlainRequestBase ? "this" : "base"
let baseOrThis = inheritsFromPlainRequestBase
? "this"
: "base"
let generated = $"public {typeName}({constructorArgs}) : {baseOrThis}({baseArgs})"
select new Constructor
{
Expand All @@ -82,6 +84,7 @@ string generic
{
Parameterless = string.IsNullOrEmpty(docPathConstArgs),
Generated = $"public {typeName}({docPathConstArgs}) : this({docPathBaseArgs})",

AdditionalCode = $"partial void DocumentFromPath({generic} document);",
Description = docPath.GetXmlDocs(Indent, skipResolvable: true, documentConstructor: true),
Body = "=> DocumentFromPath(documentWithId);"
Expand All @@ -90,15 +93,13 @@ string generic
}
var constructors = ctors.GroupBy(c => c.Generated.Split(new[] { ':' }, 2)[0]).Select(g => g.Last()).ToList();
if (!constructors.Any(c=>c.Parameterless))
{
constructors.Add(new Constructor
{
Parameterless = true,
Generated = $"protected {typeName}() : base()",
Description =
$"///<summary>Used for serialization purposes, making sure we have a parameterless constructor</summary>{Indent}[SerializationConstructor]",
});
}
return constructors;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ public IEnumerable<FluentRouteSetter> GetFluentRouteSetters()
var routeValue = "v";
var routeSetter = p.Required ? "Required" : "Optional";

if (paramName == "metric" || paramName == "watcherStatsMetric") routeValue = "(Metrics)v";
else if (paramName == "indexMetric") routeValue = "(IndexMetrics)v";

var code =
$"public {returnType} {p.InterfaceName}({p.ClrTypeName} {paramName}) => Assign({paramName}, (a,v)=>a.RouteValues.{routeSetter}(\"{p.Name}\", {routeValue}));";
var xmlDoc = $"///<summary>{p.Description}</summary>";
Expand All @@ -53,7 +50,7 @@ public IEnumerable<FluentRouteSetter> GetFluentRouteSetters()
}
if (paramName == "index" && p.Type == "list")
{
code = $"public {returnType} AllIndices() => this.Index(Indices.All);";
code = $"public {returnType} AllIndices() => Index(Indices.All);";
xmlDoc = $"///<summary>A shortcut into calling Index(Indices.All)</summary>";
setters.Add(new FluentRouteSetter { Code = code, XmlDoc = xmlDoc });
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,18 @@ public string UrlInCode
{
get
{
string Evaluator(Match m)
{
var arg = m.Groups.Last().Value.ToCamelCase();
return $"{{{arg}:{arg}}}";
}

var url = Path.TrimStart('/');
var pattern = string.Join("|", Url.Parts.Select(p => p.Name));
var urlCode = $"\"{url}\"";
if (Path.Contains("{"))
{
var patchedUrl = Regex.Replace(url, "{(" + pattern + ")}", "{$1:$1}");
var patchedUrl = Regex.Replace(url, "{(" + pattern + ")}", Evaluator);
urlCode = $"Url($\"{patchedUrl}\")";
}
return urlCode;
Expand Down
6 changes: 3 additions & 3 deletions src/CodeGeneration/ApiGenerator/Domain/RestApiSpec.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using System.Collections.ObjectModel;
using System.Linq;
using ApiGenerator.Domain.Specification;
using CsQuery.ExtensionMethods.Internal;
using Microsoft.CodeAnalysis.CSharp;

namespace ApiGenerator.Domain
{
Expand Down Expand Up @@ -38,11 +36,13 @@ string CreateName(string name, string methodName, string @namespace)
if (
name.ToLowerInvariant().Contains("metric")
||(name.ToLowerInvariant() == "status")
)
)
{
if (methodName.StartsWith(@namespace))
return methodName + name;
else
return @namespace + methodName + name;
}

return name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using ApiGenerator.Domain.Code;
using Newtonsoft.Json;

namespace ApiGenerator.Domain.Specification
Expand Down Expand Up @@ -36,7 +35,7 @@ public IReadOnlyCollection<UrlPath> Paths

private static readonly string[] DocumentApiParts = { "index", "id" };

public bool IsDocumentApi => UrlInformation.IsADocumentRoute(Parts);
public bool IsDocumentApi => IsADocumentRoute(Parts);

public static bool IsADocumentRoute(IReadOnlyCollection<UrlPart> parts) =>
parts.Count() == DocumentApiParts.Length
Expand Down
10 changes: 5 additions & 5 deletions src/CodeGeneration/ApiGenerator/Domain/Specification/UrlPart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ public string Argument
{
case "int":
case "string":
return Type + " " + Name;
return Type + " " + NameAsArgument;
case "list":
return "string " + Name;
return "string " + NameAsArgument;
case "enum":
return Name.ToPascalCase() + " " + Name;
return Name.ToPascalCase() + " " + NameAsArgument;
case "number":
return "string " + Name;
return "string " + NameAsArgument;
default:
return Type + " " + Name;
return Type + " " + NameAsArgument;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Linq;
using ApiGenerator.Configuration;
using ApiGenerator.Domain;
using ShellProgressBar;
Expand All @@ -10,10 +11,14 @@ public class DescriptorsGenerator : RazorGeneratorBase

public override void Generate(RestApiSpec spec, ProgressBar progressBar)
{
var view = ViewLocations.HighLevel("Descriptors", "Descriptors.cshtml");
var target = GeneratorLocations.HighLevel("_Generated", "Descriptors.generated.cs");

var view = ViewLocations.HighLevel("Descriptors", "RequestDescriptorBase.cshtml");
var target = GeneratorLocations.HighLevel("Descriptors.cs");
DoRazor(spec, view, target);

var dependantView = ViewLocations.HighLevel("Descriptors", "Descriptors.cshtml");
string Target(string id) => GeneratorLocations.HighLevel($"Descriptors.{id}.cs");
var namespaced = spec.EndpointsPerNamespace.ToList();
DoRazorDependantFiles(progressBar, namespaced, dependantView, kv => kv.Key, id => Target(id));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.IO;
using ApiGenerator.Configuration;
using ApiGenerator.Domain;
using ShellProgressBar;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ protected void DoRazor<TModel>(TModel model, string viewLocation, string targetL
}
catch (TemplateGenerationException e)
{
foreach (var d in e.Diagnostics)
{
Console.WriteLine(d.GetMessage());
}
throw e;
foreach (var d in e.Diagnostics) Console.WriteLine(d.GetMessage());
throw;
}
}

Expand All @@ -57,7 +54,7 @@ protected void DoRazorDependantFiles<TModel>(
protected static void WriteFormattedCsharpFile(string path, string contents)
{
var tree = CSharpSyntaxTree.ParseText(contents);
var root = tree.GetRoot().NormalizeWhitespace(indentation:"\t", "\n", elasticTrivia: false);
var root = tree.GetRoot().NormalizeWhitespace(indentation:"\t", "\n");
contents = root.ToFullString();
File.WriteAllText(path, contents);
}
Expand Down
Loading

0 comments on commit c330114

Please sign in to comment.