Skip to content

Commit b99d40d

Browse files
Add .editorconfig and formatted non-generated C# code (#1204)
1 parent 36e0fcb commit b99d40d

File tree

8 files changed

+209
-25
lines changed

8 files changed

+209
-25
lines changed

.editorconfig

+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
# editorconfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Default settings:
7+
# A newline ending every file
8+
# Use 4 spaces as indentation
9+
[*]
10+
insert_final_newline = true
11+
indent_style = space
12+
indent_size = 4
13+
trim_trailing_whitespace = true
14+
15+
# Generated code
16+
[*{_AssemblyInfo.cs,.notsupported.cs,AsmOffsets.cs}]
17+
generated_code = true
18+
19+
# C# files
20+
[*.cs]
21+
# New line preferences
22+
csharp_new_line_before_open_brace = all
23+
csharp_new_line_before_else = true
24+
csharp_new_line_before_catch = true
25+
csharp_new_line_before_finally = true
26+
csharp_new_line_before_members_in_object_initializers = true
27+
csharp_new_line_before_members_in_anonymous_types = true
28+
csharp_new_line_between_query_expression_clauses = true
29+
30+
# Indentation preferences
31+
csharp_indent_block_contents = true
32+
csharp_indent_braces = false
33+
csharp_indent_case_contents = true
34+
csharp_indent_case_contents_when_block = true
35+
csharp_indent_switch_labels = true
36+
csharp_indent_labels = one_less_than_current
37+
38+
# Modifier preferences
39+
csharp_preferred_modifier_order = public,private,protected,internal,file,static,extern,new,virtual,abstract,sealed,override,readonly,unsafe,required,volatile,async:suggestion
40+
41+
# avoid this. unless absolutely necessary
42+
dotnet_style_qualification_for_field = false:suggestion
43+
dotnet_style_qualification_for_property = false:suggestion
44+
dotnet_style_qualification_for_method = false:suggestion
45+
dotnet_style_qualification_for_event = false:suggestion
46+
47+
# Types: use keywords instead of BCL types, and permit var only when the type is clear
48+
csharp_style_var_for_built_in_types = false:suggestion
49+
csharp_style_var_when_type_is_apparent = false:none
50+
csharp_style_var_elsewhere = false:suggestion
51+
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
52+
dotnet_style_predefined_type_for_member_access = true:suggestion
53+
54+
# name all constant fields using PascalCase
55+
dotnet_naming_rule.constant_fields_should_be_pascal_case.severity = suggestion
56+
dotnet_naming_rule.constant_fields_should_be_pascal_case.symbols = constant_fields
57+
dotnet_naming_rule.constant_fields_should_be_pascal_case.style = pascal_case_style
58+
dotnet_naming_symbols.constant_fields.applicable_kinds = field
59+
dotnet_naming_symbols.constant_fields.required_modifiers = const
60+
dotnet_naming_style.pascal_case_style.capitalization = pascal_case
61+
62+
# static fields should have s_ prefix
63+
dotnet_naming_rule.static_fields_should_have_prefix.severity = suggestion
64+
dotnet_naming_rule.static_fields_should_have_prefix.symbols = static_fields
65+
dotnet_naming_rule.static_fields_should_have_prefix.style = static_prefix_style
66+
dotnet_naming_symbols.static_fields.applicable_kinds = field
67+
dotnet_naming_symbols.static_fields.required_modifiers = static
68+
dotnet_naming_symbols.static_fields.applicable_accessibilities = private, internal, private_protected
69+
dotnet_naming_style.static_prefix_style.required_prefix = s_
70+
dotnet_naming_style.static_prefix_style.capitalization = camel_case
71+
72+
# internal and private fields should be _camelCase
73+
dotnet_naming_rule.camel_case_for_private_internal_fields.severity = suggestion
74+
dotnet_naming_rule.camel_case_for_private_internal_fields.symbols = private_internal_fields
75+
dotnet_naming_rule.camel_case_for_private_internal_fields.style = camel_case_underscore_style
76+
dotnet_naming_symbols.private_internal_fields.applicable_kinds = field
77+
dotnet_naming_symbols.private_internal_fields.applicable_accessibilities = private, internal
78+
dotnet_naming_style.camel_case_underscore_style.required_prefix = _
79+
dotnet_naming_style.camel_case_underscore_style.capitalization = camel_case
80+
81+
# Code style defaults
82+
csharp_using_directive_placement = outside_namespace:suggestion
83+
dotnet_sort_system_directives_first = true
84+
csharp_prefer_braces = true:silent
85+
csharp_preserve_single_line_blocks = true:none
86+
csharp_preserve_single_line_statements = false:none
87+
csharp_prefer_static_local_function = true:suggestion
88+
csharp_prefer_simple_using_statement = false:none
89+
csharp_style_prefer_switch_expression = true:suggestion
90+
dotnet_style_readonly_field = true:suggestion
91+
92+
# Expression-level preferences
93+
dotnet_style_object_initializer = true:suggestion
94+
dotnet_style_collection_initializer = true:suggestion
95+
dotnet_style_explicit_tuple_names = true:suggestion
96+
dotnet_style_coalesce_expression = true:suggestion
97+
dotnet_style_null_propagation = true:suggestion
98+
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:suggestion
99+
dotnet_style_prefer_inferred_tuple_names = true:suggestion
100+
dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
101+
dotnet_style_prefer_auto_properties = true:suggestion
102+
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
103+
dotnet_style_prefer_conditional_expression_over_return = true:silent
104+
csharp_prefer_simple_default_expression = true:suggestion
105+
106+
# Expression-bodied members
107+
csharp_style_expression_bodied_methods = true:silent
108+
csharp_style_expression_bodied_constructors = true:silent
109+
csharp_style_expression_bodied_operators = true:silent
110+
csharp_style_expression_bodied_properties = true:silent
111+
csharp_style_expression_bodied_indexers = true:silent
112+
csharp_style_expression_bodied_accessors = true:silent
113+
csharp_style_expression_bodied_lambdas = true:silent
114+
csharp_style_expression_bodied_local_functions = true:silent
115+
116+
# Pattern matching
117+
csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
118+
csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
119+
csharp_style_inlined_variable_declaration = true:suggestion
120+
121+
# Null checking preferences
122+
csharp_style_throw_expression = true:suggestion
123+
csharp_style_conditional_delegate_call = true:suggestion
124+
125+
# Other features
126+
csharp_style_prefer_index_operator = false:none
127+
csharp_style_prefer_range_operator = false:none
128+
csharp_style_pattern_local_over_anonymous_function = false:none
129+
130+
# Space preferences
131+
csharp_space_after_cast = false
132+
csharp_space_after_colon_in_inheritance_clause = true
133+
csharp_space_after_comma = true
134+
csharp_space_after_dot = false
135+
csharp_space_after_keywords_in_control_flow_statements = true
136+
csharp_space_after_semicolon_in_for_statement = true
137+
csharp_space_around_binary_operators = before_and_after
138+
csharp_space_around_declaration_statements = do_not_ignore
139+
csharp_space_before_colon_in_inheritance_clause = true
140+
csharp_space_before_comma = false
141+
csharp_space_before_dot = false
142+
csharp_space_before_open_square_brackets = false
143+
csharp_space_before_semicolon_in_for_statement = false
144+
csharp_space_between_empty_square_brackets = false
145+
csharp_space_between_method_call_empty_parameter_list_parentheses = false
146+
csharp_space_between_method_call_name_and_opening_parenthesis = false
147+
csharp_space_between_method_call_parameter_list_parentheses = false
148+
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
149+
csharp_space_between_method_declaration_name_and_open_parenthesis = false
150+
csharp_space_between_method_declaration_parameter_list_parentheses = false
151+
csharp_space_between_parentheses = false
152+
csharp_space_between_square_brackets = false
153+
154+
# License header
155+
file_header_template = Licensed to the .NET Foundation under one or more agreements.\nThe .NET Foundation licenses this file to you under the MIT license.
156+
157+
# Xml project files
158+
[*.{csproj,vbproj,vcxproj,vcxproj.filters,proj,nativeproj,locproj}]
159+
indent_size = 2
160+
161+
[*.{csproj,vbproj,proj,nativeproj,locproj}]
162+
charset = utf-8
163+
164+
# Xml build files
165+
[*.builds]
166+
indent_size = 2
167+
168+
# Xml files
169+
[*.{xml,stylecop,resx,ruleset}]
170+
indent_size = 2
171+
172+
# Xml config files
173+
[*.{props,targets,config,nuspec}]
174+
indent_size = 2
175+
176+
# YAML config files
177+
[*.{yml,yaml}]
178+
indent_size = 2
179+
180+
# Shell scripts
181+
[*.sh]
182+
end_of_line = lf
183+
[*.{cmd,bat}]
184+
end_of_line = crlf

