Skip to content

Commit

Permalink
chore: code-rub
Browse files Browse the repository at this point in the history
  • Loading branch information
earloc committed Oct 26, 2024
1 parent fa93b59 commit bc66ce0
Show file tree
Hide file tree
Showing 46 changed files with 266 additions and 382 deletions.
82 changes: 81 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,84 @@ dotnet_diagnostic.CA1707.severity = none
dotnet_diagnostic.CA2007.severity = none

# CA1852: Type 'Program' can be sealed because it has no subtypes in its containing assembly and is not externally visible
dotnet_diagnostic.CA1852.severity = none
dotnet_diagnostic.CA1852.severity = none
csharp_indent_labels = one_less_than_current
csharp_using_directive_placement = outside_namespace:silent
csharp_prefer_simple_using_statement = true:warning
csharp_prefer_braces = true:warning
csharp_style_namespace_declarations = file_scoped:warning
csharp_style_prefer_method_group_conversion = true:warning
csharp_style_prefer_top_level_statements = true:silent
csharp_style_prefer_primary_constructors = true:suggestion
csharp_style_expression_bodied_methods = true:silent
csharp_style_expression_bodied_constructors = true:silent
csharp_style_expression_bodied_operators = true:silent
csharp_style_expression_bodied_properties = true:silent
csharp_style_expression_bodied_indexers = true:silent
csharp_style_expression_bodied_accessors = true:silent
csharp_style_expression_bodied_lambdas = true:silent
csharp_style_expression_bodied_local_functions = true:silent
csharp_style_inlined_variable_declaration = true:suggestion
csharp_style_deconstructed_variable_declaration = true:suggestion
csharp_style_var_for_built_in_types = true:silent
csharp_style_var_when_type_is_apparent = true:silent
csharp_style_var_elsewhere = true:silent
csharp_space_around_binary_operators = before_and_after

[*.{cs,vb}]
#### Naming styles ####

# Naming rules

dotnet_naming_rule.interface_should_be_begins_with_i.severity = suggestion
dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i

dotnet_naming_rule.types_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.types_should_be_pascal_case.symbols = types
dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case

dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = suggestion
dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case

# Symbol specifications

dotnet_naming_symbols.interface.applicable_kinds = interface
dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.interface.required_modifiers =

dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.types.required_modifiers =

dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
dotnet_naming_symbols.non_field_members.required_modifiers =

# Naming styles

dotnet_naming_style.begins_with_i.required_prefix = I
dotnet_naming_style.begins_with_i.required_suffix =
dotnet_naming_style.begins_with_i.word_separator =
dotnet_naming_style.begins_with_i.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case

dotnet_naming_style.pascal_case.required_prefix =
dotnet_naming_style.pascal_case.required_suffix =
dotnet_naming_style.pascal_case.word_separator =
dotnet_naming_style.pascal_case.capitalization = pascal_case
dotnet_style_operator_placement_when_wrapping = beginning_of_line
dotnet_style_coalesce_expression = true:suggestion
dotnet_style_null_propagation = true:suggestion
dotnet_style_prefer_is_null_check_over_reference_equality_method = true:warning
dotnet_style_prefer_auto_properties = true:silent
dotnet_style_object_initializer = true:suggestion
dotnet_style_collection_initializer = true:suggestion
dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
dotnet_style_prefer_conditional_expression_over_assignment = true:silent
dotnet_style_prefer_conditional_expression_over_return = true:silent
6 changes: 2 additions & 4 deletions src/Playground.Common/Greeter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ public Greeter(IStringLocalizer<Greetings> localize, IStringLocalizer<Duplicate.
this.publicDuplicate = publicDuplicate;
}

public void SayHello(string name) => Console.WriteLine(localize.Hello__name(name));

public void SayHello(string name) => Console.WriteLine(localize.Hello__name(name));

public void SayHelloPublic(string name) => Console.WriteLine(publicDuplicate.Hello__name(name));


