Skip to content

Commit 455b3a2

Browse files
committed
Reformat & CleanUp
1 parent f82c86c commit 455b3a2

5 files changed

Lines changed: 87 additions & 88 deletions

File tree

src/DynamoDBGenerator.SourceGenerator/Extensions/CompilationExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
using System.Collections.Immutable;
22
using Microsoft.CodeAnalysis;
3-
using Microsoft.CodeAnalysis.CSharp.Syntax;
43

54
namespace DynamoDBGenerator.SourceGenerator.Extensions;
65

76
// Great source: https://github.com/dotnet/runtime/blob/main/src/tools/illink/src/ILLink.RoslynAnalyzer/CompilationExtensions.cs
87
public static class CompilationExtensions
98
{
10-
public static ReadOnlySpan<INamedTypeSymbol> GetTypeSymbols(this Compilation compilation, ImmutableArray<SyntaxNode> classDeclarations)
9+
public static ReadOnlySpan<INamedTypeSymbol> GetTypeSymbols(this Compilation compilation,
10+
ImmutableArray<SyntaxNode> classDeclarations)
1111
{
1212
var span = classDeclarations.AsSpan();
1313
var symbols = new INamedTypeSymbol[classDeclarations.Length];
@@ -19,7 +19,8 @@ public static ReadOnlySpan<INamedTypeSymbol> GetTypeSymbols(this Compilation com
1919
.GetSemanticModel(classDeclarationSyntax.SyntaxTree)
2020
.GetDeclaredSymbol(classDeclarationSyntax)
2121
is not INamedTypeSymbol typeSymbol)
22-
throw new ArgumentException($"Could not convert the '{classDeclarationSyntax.ToFullString()}' into a '{nameof(ITypeSymbol)}'.");
22+
throw new ArgumentException(
23+
$"Could not convert the '{classDeclarationSyntax.ToFullString()}' into a '{nameof(ITypeSymbol)}'.");
2324

2425
symbols[i] = typeSymbol;
2526
}

src/DynamoDBGenerator.SourceGenerator/Extensions/EnumerableExtensions.cs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ public static IEnumerable<T> AllButLast<T>(this IEnumerable<T> enumerable, Func<
1616
buffer = item;
1717
}
1818

19-
if (isBuffered)
20-
{
21-
yield return buffer!;
22-
}
19+
if (isBuffered) yield return buffer!;
2320
}
24-
public static IEnumerable<TResult> AllAndLast<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> @default, Func<T, TResult> onLast)
21+
22+
public static IEnumerable<TResult> AllAndLast<T, TResult>(this IEnumerable<T> enumerable, Func<T, TResult> @default,
23+
Func<T, TResult> onLast)
2524
{
2625
var buffer = default(T);
2726
var isBuffered = false;
@@ -35,10 +34,6 @@ public static IEnumerable<TResult> AllAndLast<T, TResult>(this IEnumerable<T> en
3534
buffer = item;
3635
}
3736

38-
if (isBuffered)
39-
{
40-
yield return onLast(buffer!);
41-
}
37+
if (isBuffered) yield return onLast(buffer!);
4238
}
43-
4439
}
Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using Microsoft.CodeAnalysis;
2+
23
namespace DynamoDBGenerator.SourceGenerator.Extensions;
34