src/packageSourceGenerator/PackageSourceGeneratorTask/GenerateProject.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class GenerateProject : Task
4141
[Required]
4242
public required string ProjectRoot { get; set; }
4343

44-
/// <summary>
44+
/// <summary>
4545
/// The package's compile items, including target framework metadata.
4646
/// </summary>
4747
public ITaskItem[] CompileItems { get; set; } = Array.Empty<ITaskItem>();
@@ -72,16 +72,16 @@ public override bool Execute()
7272

7373
if (targetFrameworks.Length == 0)
7474
targetFrameworks = PackageDependencies.Select(packageDependency => packageDependency.GetMetadata(SharedMetadata.TargetFrameworkMetadataName)).ToArray();
75-
75+
7676
if (targetFrameworks.Length == 0)
7777
targetFrameworks = FrameworkReferences.Select(frameworkReference => frameworkReference.GetMetadata(SharedMetadata.TargetFrameworkMetadataName)).ToArray();
78-
78+
7979
targetFrameworks = targetFrameworks.Distinct()
8080
.Order()
8181
.ToArray();
8282

8383
// If no target framework is supplied, fallback to netstandard2.0.
84-
projectContent = projectContent.Replace("$$TargetFrameworks$$",
84+
projectContent = projectContent.Replace("$$TargetFrameworks$$",
8585
targetFrameworks.Length > 0 ? string.Join(';', targetFrameworks) : "netstandard2.0");
8686

8787
projectContent = projectContent.Replace("$$PackageVersion$$", PackageVersion);
@@ -95,7 +95,7 @@ public override bool Execute()
9595
foreach (ITaskItem packageDependency in PackageDependencies.Where(packageDependency => packageDependency.GetMetadata(SharedMetadata.TargetFrameworkMetadataName) == targetFramework))
9696
{
9797
string dependencyVersion = packageDependency.GetMetadata("Version");
98-
string dependencyProjectRelativePath = Path.Combine(packageDependency.ItemSpec.ToLowerInvariant(), dependencyVersion, $"{packageDependency.ItemSpec}.{dependencyVersion}.csproj");
98+
string dependencyProjectRelativePath = Path.Combine(packageDependency.ItemSpec.ToLowerInvariant(), dependencyVersion, $"{packageDependency.ItemSpec}.{dependencyVersion}.csproj");
9999

100100
// If the dependency is on the package reference allowed list (i.e. for source-build-externals packages like Newtonsoft.Json), emit a package reference. Otherwise, emit a project reference.
101101
if (AllowedPackageReference is not null && AllowedPackageReference.Contains(packageDependency.ItemSpec))

src/packageSourceGenerator/PackageSourceGeneratorTask/GetPackageItems.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Linq;
76
using System.IO;
7+
using System.Linq;
88
using System.Reflection;
99
using System.Text.RegularExpressions;
1010
using Microsoft.Build.Framework;
1111
using Microsoft.Build.Utilities;
1212
using NuGet.Client;
1313
using NuGet.ContentModel;
14-
using NuGet.Packaging;
1514
using NuGet.Frameworks;
15+
using NuGet.Packaging;
1616
using NuGet.Packaging.Core;
1717

1818
namespace Microsoft.DotNet.SourceBuild.Tasks

tests/SbrpTests/DummyAttributeTypeProvider.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ internal class DummyAttributeTypeProvider : ICustomAttributeTypeProvider<Type?>
2525
public Type? GetTypeFromSerializedName(string name) => default(Type);
2626

2727
public PrimitiveTypeCode GetUnderlyingEnumType(Type? type) => default(PrimitiveTypeCode);
28-
28+
2929
public bool IsSystemType(Type? type) => default(bool);
3030
}

tests/SbrpTests/ExecuteHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public static (Process Process, string StdOut, string StdErr) ExecuteProcess(
1818
bool logOutput = false,
1919
Action<Process>? configure = null,
2020
int millisecondTimeout = -1)
21-
{
21+
{
2222
outputHelper.WriteLine($"Executing: {fileName} {args}");
2323

2424
Process process = new()
@@ -116,4 +116,4 @@ public static void ValidateExitCode((Process Process, string StdOut, string StdE
116116
throw new InvalidOperationException(msg);
117117
}
118118
}
119-
}
119+
}

tests/SbrpTests/GenerateScriptTests.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public enum PackageType
2727
new object[] { "System.Buffers", "4.6.0", PackageType.Reference },
2828
new object[] { "System.Security.Cryptography.ProtectedData", "8.0.0", PackageType.Reference },
2929
};
30-
30+
3131
public string SandboxDirectory { get; set; }
3232
public ITestOutputHelper Output { get; set; }
3333

