Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
01372c5
Add Microsoft.CodeAnalysis.CSharp.CodeStyle
Evangelink Jul 16, 2021
b78f5aa
Add default .editorconfig options for C# code style
Evangelink Jul 16, 2021
2a8204b
Enforce `this.` qualifier for fields
Evangelink Jul 16, 2021
95448c5
Prefer expression body
Evangelink Jul 16, 2021
c9b43b7
Expression body for ctor
Evangelink Jul 16, 2021
4f3b8a1
Expression body for lambdas
Evangelink Jul 16, 2021
e9bc23b
Expression body for operators
Evangelink Jul 16, 2021
cdebf87
Add missing accessibilities
Evangelink Jul 16, 2021
19a387d
Make fields readonly
Evangelink Jul 16, 2021
c48599e
Disable warning for braces for ifs (IDE0011)
Evangelink Jul 16, 2021
737f624
IDE0090 use new(...) syntax
Evangelink Jul 16, 2021
0ff7037
IDE0041: Use 'is null' check
Evangelink Jul 16, 2021
669320f
Add Microsoft.CodeAnalysis.NetAnalyzers
Evangelink Jul 16, 2021
d3517b0
CA1825: Avoid zero-length array allocations
Evangelink Jul 16, 2021
3452926
CA1822: Mark members as static
Evangelink Jul 16, 2021
dfd39e8
CA2208: Instantiate argument exceptions correctly
Evangelink Jul 16, 2021
3e90f20
Fix formatting
Evangelink Jul 16, 2021
3fee4c1
Extract the analyzers into Directory.Build.props
Evangelink Jul 16, 2021
495a699
Remove underscore prefix for fields
Evangelink Jul 16, 2021
5aa0803
Run dotnet format command
Evangelink Jul 16, 2021
e20a0ef
Fix formatting issues
Evangelink Jul 16, 2021
27e394a
Fix some formatting issues
Evangelink Jul 16, 2021
a7584a4
Fix more alignment
Evangelink Jul 16, 2021
abed44c
Expression body for property, accessor and indexer
Evangelink Jul 17, 2021
3685fbf
Fix field name in reflection
Evangelink Jul 17, 2021
8093bf1
Add missing `this.` prefix
Evangelink Jul 17, 2021
63dac65
Post rebase cleanup
Evangelink Jul 22, 2021
f6bc602
Fix using order
Evangelink Jul 23, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# http://editorconfig.org

# top-most EditorConfig file
root = true

[*]
Expand All @@ -11,3 +13,162 @@ insert_final_newline = true

[*.yml]
indent_size = 2

### CSharp code style settings ###
[*.cs]
# Organize usings
dotnet_separate_import_directive_groups = false
dotnet_sort_system_directives_first = true

# Avoid "this." and "Me." if not necessary
dotnet_style_qualification_for_field = true:warning
dotnet_style_qualification_for_property = false:silent
dotnet_style_qualification_for_method = false:silent
dotnet_style_qualification_for_event = false:silent

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

# Parentheses preferences
dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent
dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:silent

# Modifier preferences
dotnet_style_require_accessibility_modifiers = for_non_interface_members:warning

# Field preferences
dotnet_style_readonly_field = true:warning

# Parameter preferences
dotnet_code_quality_unused_parameters = all:warning

# Expression-level preferences
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_explicit_tuple_names = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_object_initializer = true:suggestion
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_prefer_compound_assignment = true:warning
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
dotnet_style_prefer_inferred_tuple_names = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
dotnet_style_prefer_simplified_interpolation = true:suggestion

# Dispose rules (CA2000 and CA2213) ported to IDE analyzers. We already execute the CA rules on the repo, so disable the IDE ones.
dotnet_diagnostic.IDE0067.severity = none
dotnet_diagnostic.IDE0068.severity = none
dotnet_diagnostic.IDE0069.severity = none

#### C# Coding Conventions ####

# Prefer "var" everywhere
csharp_style_var_for_built_in_types = true:silent
csharp_style_var_when_type_is_apparent = true:silent
csharp_style_var_elsewhere = true:silent

# Prefer method-like constructs to have a block body, except for lambdas
csharp_style_expression_bodied_methods = true:warning
csharp_style_expression_bodied_constructors = true:warning
csharp_style_expression_bodied_operators = true:warning
csharp_style_expression_bodied_local_functions = true:warning
csharp_style_expression_bodied_lambdas = true:warning
csharp_style_expression_bodied_properties = true:warning
csharp_style_expression_bodied_accessors = true:warning
csharp_style_expression_bodied_indexers = true:warning

