Skip to content

Commit

Permalink
Fix code style and naming
Browse files Browse the repository at this point in the history
  • Loading branch information
afxres committed May 31, 2024
1 parent 0c1baca commit 1f3cae0
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,58 +48,58 @@ private class Resource(ImmutableDictionary<INamedTypeSymbol, TypeBaseInfo> suppo

private static ImmutableDictionary<INamedTypeSymbol, TypeBaseInfo> CreateResourceForSupportedTypes(Compilation compilation)
{
static void Register(Compilation compilation, ImmutableDictionary<INamedTypeSymbol, TypeBaseInfo>.Builder builder, string name, ConstructorArgumentKind source, string method)
static void Register(Compilation compilation, ImmutableDictionary<INamedTypeSymbol, TypeBaseInfo>.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<INamedTypeSymbol, TypeBaseInfo>(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<INamedTypeSymbol, TypeBaseInfo>(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<INamedTypeSymbol> CreateResourceForUnsupportedTypes(Compilation compilation)
{
static void Register(Compilation compilation, ImmutableHashSet<INamedTypeSymbol>.Builder builder, string name)
static void Register(Compilation compilation, ImmutableHashSet<INamedTypeSymbol>.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<INamedTypeSymbol>(SymbolEqualityComparer.Default);
var result = ImmutableHashSet.CreateBuilder<INamedTypeSymbol>(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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ private class TypeInfo(string name, TypeArgumentsOption option, ImmutableArray<I

private static ImmutableHashSet<INamedTypeSymbol> CreateResource(Compilation compilation)
{
static void Register(Compilation compilation, ImmutableHashSet<INamedTypeSymbol>.Builder builder, string name)
static void Register(Compilation compilation, ImmutableHashSet<INamedTypeSymbol>.Builder result, string name)
{
if (compilation.GetTypeByMetadataName(name)?.ConstructUnboundGenericType() is not { } type)
return;
_ = builder.Add(type);
_ = result.Add(type);
}

var builder = ImmutableHashSet.CreateBuilder<INamedTypeSymbol>(SymbolEqualityComparer.Default);
var result = ImmutableHashSet.CreateBuilder<INamedTypeSymbol>(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)
Expand Down
8 changes: 4 additions & 4 deletions code/Binary.SourceGeneration/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private static void Invoke(Compilation compilation, SourceProductionContext prod

private static ImmutableDictionary<ITypeSymbol, AttributeData> GetInclusions(SourceGeneratorContext context, INamedTypeSymbol type, INamedTypeSymbol? include)
{
var builder = ImmutableDictionary.CreateBuilder<ITypeSymbol, AttributeData>(SymbolEqualityComparer.Default);
var result = ImmutableDictionary.CreateBuilder<ITypeSymbol, AttributeData>(SymbolEqualityComparer.Default);
var attributes = type.GetAttributes();
var cancellation = context.CancellationToken;
foreach (var attribute in attributes)
Expand All @@ -109,11 +109,11 @@ private static ImmutableDictionary<ITypeSymbol, AttributeData> 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)
Expand Down
10 changes: 5 additions & 5 deletions code/Binary.SourceGeneration/Symbols.Validation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ public static SymbolTypeKind ValidateType(SourceGeneratorContext context, ITypeS
var diagnostics = new List<Diagnostic>();
var attributes = new[] { converterAttribute, converterCreatorAttribute, namedObjectAttribute, tupleObjectAttribute }
.OfType<AttributeData>()
.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;
Expand Down
6 changes: 3 additions & 3 deletions code/Binary.SourceGeneration/Symbols.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public static bool IsTypeInvalid(ITypeSymbol symbol)

public static ImmutableArray<ISymbol> FilterFieldsAndProperties(ImmutableArray<ISymbol> members, CancellationToken cancellation)
{
var builder = ImmutableArray.CreateBuilder<ISymbol>();
var result = ImmutableArray.CreateBuilder<ISymbol>();
foreach (var member in members)
{
cancellation.ThrowIfCancellationRequested();
Expand All @@ -204,9 +204,9 @@ public static ImmutableArray<ISymbol> FilterFieldsAndProperties(ImmutableArray<I
continue;
if (IsTypeInvalid(memberType))
continue;
builder.Add(member);
result.Add(member);
}
return builder.ToImmutable();
return result.ToImmutable();
}

public static int CompareInheritance(Compilation compilation, ITypeSymbol x, ITypeSymbol y)
Expand Down

0 comments on commit 1f3cae0

Please sign in to comment.