@@ -38,7 +38,7 @@ public GenerateScriptTests(ITestOutputHelper output)
3838
Directory.CreateDirectory(SandboxDirectory);
3939
}
4040

41-
[Theory]
41+
[Theory]
4242
[MemberData(nameof(GenerateScriptTests.Data), MemberType = typeof(GenerateScriptTests))]
4343
public void VerifyGenerateScript(string package, string version, PackageType type)
4444
{
@@ -77,7 +77,7 @@ public void VerifyGenerateScript(string package, string version, PackageType typ
7777
}
7878
}
7979

80-
(Process Process, string StdOut, string StdErr) result =
80+
(Process Process, string StdOut, string StdErr) result =
8181
ExecuteHelper.ExecuteProcess("git", $"diff --no-index {pkgSrcDirectory} {pkgSandboxDirectory}", Output, true);
8282

8383
string diff = result.StdOut;

tests/SbrpTests/PathUtilities.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ namespace SbrpTests;
99

1010
internal static class PathUtilities
1111
{
12-
public static string GetRepoRoot () =>
12+
public static string GetRepoRoot() =>
1313
(string)AppContext.GetData("SbrpTests.RepoRoot")!;
1414

15-
public static string GetSourceBuildRepoRoot ()
15+
public static string GetSourceBuildRepoRoot()
1616
{
17-
var artifactsDir = (string)AppContext.GetData ("SbrpTests.ArtifactsDir")!;
17+
var artifactsDir = (string)AppContext.GetData("SbrpTests.ArtifactsDir")!;
1818
return Path.Combine(artifactsDir, "sb", "src");
1919
}
20-
21-
public static string GetSourceBuildPackagesShippingDir ()
20+
21+
public static string GetSourceBuildPackagesShippingDir()
2222
{
23-
var configuration = (string)AppContext.GetData ("SbrpTests.Configuration")!;
23+
var configuration = (string)AppContext.GetData("SbrpTests.Configuration")!;
2424
return Path.Combine(GetSourceBuildRepoRoot(), "artifacts", "packages", configuration, "Shipping");
2525
}
2626
}