45
public static class NotNullEvaluationExtensions
56
{
6-
public static string NotNullTernaryExpression(this ITypeSymbol typeSymbol, in string accessPattern, in string truthy,
7+
public static string NotNullTernaryExpression(this ITypeSymbol typeSymbol, in string accessPattern,
8+
in string truthy,
79
in string falsy)
810
{
911
var result = Expression(in typeSymbol, in accessPattern) is { } expression
@@ -18,47 +20,51 @@ private static string CreateException(in string accessPattern)
1820
return $"throw {Constants.DynamoDBGenerator.ExceptionHelper.NullExceptionMethod}(nameof({accessPattern}));";
1921
}
2022

21-
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, string truthy)
23+
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern,
24+
string truthy)
2225
{
2326
return NotNullIfStatement(typeSymbol, accessPattern, obj: truthy);
2427
}
25-
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, IEnumerable<string> truthy)
28+
29+
public static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern,
30+
IEnumerable<string> truthy)
2631
{
2732
return NotNullIfStatement(typeSymbol, accessPattern, obj: truthy);
2833
}
29-
34+
3035
private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbol, string accessPattern, object obj)
3136
{
3237
if (Expression(typeSymbol, accessPattern) is not { } expression)
3338
{
34-
if(obj is string single)
35-
yield return single;
36-
else if(obj is IEnumerable<string> truthies)
37-
foreach (var x in truthies)
38-
yield return x;
39-
else
40-
throw new NotImplementedException($"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'");
39+
if (obj is string single)
40+
yield return single;
41+
else if (obj is IEnumerable<string> truthies)
42+
foreach (var x in truthies)
43+
yield return x;
44+
else
45+
throw new NotImplementedException(
46+
$"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'");
4147
}
4248
else
4349
{
44-
45-
var ifClause = obj switch
50+
var ifClause = obj switch
4651
{
47-
string single => $"if ({expression})".CreateScope(single),
48-
IEnumerable<string> multiple => $"if ({expression})".CreateScope(multiple),
49-
_ => throw new NotImplementedException($"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'")
52+
string single => $"if ({expression})".CreateScope(single),
53+
IEnumerable<string> multiple => $"if ({expression})".CreateScope(multiple),
54+
_ => throw new NotImplementedException(
55+
$"Method '{nameof(NotNullIfStatement)}' could not determine type '{obj.GetType().Name}'")
5056
};
5157
var enumerable = typeSymbol.NullableAnnotation switch
5258
{
5359
NullableAnnotation.None or NullableAnnotation.Annotated => ifClause,
54-
NullableAnnotation.NotAnnotated => ifClause.Concat("else".CreateScope(CreateException(in accessPattern))),
60+
NullableAnnotation.NotAnnotated => ifClause.Concat(
61+
"else".CreateScope(CreateException(in accessPattern))),
5562
_ => throw new ArgumentOutOfRangeException(typeSymbol.ToDisplayString())
5663
};
5764

5865
foreach (var element in enumerable)
5966
yield return element;
6067
}
61-
6268
}
6369

6470

@@ -73,7 +79,6 @@ private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbo
7379

7480
static string? OnValueType(in ITypeSymbol typeSymbol, in string accessPattern)
7581
{
76-
7782
if (typeSymbol.TryGetNullableValueType() is not { } namedTypeSymbol)
7883
return null;
7984

@@ -86,6 +91,4 @@ private static IEnumerable<string> NotNullIfStatement(this ITypeSymbol typeSymbo
8691
: $"{accessPattern} is not null && {expression}";
8792
}
8893
}
89-
90-
91-
}
94+
}

src/DynamoDBGenerator.SourceGenerator/Extensions/StringExtensions.cs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,39 @@
1+
using System.Runtime.CompilerServices;
2+
13
namespace DynamoDBGenerator.SourceGenerator.Extensions;
24

