From 1f3cae0094ee071adb53dab3d89fa7e8adf2a3d0 Mon Sep 17 00:00:00 2001 From: miko Date: Fri, 31 May 2024 20:36:07 +0800 Subject: [PATCH] Fix code style and naming --- .../CollectionConverterContext.Static.cs | 72 +++++++++---------- .../GenericConverterContext.Static.cs | 32 ++++----- .../SourceGenerator.cs | 8 +-- .../Symbols.Validation.cs | 10 +-- code/Binary.SourceGeneration/Symbols.cs | 6 +- 5 files changed, 64 insertions(+), 64 deletions(-) diff --git a/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.Static.cs b/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.Static.cs index b35b7469..712d1a6c 100644 --- a/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.Static.cs +++ b/code/Binary.SourceGeneration/Contexts/CollectionConverterContext.Static.cs @@ -48,58 +48,58 @@ private class Resource(ImmutableDictionary suppo private static ImmutableDictionary CreateResourceForSupportedTypes(Compilation compilation) { - static void Register(Compilation compilation, ImmutableDictionary.Builder builder, string name, ConstructorArgumentKind source, string method) + static void Register(Compilation compilation, ImmutableDictionary.Builder result, string name, ConstructorArgumentKind source, string method) { if (compilation.GetTypeByMetadataName(name) is not { } type) return; - builder.Add(type.ConstructUnboundGenericType(), new TypeBaseInfo(source, method)); + result.Add(type.ConstructUnboundGenericType(), new TypeBaseInfo(source, method)); } - var builder = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); - - Register(compilation, builder, "System.Collections.Frozen.FrozenSet`1", ConstructorArgumentKind.List, $"System.Collections.Frozen.FrozenSet.ToFrozenSet({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Frozen.FrozenDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Generic.IList`1", ConstructorArgumentKind.List, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.ICollection`1", ConstructorArgumentKind.List, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.IEnumerable`1", ConstructorArgumentKind.List, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.IReadOnlyList`1", ConstructorArgumentKind.List, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.IReadOnlyCollection`1", ConstructorArgumentKind.List, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.ISet`1", ConstructorArgumentKind.HashSet, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.IReadOnlySet`1", ConstructorArgumentKind.HashSet, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.IDictionary`2", ConstructorArgumentKind.Dictionary, ConstructorArgument); - Register(compilation, builder, "System.Collections.Generic.IReadOnlyDictionary`2", ConstructorArgumentKind.Dictionary, ConstructorArgument); - Register(compilation, builder, "System.Collections.Immutable.IImmutableDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Immutable.ImmutableDictionary.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.IImmutableList`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableList.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.IImmutableQueue`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableQueue.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.IImmutableSet`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableHashSet.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Immutable.ImmutableDictionary.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableHashSet`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableHashSet.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableList`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableList.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableQueue`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableQueue.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableSortedDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Immutable.ImmutableSortedDictionary.CreateRange({ConstructorArgument})"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableSortedSet`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableSortedSet.CreateRange({ConstructorArgument})"); - - return builder.ToImmutable(); + var result = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); + + Register(compilation, result, "System.Collections.Frozen.FrozenSet`1", ConstructorArgumentKind.List, $"System.Collections.Frozen.FrozenSet.ToFrozenSet({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Frozen.FrozenDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Frozen.FrozenDictionary.ToFrozenDictionary({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Generic.IList`1", ConstructorArgumentKind.List, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.ICollection`1", ConstructorArgumentKind.List, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.IEnumerable`1", ConstructorArgumentKind.List, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.IReadOnlyList`1", ConstructorArgumentKind.List, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.IReadOnlyCollection`1", ConstructorArgumentKind.List, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.ISet`1", ConstructorArgumentKind.HashSet, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.IReadOnlySet`1", ConstructorArgumentKind.HashSet, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.IDictionary`2", ConstructorArgumentKind.Dictionary, ConstructorArgument); + Register(compilation, result, "System.Collections.Generic.IReadOnlyDictionary`2", ConstructorArgumentKind.Dictionary, ConstructorArgument); + Register(compilation, result, "System.Collections.Immutable.IImmutableDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Immutable.ImmutableDictionary.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.IImmutableList`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableList.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.IImmutableQueue`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableQueue.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.IImmutableSet`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableHashSet.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.ImmutableDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Immutable.ImmutableDictionary.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.ImmutableHashSet`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableHashSet.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.ImmutableList`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableList.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.ImmutableQueue`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableQueue.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.ImmutableSortedDictionary`2", ConstructorArgumentKind.ListKeyValuePair, $"System.Collections.Immutable.ImmutableSortedDictionary.CreateRange({ConstructorArgument})"); + Register(compilation, result, "System.Collections.Immutable.ImmutableSortedSet`1", ConstructorArgumentKind.List, $"System.Collections.Immutable.ImmutableSortedSet.CreateRange({ConstructorArgument})"); + + return result.ToImmutable(); } private static ImmutableHashSet CreateResourceForUnsupportedTypes(Compilation compilation) { - static void Register(Compilation compilation, ImmutableHashSet.Builder builder, string name) + static void Register(Compilation compilation, ImmutableHashSet.Builder result, string name) { if (compilation.GetTypeByMetadataName(name) is not { } type) return; - _ = builder.Add(type.IsGenericType ? type.ConstructUnboundGenericType() : type); + _ = result.Add(type.IsGenericType ? type.ConstructUnboundGenericType() : type); } - var builder = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); + var result = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); - Register(compilation, builder, "System.String"); - Register(compilation, builder, "System.Collections.Generic.Stack`1"); - Register(compilation, builder, "System.Collections.Concurrent.ConcurrentStack`1"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableStack`1"); - Register(compilation, builder, "System.Collections.Immutable.IImmutableStack`1"); + Register(compilation, result, "System.String"); + Register(compilation, result, "System.Collections.Generic.Stack`1"); + Register(compilation, result, "System.Collections.Concurrent.ConcurrentStack`1"); + Register(compilation, result, "System.Collections.Immutable.ImmutableStack`1"); + Register(compilation, result, "System.Collections.Immutable.IImmutableStack`1"); - return builder.ToImmutable(); + return result.ToImmutable(); } private static Resource CreateResource(Compilation compilation) diff --git a/code/Binary.SourceGeneration/Contexts/GenericConverterContext.Static.cs b/code/Binary.SourceGeneration/Contexts/GenericConverterContext.Static.cs index 933cbaa7..b30e65df 100644 --- a/code/Binary.SourceGeneration/Contexts/GenericConverterContext.Static.cs +++ b/code/Binary.SourceGeneration/Contexts/GenericConverterContext.Static.cs @@ -23,29 +23,29 @@ private class TypeInfo(string name, TypeArgumentsOption option, ImmutableArray CreateResource(Compilation compilation) { - static void Register(Compilation compilation, ImmutableHashSet.Builder builder, string name) + static void Register(Compilation compilation, ImmutableHashSet.Builder result, string name) { if (compilation.GetTypeByMetadataName(name)?.ConstructUnboundGenericType() is not { } type) return; - _ = builder.Add(type); + _ = result.Add(type); } - var builder = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); + var result = ImmutableHashSet.CreateBuilder(SymbolEqualityComparer.Default); - Register(compilation, builder, "System.ArraySegment`1"); - Register(compilation, builder, "System.Memory`1"); - Register(compilation, builder, "System.Nullable`1"); - Register(compilation, builder, "System.ReadOnlyMemory`1"); - Register(compilation, builder, "System.Buffers.ReadOnlySequence`1"); - Register(compilation, builder, "System.Collections.Generic.List`1"); - Register(compilation, builder, "System.Collections.Generic.Dictionary`2"); - Register(compilation, builder, "System.Collections.Generic.HashSet`1"); - Register(compilation, builder, "System.Collections.Generic.KeyValuePair`2"); - Register(compilation, builder, "System.Collections.Generic.LinkedList`1"); - Register(compilation, builder, "System.Collections.Generic.PriorityQueue`2"); - Register(compilation, builder, "System.Collections.Immutable.ImmutableArray`1"); + Register(compilation, result, "System.ArraySegment`1"); + Register(compilation, result, "System.Memory`1"); + Register(compilation, result, "System.Nullable`1"); + Register(compilation, result, "System.ReadOnlyMemory`1"); + Register(compilation, result, "System.Buffers.ReadOnlySequence`1"); + Register(compilation, result, "System.Collections.Generic.List`1"); + Register(compilation, result, "System.Collections.Generic.Dictionary`2"); + Register(compilation, result, "System.Collections.Generic.HashSet`1"); + Register(compilation, result, "System.Collections.Generic.KeyValuePair`2"); + Register(compilation, result, "System.Collections.Generic.LinkedList`1"); + Register(compilation, result, "System.Collections.Generic.PriorityQueue`2"); + Register(compilation, result, "System.Collections.Immutable.ImmutableArray`1"); - return builder.ToImmutable(); + return result.ToImmutable(); } private static TypeInfo? GetInfo(SourceGeneratorContext context, ITypeSymbol type) diff --git a/code/Binary.SourceGeneration/SourceGenerator.cs b/code/Binary.SourceGeneration/SourceGenerator.cs index 28376648..318c635c 100644 --- a/code/Binary.SourceGeneration/SourceGenerator.cs +++ b/code/Binary.SourceGeneration/SourceGenerator.cs @@ -96,7 +96,7 @@ private static void Invoke(Compilation compilation, SourceProductionContext prod private static ImmutableDictionary GetInclusions(SourceGeneratorContext context, INamedTypeSymbol type, INamedTypeSymbol? include) { - var builder = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); + var result = ImmutableDictionary.CreateBuilder(SymbolEqualityComparer.Default); var attributes = type.GetAttributes(); var cancellation = context.CancellationToken; foreach (var attribute in attributes) @@ -109,11 +109,11 @@ private static ImmutableDictionary GetInclusions(Sou if (SymbolEqualityComparer.Default.Equals(definitions, include) is false) continue; var includedType = attributeClass.TypeArguments.Single(); - if (Symbols.ValidateIncludeType(context, builder, attribute, includedType) is false) + if (Symbols.ValidateIncludeType(context, result, attribute, includedType) is false) continue; - builder.Add(includedType, attribute); + result.Add(includedType, attribute); } - return builder.ToImmutable(); + return result.ToImmutable(); } private static string Invoke(SourceGeneratorContext context, ContextInfo info) diff --git a/code/Binary.SourceGeneration/Symbols.Validation.cs b/code/Binary.SourceGeneration/Symbols.Validation.cs index 4e0b7eaa..10b35cad 100644 --- a/code/Binary.SourceGeneration/Symbols.Validation.cs +++ b/code/Binary.SourceGeneration/Symbols.Validation.cs @@ -42,19 +42,19 @@ public static SymbolTypeKind ValidateType(SourceGeneratorContext context, ITypeS var diagnostics = new List(); var attributes = new[] { converterAttribute, converterCreatorAttribute, namedObjectAttribute, tupleObjectAttribute } .OfType() - .ToImmutableArray(); + .ToList(); ValidateConverterAttribute(context, converterAttribute, diagnostics); ValidateConverterCreatorAttribute(context, converterCreatorAttribute, diagnostics); cancellation.ThrowIfCancellationRequested(); - if (attributes.Length > 1) - diagnostics.Add(Constants.MultipleAttributesFoundOnType.With(symbol, [symbolDisplay])); - else + if (attributes.Count is 0 or 1) ValidateType(context, symbol, symbolDisplay, attributes.SingleOrDefault(), diagnostics); + else + diagnostics.Add(Constants.MultipleAttributesFoundOnType.With(symbol, [symbolDisplay])); if (diagnostics.Count is 0) - return attributes.Length is 0 ? SymbolTypeKind.Native : SymbolTypeKind.Custom; + return attributes.Count is 0 ? SymbolTypeKind.Native : SymbolTypeKind.Custom; foreach (var diagnostic in diagnostics) context.Collect(diagnostic); return SymbolTypeKind.Ignore; diff --git a/code/Binary.SourceGeneration/Symbols.cs b/code/Binary.SourceGeneration/Symbols.cs index 04529971..36984bcc 100644 --- a/code/Binary.SourceGeneration/Symbols.cs +++ b/code/Binary.SourceGeneration/Symbols.cs @@ -187,7 +187,7 @@ public static bool IsTypeInvalid(ITypeSymbol symbol) public static ImmutableArray FilterFieldsAndProperties(ImmutableArray members, CancellationToken cancellation) { - var builder = ImmutableArray.CreateBuilder(); + var result = ImmutableArray.CreateBuilder(); foreach (var member in members) { cancellation.ThrowIfCancellationRequested(); @@ -204,9 +204,9 @@ public static ImmutableArray FilterFieldsAndProperties(ImmutableArray