tests/SbrpTests/ValidationTests.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ public ValidationTests(ITestOutputHelper output)
3535

3636
[SkippableFact]
3737
public void ValidateSbrpAttribute()
38-
{
38+
{
3939
string[] packages = GetPackages();
4040

41-
HashSet<string> targetAndTextOnlyPacks = new (
41+
HashSet<string> targetAndTextOnlyPacks = new(
4242
Directory.GetDirectories(Path.Combine(PathUtilities.GetSourceBuildRepoRoot(), "src/targetPacks/ILsrc"))
4343
.Union(Directory.GetDirectories(Path.Combine(PathUtilities.GetSourceBuildRepoRoot(), "src/textOnlyPackages/src")))
4444
.Select(x => Path.GetFileName(x).ToLower())
@@ -66,8 +66,8 @@ public void ValidateSbrpAttribute()
6666

6767
foreach (var dll in dlls)
6868
{
69-
using FileStream stream = new (dll, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
70-
using PEReader peReader = new (stream);
69+
using FileStream stream = new(dll, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
70+
using PEReader peReader = new(stream);
7171
MetadataReader reader = peReader.GetMetadataReader();
7272

7373
Assert.True(HasSbrpAttribute(reader), $"{package}/{Path.GetRelativePath(tempDirectory, dll)} does not contain the {SbrpAttributeType} attribute with key='source' and value='{SbrpRepoIdentifier}'.");
@@ -86,7 +86,7 @@ public async Task ValidateSignatures()
8686
string[] packages = GetPackages();
8787

8888
ISignatureVerificationProvider[] trustProviders = [new SignatureTrustAndValidityVerificationProvider()];
89-
PackageSignatureVerifier verifier = new (trustProviders);
89+
PackageSignatureVerifier verifier = new(trustProviders);
9090
var settings = SignedPackageVerifierSettings.GetDefault();
9191

9292
Output.WriteLine($"Checking {packages.Count()} packages for signatures.");
@@ -159,7 +159,7 @@ private static bool IsAttributeSbrp(MetadataReader reader, CustomAttribute attr)
159159

160160
private static async Task<bool> IsPackageSignedAsync(string packagePath, PackageSignatureVerifier verifier, SignedPackageVerifierSettings settings)
161161
{
162-
using PackageArchiveReader packageReader = new (packagePath);
162+
using PackageArchiveReader packageReader = new(packagePath);
163163
var result = await verifier.VerifySignaturesAsync(packageReader, settings, CancellationToken.None);
164164
return result.IsSigned;
165165
}

0 commit comments

Comments
 (0)