35
public static class StringExtensions
46
{
5-
public static string ToCamelCaseFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
7+
public static string ToCamelCaseFromPascal(this string str, [CallerMemberName] string? memberName = null)
68
{
79
return ToCamelCaseFromPascal(str.AsSpan(), memberName).ToString();
810
}
911

10-
public static string ToPrivateFieldFromPascal(this string str, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
12+
public static string ToPrivateFieldFromPascal(this string str, [CallerMemberName] string? memberName = null)
1113
{
1214
return ToPrivateFieldFromPascal(str.AsSpan(), memberName).ToString();
1315
}
1416

15-
public static ReadOnlySpan<char> ToPrivateFieldFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
17+
public static ReadOnlySpan<char> ToPrivateFieldFromPascal(this ReadOnlySpan<char> span,
18+
[CallerMemberName] string? memberName = null)
1619
{
1720
if (span.Length is 0)
1821
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");
1922

2023
var array = new char[span.Length + 1];
2124

2225
array[0] = '_';
23-
array[1] = Char.ToLowerInvariant(span[0]);
26+
array[1] = char.ToLowerInvariant(span[0]);
2427

2528
// Skip first element since we handled it manually.
2629
for (var i = 1; i < span.Length; i++)
2730
array[i + 1] = span[i];
2831

2932
return array;
3033
}
31-
public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> span, [System.Runtime.CompilerServices.CallerMemberName] string? memberName = null)
34+
35+
public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> span,
36+
[CallerMemberName] string? memberName = null)
3237
{
3338
if (span.Length is 0)
3439
throw new ArgumentException($"Null or Empty string was provided from '{memberName}'");
@@ -38,7 +43,7 @@ public static ReadOnlySpan<char> ToCamelCaseFromPascal(this ReadOnlySpan<char> s
3843

3944
var array = new char[span.Length];
4045

41-
array[0] = Char.ToLowerInvariant(span[0]);
46+
array[0] = char.ToLowerInvariant(span[0]);
4247

4348
// Skip first element since we handled it manually.
4449
for (var i = 1; i < span.Length; i++)
@@ -92,12 +97,9 @@ public static string ToAlphaNumericMethodName(this string txt)
9297
for (var i = 0; i < txt.Length; i++)
9398
{
9499
var c = txt[i];
95-
if (char.IsLetter(c) || index > 0 && char.IsNumber(c))
96-
{
97-
arr[index++] = c;
98-
}
100+
if (char.IsLetter(c) || (index > 0 && char.IsNumber(c))) arr[index++] = c;
99101
}
100102

101103
return new string(arr, 0, index);
102104
}
103-
}
105+
}

src/DynamoDBGenerator.SourceGenerator/Extensions/TypeExtensions.cs

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ namespace DynamoDBGenerator.SourceGenerator.Extensions;
66

77
public static class TypeExtensions
88
{
9+
private static readonly Dictionary<ITypeSymbol, TypeIdentifier> TypeIdentifierDictionary =
10+
new(SymbolEqualityComparer.IncludeNullability);
11+
12+
private static readonly SymbolDisplayFormat DisplayFormat = new(
13+
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters
14+
);
15+
916
public static IEnumerable<string> NamespaceDeclaration(this INamedTypeSymbol type, IEnumerable<string> content)
1017
{
1118
return type.ContainingNamespace.IsGlobalNamespace
@@ -48,10 +55,6 @@ public static Func<ITypeSymbol, T> CacheFactory<T>(IEqualityComparer<ISymbol> co
4855
return x => cache.TryGetValue(x, out var value) ? value : cache[x] = selector(x);
4956
}
5057

51-
52-
private static readonly Dictionary<ITypeSymbol, TypeIdentifier> TypeIdentifierDictionary =
53-
new(SymbolEqualityComparer.IncludeNullability);
54-
5558
public static TypeIdentifier TypeIdentifier(this ITypeSymbol type)
5659
{
5760
if (TypeIdentifierDictionary.TryGetValue(type, out var typeIdentifier))
@@ -71,43 +74,12 @@ static TypeIdentifier Create(ITypeSymbol typeSymbol)
7174
}
7275
}
7376

74-
75-
76-
//Source: https://referencesource.microsoft.com/#mscorlib/system/tuple.cs,49b112811bc359fd,references
77-
private class TupleComparer : IEqualityComparer<(ITypeSymbol, ITypeSymbol)>
78-
{
79-
private readonly IEqualityComparer<ITypeSymbol> _comparer;
80-
81-
public TupleComparer(IEqualityComparer<ITypeSymbol> comparer)
82-
{
83-
_comparer = comparer;
84-
}
85-
86-
public bool Equals((ITypeSymbol, ITypeSymbol) x, (ITypeSymbol, ITypeSymbol) y)
87-
{
88-
return _comparer.Equals(x.Item1, y.Item1) && _comparer.Equals(x.Item2, y.Item2);
89-
}
90-
91-
public int GetHashCode((ITypeSymbol, ITypeSymbol) obj)
92-
{
93-
var hashCode1 = obj.Item1 is null ? 0 : _comparer.GetHashCode(obj.Item1);
94-
var hashCode2 = obj.Item2 is null ? 0 : _comparer.GetHashCode(obj.Item2);
95-
return CombineHashCodes(hashCode1, hashCode2);
96-
}
97-
98-
private static int CombineHashCodes(int h1, int h2)
99-
{
100-
return ((h1 << 5) + h1) ^ h2;
101-
}
102-
}
103-
10477
public static Func<ITypeSymbol, ITypeSymbol, string> SuffixedFullyQualifiedTypeName(string suffix,
10578
IEqualityComparer<ISymbol?> comparer)
10679
{
10780
IEqualityComparer<(ITypeSymbol, ITypeSymbol)> tupleComparer = new TupleComparer(comparer);
10881
var dict = new ConcurrentDictionary<(ITypeSymbol, ITypeSymbol), string>(tupleComparer);
10982
if (Equals(comparer, SymbolEqualityComparer.Default))
110-
{
11183
return (x, y) => dict.GetOrAdd(
11284
(x, y),
11385
tuple =>
@@ -117,17 +89,12 @@ public static Func<ITypeSymbol, ITypeSymbol, string> SuffixedFullyQualifiedTypeN
11789
? $"{p.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}.{c.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat)}_{c.BaseType!.ToDisplayString()}{suffix}"
11890
: $"{p.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)}.{c.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat).ToAlphaNumericMethodName()}{suffix}";
11991
});
120-
}
12192

12293
throw new NotSupportedException(
12394
$"Method {nameof(SuffixedFullyQualifiedTypeName)} does not implement equality comparer '{comparer.GetType().Name}"
12495
);
12596
}
12697