# Pattern matching preferences
csharp_style_pattern_matching_over_as_with_null_check = true:warning
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
csharp_style_prefer_switch_expression = true:suggestion

# Null-checking preferences
csharp_style_conditional_delegate_call = true:warning

# Modifier preferences
csharp_prefer_static_local_function = true:warning
csharp_preferred_modifier_order = public,private,protected,internal,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,volatile,async:silent

# Code-block preferences
csharp_prefer_braces = when_multiline:none
csharp_prefer_simple_using_statement = true:warning

# Expression-level preferences
csharp_style_unused_value_assignment_preference = discard_variable:warning
csharp_prefer_simple_default_expression = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_pattern_local_over_anonymous_function = true:suggestion
csharp_style_prefer_index_operator = true:suggestion
csharp_style_prefer_range_operator = true:suggestion
csharp_style_throw_expression = true:suggestion
csharp_style_unused_value_expression_statement_preference = discard_variable:silent

# 'using' directive preferences
csharp_using_directive_placement = outside_namespace:silent

#### C# Formatting Rules ####

# New line preferences
csharp_new_line_before_open_brace = all
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_between_query_expression_clauses = true

# Indentation preferences
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents = true
csharp_indent_case_contents_when_block = true
csharp_indent_labels = no_change
csharp_indent_switch_labels = true

# Space preferences
csharp_space_after_cast = false
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_after_comma = true
csharp_space_after_dot = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_after_semicolon_in_for_statement = true
csharp_space_around_binary_operators = before_and_after
csharp_space_around_declaration_statements = false
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_before_comma = false
csharp_space_before_dot = false
csharp_space_before_open_square_brackets = false
csharp_space_before_semicolon_in_for_statement = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_parentheses = false
csharp_space_between_square_brackets = false

# IDE0011: Add braces
dotnet_diagnostic.IDE0011.severity = none

# IDE0090: Use 'new(...)'
dotnet_diagnostic.IDE0090.severity = warning

# IDE0041: Use 'is null' check
dotnet_diagnostic.IDE0041.severity = warning

# CA1825: Avoid zero-length array allocations
dotnet_diagnostic.CA1825.severity = warning

# CA1822: Mark members as static
dotnet_diagnostic.CA1822.severity = warning

# CA2208: Instantiate argument exceptions correctly
dotnet_diagnostic.CA2208.severity = warning
22 changes: 22 additions & 0 deletions src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
<PackageVersion_NunitXmlTestLogger>3.0.107</PackageVersion_NunitXmlTestLogger>
<PackageVersion_Shouldly>4.0.3</PackageVersion_Shouldly>

<PackageVersion_MicrosoftCodeStyle>3.10.0</PackageVersion_MicrosoftCodeStyle>
<PackageVersion_MicrosoftNetAnalyzers>5.0.3</PackageVersion_MicrosoftNetAnalyzers>
</PropertyGroup>

<ItemGroup Condition=" '$(IsTestProject)' == 'true' ">
Expand Down Expand Up @@ -74,4 +76,24 @@
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.2" PrivateAssets="All" />
</ItemGroup>

<!-- Workaround to enable .editorconfig based analyzer configuration until dotnet compilers support .editorconfig based configuration -->
<PropertyGroup>
<SkipDefaultEditorConfigAsAdditionalFile>true</SkipDefaultEditorConfigAsAdditionalFile>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="$(MSBuildThisFileDirectory)..\.editorconfig" />
</ItemGroup>

<!-- Common code quality and styling analyzers -->
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeStyle" Version="$(PackageVersion_MicrosoftCodeStyle)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis.NetAnalyzers" Version="$(PackageVersion_MicrosoftNetAnalyzers)">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
7 changes: 2 additions & 5 deletions src/GitTools.Testing/Fixtures/BaseGitFlowRepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@ public BaseGitFlowRepositoryFixture(string initialVersion) :
///
/// The initial setup actions will be performed before branching develop
/// </summary>
public BaseGitFlowRepositoryFixture(Action<IRepository> initialMainAction)
{
SetupRepo(initialMainAction);
}
public BaseGitFlowRepositoryFixture(Action<IRepository> initialMainAction) => SetupRepo(initialMainAction);