public void GoodMorning(string name) => Console.WriteLine(duplicates.GoodMorning__name(name));

}
3 changes: 2 additions & 1 deletion src/Playground.Console/IStringLocalizerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ namespace Playground.Console;
internal static class IStringLocalizerExtensions
{
public static LocalizedString Bar(this IStringLocalizer that) => that["Bar"];

public static LocalizedString Bar(this IStringLocalizer that, string value) => that["Bar", value];

public static LocalizedString Foo(this IStringLocalizer that, string value) => that["Foo {bar}", value];

public static LocalizedString FooBar(this IStringLocalizer that, string value) => that["FooBar {0}", value];

public static LocalizedString Bars(this IStringLocalizer that, string v1, int v2) => that["Bar {0} {1}", v1, v2];

}
5 changes: 1 addition & 4 deletions src/TypealizR.CLI/Abstractions/FileStorage.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
namespace TypealizR.CLI.Abstractions;
internal class FileStorage : IStorage
{
public async Task AddAsync(string fileName, string content)
{
await File.WriteAllTextAsync(fileName, content);
}
public async Task AddAsync(string fileName, string content) => await File.WriteAllTextAsync(fileName, content);
}
8 changes: 4 additions & 4 deletions src/TypealizR.CLI/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public App(Action<IServiceCollection>? configureServices = null, params string[]

codeFirstCommand.AddAlias("cf");

var rootCommand = new RootCommand()
{
RootCommand rootCommand =
[
codeFirstCommand
};
];

runner = new CommandLineBuilder(rootCommand)
.UseDefaults()
Expand All @@ -57,5 +57,5 @@ private void ConfigureServices(IServiceCollection services)
services.TryAddSingleton<IStorage, FileStorage>();
}

public Task<int> RunAsync() => runner.InvokeAsync(args);
public Task<int> RunAsync() => runner.InvokeAsync(args);
}
73 changes: 33 additions & 40 deletions src/TypealizR.CLI/Commands/CodeFirst/ExportCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public Implementation(IConsole console, IStorage storage)
this.storage = storage;
}

public int Invoke(InvocationContext context)
{
throw new NotImplementedException();
}
public int Invoke(InvocationContext context) => throw new NotImplementedException();

public async Task<int> InvokeAsync(InvocationContext context)
{
Expand Down Expand Up @@ -130,25 +127,23 @@ private static async Task ExportAsync(IConsole console, string baseDirectory, IE
}
}

private static IEnumerable<BaseNamespaceDeclarationSyntax> FindNamespaces(Compilation compilation, CancellationToken cancellationToken)
=> compilation.SyntaxTrees
.Where(x => x.GetRoot() is CompilationUnitSyntax)
.Select(x => (CompilationUnitSyntax)x.GetRoot(cancellationToken))
.SelectMany(x => x.Members.OfType<BaseNamespaceDeclarationSyntax>())
;

private static IEnumerable<InterfaceInfo> FindInterfaces(Compilation compilation, IEnumerable<BaseNamespaceDeclarationSyntax> allNamespaces, CancellationToken cancellationToken)
=> allNamespaces
.SelectMany(x => x.Members.OfType<InterfaceDeclarationSyntax>())
.Select(x => new { Declaration = x, Model = compilation.GetSemanticModel(x.SyntaxTree) })
.Select(x => new { x.Declaration, Symbol = x.Model.GetDeclaredSymbol(x.Declaration, cancellationToken) })
.Where(x => x.Symbol is not null)
.Select(x => new InterfaceInfo(x.Declaration, x.Symbol!))
.Where(x => x.Symbol
.GetAttributes()
.Any(x => x.AttributeClass is not null && x.AttributeClass!.Name.StartsWith(MarkerAttributeName, StringComparison.Ordinal))
)
;
private static IEnumerable<BaseNamespaceDeclarationSyntax> FindNamespaces(Compilation compilation, CancellationToken cancellationToken) => compilation.SyntaxTrees
.Where(x => x.GetRoot() is CompilationUnitSyntax)
.Select(x => (CompilationUnitSyntax)x.GetRoot(cancellationToken))
.SelectMany(x => x.Members.OfType<BaseNamespaceDeclarationSyntax>())
;

private static IEnumerable<InterfaceInfo> FindInterfaces(Compilation compilation, IEnumerable<BaseNamespaceDeclarationSyntax> allNamespaces, CancellationToken cancellationToken) => allNamespaces
.SelectMany(x => x.Members.OfType<InterfaceDeclarationSyntax>())
.Select(x => new { Declaration = x, Model = compilation.GetSemanticModel(x.SyntaxTree) })
.Select(x => new { x.Declaration, Symbol = x.Model.GetDeclaredSymbol(x.Declaration, cancellationToken) })
.Where(x => x.Symbol is not null)
.Select(x => new InterfaceInfo(x.Declaration, x.Symbol!))
.Where(x => x.Symbol
.GetAttributes()
.Any(x => x.AttributeClass is not null && x.AttributeClass!.Name.StartsWith(MarkerAttributeName, StringComparison.Ordinal))
)
;