127-
private static readonly SymbolDisplayFormat DisplayFormat = new(
128-
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters
129-
);
130-
13198
public static Func<ITypeSymbol, string> SuffixedTypeSymbolNameFactory(string? suffix,
13299
IEqualityComparer<ISymbol?> comparer)
133100
{
@@ -201,8 +168,10 @@ when string.Join("_", namedTypeSymbol.TypeArguments.Select(NullableAnnotation))
201168
_ => throw new NotImplementedException(ExceptionMessage(x))
202169
};
203170

204-
static string ExceptionMessage(ITypeSymbol typeSymbol) =>
205-
$"Could not apply naming suffix on type: {typeSymbol.ToDisplayString()}";
171+
static string ExceptionMessage(ITypeSymbol typeSymbol)
172+
{
173+
return $"Could not apply naming suffix on type: {typeSymbol.ToDisplayString()}";
174+
}
206175
}
207176
}
208177

@@ -282,4 +251,33 @@ static IEnumerable<ISymbol> Iterator(ITypeSymbol typeSymbol)
282251
} while (namedTypeSymbol is { SpecialType: not SpecialType.System_Object });
283252
}
284253
}
254+
255+
256+
//Source: https://referencesource.microsoft.com/#mscorlib/system/tuple.cs,49b112811bc359fd,references
257+
private class TupleComparer : IEqualityComparer<(ITypeSymbol, ITypeSymbol)>
258+
{
259+
private readonly IEqualityComparer<ITypeSymbol> _comparer;
260+
261+
public TupleComparer(IEqualityComparer<ITypeSymbol> comparer)
262+
{
263+
_comparer = comparer;
264+
}
265+
266+
public bool Equals((ITypeSymbol, ITypeSymbol) x, (ITypeSymbol, ITypeSymbol) y)
267+
{
268+
return _comparer.Equals(x.Item1, y.Item1) && _comparer.Equals(x.Item2, y.Item2);
269+
}
270+
271+
public int GetHashCode((ITypeSymbol, ITypeSymbol) obj)
272+
{
273+
var hashCode1 = obj.Item1 is null ? 0 : _comparer.GetHashCode(obj.Item1);
274+
var hashCode2 = obj.Item2 is null ? 0 : _comparer.GetHashCode(obj.Item2);
275+
return CombineHashCodes(hashCode1, hashCode2);
276+
}
277+
278+
private static int CombineHashCodes(int h1, int h2)
279+
{
280+
return ((h1 << 5) + h1) ^ h2;
281+
}
282+
}
285283
}

0 commit comments

Comments
 (0)