void SetupRepo(Action<IRepository> initialMainAction)
private void SetupRepo(Action<IRepository> initialMainAction)
{
var randomFile = Path.Combine(Repository.Info.WorkingDirectory, Guid.NewGuid().ToString());
File.WriteAllText(randomFile, string.Empty);
Expand Down
10 changes: 2 additions & 8 deletions src/GitTools.Testing/Fixtures/RemoteRepositoryFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ namespace GitTools.Testing
public class RemoteRepositoryFixture : RepositoryFixtureBase
{
public RemoteRepositoryFixture(Func<string, IRepository> builder)
: base(builder)
{
CreateLocalRepository();
}
: base(builder) => CreateLocalRepository();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no analyzer to help with this kind of formatting. I will make the changes manually, just let me know your preference between:

  1. Always break after =>
  2. Always break before => (that's the one I usually go for but that's purely personal)
  3. Break only on long line.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3


public RemoteRepositoryFixture() : this(CreateNewRepository)
{
Expand All @@ -35,10 +32,7 @@ private static IRepository CreateNewRepository(string path)
return repo;
}

private void CreateLocalRepository()
{
LocalRepositoryFixture = CloneRepository();
}
private void CreateLocalRepository() => LocalRepositoryFixture = CloneRepository();

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Expand Down
38 changes: 13 additions & 25 deletions src/GitTools.Testing/Fixtures/RepositoryFixtureBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace GitTools.Testing
/// </summary>
public abstract class RepositoryFixtureBase : IDisposable
{
readonly SequenceDiagram _sequenceDiagram;
private readonly SequenceDiagram sequenceDiagram;

protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder)
: this(repoBuilder(PathHelper.GetTempPath()))
Expand All @@ -18,23 +18,17 @@ protected RepositoryFixtureBase(Func<string, IRepository> repoBuilder)

protected RepositoryFixtureBase(IRepository repository)
{
_sequenceDiagram = new SequenceDiagram();
this.sequenceDiagram = new SequenceDiagram();
Repository = repository;
Repository.Config.Set("user.name", "Test");
Repository.Config.Set("user.email", "[email protected]");
}

public IRepository Repository { get; }

public string RepositoryPath
{
get { return Repository.Info.WorkingDirectory.TrimEnd('\\'); }
}
public string RepositoryPath => Repository.Info.WorkingDirectory.TrimEnd('\\');

public SequenceDiagram SequenceDiagram
{
get { return _sequenceDiagram; }
}
public SequenceDiagram SequenceDiagram => this.sequenceDiagram;

/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
Expand All @@ -53,21 +47,15 @@ public virtual void Dispose()
e.Message);
}

_sequenceDiagram.End();
this.sequenceDiagram.End();
Console.WriteLine("**Visualisation of test:**");
Console.WriteLine(string.Empty);
Console.WriteLine(_sequenceDiagram.GetDiagram());
Console.WriteLine(this.sequenceDiagram.GetDiagram());
}

public void Checkout(string branch)
{
Commands.Checkout(Repository, branch);
}
public void Checkout(string branch) => Commands.Checkout(Repository, branch);

public static void Init(string path)
{
GitTestExtensions.ExecuteGitCmd($"init {path} -b main");
}
public static void Init(string path) => GitTestExtensions.ExecuteGitCmd($"init {path} -b main");

public void MakeATaggedCommit(string tag)
{
Expand All @@ -77,28 +65,28 @@ public void MakeATaggedCommit(string tag)

public void ApplyTag(string tag)
{
_sequenceDiagram.ApplyTag(tag, Repository.Head.FriendlyName);
this.sequenceDiagram.ApplyTag(tag, Repository.Head.FriendlyName);
Repository.ApplyTag(tag);
}

public void BranchTo(string branchName, string @as = null)
{
_sequenceDiagram.BranchTo(branchName, Repository.Head.FriendlyName, @as);
this.sequenceDiagram.BranchTo(branchName, Repository.Head.FriendlyName, @as);
var branch = Repository.CreateBranch(branchName);
Commands.Checkout(Repository, branch);
}

public void BranchToFromTag(string branchName, string fromTag, string onBranch, string @as = null)
{
_sequenceDiagram.BranchToFromTag(branchName, fromTag, onBranch, @as);
this.sequenceDiagram.BranchToFromTag(branchName, fromTag, onBranch, @as);
var branch = Repository.CreateBranch(branchName);
Commands.Checkout(Repository, branch);
}

public void MakeACommit()
{
var to = Repository.Head.FriendlyName;
_sequenceDiagram.MakeACommit(to);
this.sequenceDiagram.MakeACommit(to);
Repository.MakeACommit();
}

Expand All @@ -107,7 +95,7 @@ public void MakeACommit()
/// </summary>
public void MergeNoFF(string mergeSource)
{
_sequenceDiagram.Merge(mergeSource, Repository.Head.FriendlyName);
this.sequenceDiagram.Merge(mergeSource, Repository.Head.FriendlyName);
Repository.MergeNoFF(mergeSource, Generate.SignatureNow());
}

Expand Down
Loading