private static IEnumerable<TypeInfo> FindClasses(Compilation compilation, IEnumerable<BaseNamespaceDeclarationSyntax> allNamespaces, IEnumerable<InterfaceInfo> markedInterfacesIdentifier, CancellationToken cancellationToken)
{
Expand All @@ -172,23 +167,21 @@ private static IEnumerable<TypeInfo> FindClasses(Compilation compilation, IEnume
;
}

private static VariableDeclaratorSyntax? FindKeyOf(TypeInfo type, PropertyDeclarationSyntax propertySyntax)
=> type.Declaration.Members
.OfType<FieldDeclarationSyntax>()
.Where(x => x.Modifiers.Any(y => y.Text == "const"))
.Select(x => x.Declaration.Variables.SingleOrDefault())
.Where(x => x is not null).Select(x => x!)
.FirstOrDefault(x => x.Identifier.Text == $"{propertySyntax.Identifier.Text}{TypealizR._.FallBackKeySuffix}")
;

private static VariableDeclaratorSyntax? FindKeyOf(TypeInfo type, MethodDeclarationSyntax methodSyntax)
=> type.Declaration.Members
.OfType<FieldDeclarationSyntax>()
.Where(x => x.Modifiers.Any(y => y.Text == "const"))
.Select(x => x.Declaration.Variables.SingleOrDefault())
.Where(x => x is not null).Select(x => x!)
.FirstOrDefault(x => x.Identifier.Text == $"{methodSyntax.Identifier.Text}{TypealizR._.FallBackKeySuffix}")
;
private static VariableDeclaratorSyntax? FindKeyOf(TypeInfo type, PropertyDeclarationSyntax propertySyntax) => type.Declaration.Members
.OfType<FieldDeclarationSyntax>()
.Where(x => x.Modifiers.Any(y => y.Text == "const"))
.Select(x => x.Declaration.Variables.SingleOrDefault())
.Where(x => x is not null).Select(x => x!)
.FirstOrDefault(x => x.Identifier.Text == $"{propertySyntax.Identifier.Text}{TypealizR._.FallBackKeySuffix}")
;

private static VariableDeclaratorSyntax? FindKeyOf(TypeInfo type, MethodDeclarationSyntax methodSyntax) => type.Declaration.Members
.OfType<FieldDeclarationSyntax>()
.Where(x => x.Modifiers.Any(y => y.Text == "const"))
.Select(x => x.Declaration.Variables.SingleOrDefault())
.Where(x => x is not null).Select(x => x!)
.FirstOrDefault(x => x.Identifier.Text == $"{methodSyntax.Identifier.Text}{TypealizR._.FallBackKeySuffix}")
;

private static void AddProperty(IConsole console, TypeInfo type, ResxBuilder builder, PropertyDeclarationSyntax property)
{
Expand Down
3 changes: 1 addition & 2 deletions src/TypealizR.CLI/Extensions/ExpressionSyntaxExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Microsoft.CodeAnalysis.CSharp.Syntax;
internal static class ExpressionSyntaxExtensions
{
public static string ToResourceKey(this ExpressionSyntax that)
=> that.ToString().Trim('@', '$', '"');
public static string ToResourceKey(this ExpressionSyntax that) => that.ToString().Trim('@', '$', '"');
}
4 changes: 2 additions & 2 deletions src/TypealizR.Tests/CLI.Tests/ExportCommand.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
namespace TypealizR.Tests.CLI.Tests;
public class ExportCommand_Tests
{
private static string ProjectFile(string x) => $"../../../../{x}/{x}.csproj";

private static string ProjectFile(string x) => $"../../../../{x}/{x}.csproj";

[Fact]
public async Task Export_Generates_ResxFiles()
{
Expand Down
20 changes: 4 additions & 16 deletions src/TypealizR.Tests/CodeFirstSourceGenerator.Tests/.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,35 @@ public class CodeFirstSourceGenerator_Tests
private const string BaseDirectory = "../../../CodeFirstSourceGenerator.Tests";

[Fact]
public async Task Does_Not_Care_About_NonMarked_Interfaces()
{
await GeneratorTesterBuilder<CodeFirstSourceGenerator>
public async Task Does_Not_Care_About_NonMarked_Interfaces() => await GeneratorTesterBuilder<CodeFirstSourceGenerator>
.Create(BaseDirectory, null)
.WithSourceFile("ISomeInterface.cs")
.Build()
.Verify()
;
}


[Fact]
public async Task Uses_Default_Values_From_Member_Names()
{
await GeneratorTesterBuilder<CodeFirstSourceGenerator>
public async Task Uses_Default_Values_From_Member_Names() => await GeneratorTesterBuilder<CodeFirstSourceGenerator>
.Create(BaseDirectory, null)
.WithSourceFile("ITranslatables.cs")
.Build()
.Verify()
;
}

[Fact]
public async Task Honors_Members_With_Simple_Xml_Comment()
{
await GeneratorTesterBuilder<CodeFirstSourceGenerator>
public async Task Honors_Members_With_Simple_Xml_Comment() => await GeneratorTesterBuilder<CodeFirstSourceGenerator>
.Create(BaseDirectory, null)
.WithSourceFile("IMembersWithSimpleXmlComment.cs")
.Build()
.Verify()
;
}

[Fact]
public async Task Honors_Methods_With_Parameters_In_Xml_Comment()
{
await GeneratorTesterBuilder<CodeFirstSourceGenerator>
public async Task Honors_Methods_With_Parameters_In_Xml_Comment() => await GeneratorTesterBuilder<CodeFirstSourceGenerator>
.Create(BaseDirectory, null)
.WithSourceFile("IMethodsWithXmlCommentParameters.cs")
.Build()
.Verify()
;
}
}
4 changes: 1 addition & 3 deletions src/TypealizR.Tests/DiagnosticsFactory.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ namespace TypealizR.Tests;
public class DiagnosticsFactory_Tests
{

private static DiagnosticsFactory CreateSut(DiagnosticsEntry entryid, DiagnosticSeverity severity)
=> new("someFile.resx", "someKey", 42, new Dictionary<string, DiagnosticSeverity>() { { entryid.Id.ToString(), severity } });

private static DiagnosticsFactory CreateSut(DiagnosticsEntry entryid, DiagnosticSeverity severity) => new("someFile.resx", "someKey", 42, new Dictionary<string, DiagnosticSeverity>() { { entryid.Id.ToString(), severity } });

[Theory]
[InlineData(DiagnosticSeverity.Error)]
Expand Down
5 changes: 1 addition & 4 deletions src/TypealizR.Tests/ModuleInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,5 @@ namespace TypealizR.Tests;
public static class ModuleInitializer
{
[ModuleInitializer]
public static void Init()
{
VerifySourceGenerators.Initialize();
}
public static void Init() => VerifySourceGenerators.Initialize();
}
12 changes: 3 additions & 9 deletions src/TypealizR.Tests/RessourceFile.Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,16 @@ internal sealed class EmptyFile : AdditionalText

public override string Path { get; }

public EmptyFile(string path)
{
Path = path;
}
public EmptyFile(string path) => Path = path;

public override SourceText GetText(CancellationToken cancellationToken = new CancellationToken())
{
return SourceText.From(text);
}
public override SourceText GetText(CancellationToken cancellationToken = new CancellationToken()) => SourceText.From(text);
}

public class RessourceFile_Tests
{
private sealed record LineInfo(int LineNumber = 42, int LinePosition = 1337, bool HasLineInfo = true) : IXmlLineInfo
{
bool IXmlLineInfo.HasLineInfo() => HasLineInfo;
bool IXmlLineInfo.HasLineInfo() => HasLineInfo;
}

[Theory]
Expand Down
2 changes: 1 addition & 1 deletion src/TypealizR.Tests/Snapshots/GeneratorTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ public Task Verify([CallerMemberName] string caller = "") => Verifier
.ScrubEmptyLines()
.UseFileName(caller)
.UseDirectory(snapshotDirectory)
;
;
}
3 changes: 1 addition & 2 deletions src/TypealizR.Tests/Snapshots/GeneratorTesterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,7 @@ public IVerifiable Build()

private readonly Dictionary<DiagnosticsId, string> severityConfig = [];

internal GeneratorTesterBuilder<TGenerator> WithSeverityConfig(DiagnosticsId id, DiagnosticSeverity severity)
=> WithSeverityConfig(id, severity.ToString());
internal GeneratorTesterBuilder<TGenerator> WithSeverityConfig(DiagnosticsId id, DiagnosticSeverity severity) => WithSeverityConfig(id, severity.ToString());

internal GeneratorTesterBuilder<TGenerator> WithSeverityConfig(DiagnosticsId id, string severity)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ public GeneratorTesterOptionsProvider(

public override AnalyzerConfigOptions GlobalOptions => globalOptions;

public override AnalyzerConfigOptions GetOptions(SyntaxTree tree)
{
throw new NotImplementedException();
}
public override AnalyzerConfigOptions GetOptions(SyntaxTree tree) => throw new NotImplementedException();

public override AnalyzerConfigOptions GetOptions(AdditionalText textFile)
{
Expand Down
5 changes: 1 addition & 4 deletions src/TypealizR.Tests/Snapshots/ResxFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,5 @@ public ResxFile(string path)
text = File.ReadAllText(path);
}

public override SourceText GetText(CancellationToken cancellationToken = new CancellationToken())
{
return SourceText.From(text);
}
public override SourceText GetText(CancellationToken cancellationToken = new CancellationToken()) => SourceText.From(text);
}
Loading

0 comments on commit bc66ce0

Please sign in to comment.