Skip to content

Commit

Permalink
Automatically linting code
Browse files Browse the repository at this point in the history
  • Loading branch information
david-driscoll authored and github-actions[bot] committed Feb 2, 2024
1 parent 814a48b commit 17e6931
Show file tree
Hide file tree
Showing 13 changed files with 129 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public partial record Request : IRequest<LaunchRecordModel>
}

/// <summary>
/// The patch request
/// The patch request
/// </summary>
/// <param name="Id">The rocket id</param>
public partial record PatchRequest(LaunchRecordId Id) : IRequest<LaunchRecordModel>, IPropertyTracking<Request>;
Expand Down Expand Up @@ -135,4 +135,4 @@ public async Task<LaunchRecordModel> Handle(Request request, CancellationToken c
return mapper.Map<LaunchRecordModel>(rocket);
}
}
}
}
20 changes: 10 additions & 10 deletions src/Analyzers/GraphqlMutationActionBodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ IParameterSymbol parameter
.FirstOrDefault(
param => param switch
{
IPropertySymbol { IsImplicitlyDeclared: false } ps => SymbolEqualityComparer.Default.Equals(claimsPrincipal, ps.Type),
IFieldSymbol { IsImplicitlyDeclared: false } fs => SymbolEqualityComparer.Default.Equals(claimsPrincipal, fs.Type),
_ => false
IPropertySymbol { IsImplicitlyDeclared: false, } ps => SymbolEqualityComparer.Default.Equals(claimsPrincipal, ps.Type),
IFieldSymbol { IsImplicitlyDeclared: false, } fs => SymbolEqualityComparer.Default.Equals(claimsPrincipal, fs.Type),
_ => false,
}
);
var hasClaimsPrincipal = claimsPrincipalProperty is { };
Expand Down Expand Up @@ -187,7 +187,7 @@ IParameterSymbol parameter

static ExpressionSyntax sendMediatorRequest(ExpressionSyntax nameSyntax, IParameterSymbol? cancellationTokenParameter)
{
var arguments = new List<ArgumentSyntax> { Argument(nameSyntax) };
var arguments = new List<ArgumentSyntax> { Argument(nameSyntax), };
if (cancellationTokenParameter is { })
{
arguments.Add(Argument(IdentifierName(cancellationTokenParameter.Name)));
Expand Down Expand Up @@ -224,7 +224,7 @@ static ExpressionSyntax sendMediatorRequest(ExpressionSyntax nameSyntax, IParame

static ExpressionSyntax streamMediatorRequest(ExpressionSyntax nameSyntax, IParameterSymbol? cancellationTokenParameter)
{
var arguments = new List<ArgumentSyntax> { Argument(nameSyntax) };
var arguments = new List<ArgumentSyntax> { Argument(nameSyntax), };
if (cancellationTokenParameter is { })
{
arguments.Add(Argument(IdentifierName(cancellationTokenParameter.Name)));
Expand All @@ -246,7 +246,7 @@ private void GenerateMethods(
(ClassDeclarationSyntax syntax, INamedTypeSymbol symbol, SemanticModel semanticModel) valueTuple
)
{
var (syntax, symbol, semanticModel) = valueTuple;
( var syntax, var symbol, var semanticModel ) = valueTuple;
var claimsPrincipal = semanticModel.Compilation.GetTypeByMetadataName("System.Security.Claims.ClaimsPrincipal")!;
var mediator = semanticModel.Compilation.GetTypeByMetadataName("MediatR.IMediator")!;
var cancellationToken = semanticModel.Compilation.GetTypeByMetadataName("System.Threading.CancellationToken")!;
Expand All @@ -273,7 +273,7 @@ private void GenerateMethods(
return ( method, symbol: methodSymbol, request: request ?? streamRequest );
}
)
.Where(z => z is { symbol: { }, method: { } })
.Where(z => z is { symbol: { }, method: { }, })
.ToImmutableArray();

var newClass = syntax
Expand All @@ -284,7 +284,7 @@ private void GenerateMethods(
;


foreach (var (method, methodSymbol, request) in members)
foreach (( var method, var methodSymbol, var request ) in members)
{
if (request != null)
{
Expand All @@ -302,7 +302,7 @@ private void GenerateMethods(
}
}

var additionalUsings = new[] { "MediatR" };
var additionalUsings = new[] { "MediatR", };

var usings = syntax
.SyntaxTree.GetCompilationUnitRoot()
Expand Down Expand Up @@ -352,4 +352,4 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

context.RegisterSourceOutput(syntaxProvider, GenerateMethods);
}
}
}
56 changes: 30 additions & 26 deletions src/Analyzers/GraphqlOptionalPropertyTrackingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ INamedTypeSymbol targetSymbol
targetSymbol
.GetMembers()
.OfType<IPropertySymbol>()
.Where(z => z is { IsStatic: false, IsIndexer: false, IsReadOnly: false });
.Where(z => z is { IsStatic: false, IsIndexer: false, IsReadOnly: false, });
if (!targetSymbol.IsRecord)
{
// not able to use with operator, so ignore any init only properties.
writeableProperties = writeableProperties.Where(z => z is { SetMethod.IsInitOnly: false, GetMethod.IsReadOnly: false });
writeableProperties = writeableProperties.Where(z => z is { SetMethod.IsInitOnly: false, GetMethod.IsReadOnly: false, });
}


Expand All @@ -77,33 +77,37 @@ INamedTypeSymbol targetSymbol
var existingMembers = targetSymbol
.GetMembers()
.OfType<IPropertySymbol>()
.Where(z => z is { IsStatic: false, IsIndexer: false, IsReadOnly: false })
.Where(z => z is { IsStatic: false, IsIndexer: false, IsReadOnly: false, })
.Where(z => symbol.GetMembers(z.Name).Any())
.Except(writeableProperties)
.ToArray();

var memberNamesSet = targetSymbol.MemberNames.ToImmutableHashSet(StringComparer.OrdinalIgnoreCase);

var constructor = targetSymbol.Constructors
.Where(z => !z.Parameters.Any(x => SymbolEqualityComparer.Default.Equals(x.Type, targetSymbol)))
.Where(z => !z.IsImplicitlyDeclared)
.OrderByDescending(z => z.Parameters.Length)
.FirstOrDefault();
var constructor = targetSymbol
.Constructors
.Where(z => !z.Parameters.Any(x => SymbolEqualityComparer.Default.Equals(x.Type, targetSymbol)))
.Where(z => !z.IsImplicitlyDeclared)
.OrderByDescending(z => z.Parameters.Length)
.FirstOrDefault();

existingMembers = existingMembers.Except(
existingMembers
.Join(
constructor?.Parameters ?? ImmutableArray<IParameterSymbol>.Empty,
z => z.Name,
z => z.Name,
(a, _) => a,
StringComparer.OrdinalIgnoreCase
)
)
.ToArray();
existingMembers = existingMembers
.Except(
existingMembers
.Join(
constructor?.Parameters ?? ImmutableArray<IParameterSymbol>.Empty,
z => z.Name,
z => z.Name,
(a, _) => a,
StringComparer.OrdinalIgnoreCase
)
)
.ToArray();
var createArgumentList = constructor is null
? ArgumentList()
: ArgumentList(SeparatedList(constructor.Parameters.Select(z => Argument(IdentifierName(memberNamesSet.TryGetValue(z.Name, out var name) ? name : z.Name)))));
: ArgumentList(
SeparatedList(constructor.Parameters.Select(z => Argument(IdentifierName(memberNamesSet.TryGetValue(z.Name, out var name) ? name : z.Name))))
);

var createBody = Block()
.AddStatements(
Expand Down Expand Up @@ -164,12 +168,12 @@ static void AddNamespacesFromPropertyType(HashSet<string> namespaces, ITypeSymbo
ITypeSymbol propertyType;
propertyType = propertySymbol.Type is INamedTypeSymbol
{
Name: "Assigned", ContainingAssembly.Name: "Rocket.Surgery.LaunchPad.Foundation"
Name: "Assigned", ContainingAssembly.Name: "Rocket.Surgery.LaunchPad.Foundation",
} namedTypeSymbol
? namedTypeSymbol.TypeArguments[0]
: propertySymbol.Type;
type = ParseTypeName(propertyType.ToDisplayString(NullableFlowState.MaybeNull, SymbolDisplayFormat.MinimallyQualifiedFormat));
if (propertyType is { TypeKind: TypeKind.Struct or TypeKind.Enum } && type is not NullableTypeSyntax)
if (propertyType is { TypeKind: TypeKind.Struct or TypeKind.Enum, } && type is not NullableTypeSyntax)
{
type = NullableType(type);
}
Expand Down Expand Up @@ -323,7 +327,7 @@ private static MemberDeclarationSyntax[] GenerateTrackingProperties(IPropertySym
}
)
)
)
),
};
}

Expand All @@ -337,7 +341,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
node is (ClassDeclarationSyntax or RecordDeclarationSyntax)
and TypeDeclarationSyntax
{
BaseList: { } baseList
BaseList: { } baseList,
}
&& baseList.Types.Any(
z => z.Type is GenericNameSyntax qns && qns.Identifier.Text.EndsWith("IOptionalTracking", StringComparison.Ordinal)
Expand Down Expand Up @@ -367,12 +371,12 @@ and TypeDeclarationSyntax
);
}
)
.Where(x => x.symbol is not null && x.targetSymbol is not null);
.Where(x => x.symbol is { } && x.targetSymbol is { });

context.RegisterSourceOutput(
values,
// ReSharper disable once NullableWarningSuppressionIsUsed
static (productionContext, tuple) => GeneratePropertyTracking(productionContext, tuple.syntax, tuple.symbol, tuple.targetSymbol!)
);
}
}
}
19 changes: 9 additions & 10 deletions src/Analyzers/PropertyTrackingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ INamedTypeSymbol targetSymbol
.OfType<IPropertySymbol>()
// only works for `set`able properties not init only
.Where(z => !symbol.GetMembers(z.Name).Any())
.Where(z => z is { IsStatic: false, IsIndexer: false, IsReadOnly: false })
.Where(z => z is { IsStatic: false, IsIndexer: false, IsReadOnly: false, })
.ToArray();
if (!targetSymbol.IsRecord)
{
// not able to use with operator, so ignore any init only properties.
writeableProperties = writeableProperties.Where(z => z is { SetMethod.IsInitOnly: false, GetMethod.IsReadOnly: false }).ToArray();
writeableProperties = writeableProperties.Where(z => z is { SetMethod.IsInitOnly: false, GetMethod.IsReadOnly: false, }).ToArray();
}

var changesRecord = RecordDeclaration(Token(SyntaxKind.RecordKeyword), "Changes")
Expand Down Expand Up @@ -110,7 +110,7 @@ static void AddNamespacesFromPropertyType(HashSet<string> namespaces, ITypeSymbo
var type = ParseTypeName(propertySymbol.Type.ToDisplayString(SymbolDisplayFormat.MinimallyQualifiedFormat));
AddNamespacesFromPropertyType(namespaces, propertySymbol.Type);

var assignedType = GenericName(Identifier("Rocket.Surgery.LaunchPad.Foundation.Assigned"))
var assignedType = GenericName(Identifier("Rocket.Surgery.LaunchPad.Foundation.Assigned"))
.WithTypeArgumentList(TypeArgumentList(SingletonSeparatedList(type)));

classToInherit = classToInherit.AddMembers(GenerateTrackingProperties(propertySymbol, assignedType));
Expand All @@ -123,7 +123,7 @@ static void AddNamespacesFromPropertyType(HashSet<string> namespaces, ITypeSymbo
new[]
{
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
AccessorDeclaration(SyntaxKind.InitAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
AccessorDeclaration(SyntaxKind.InitAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
}
)
)
Expand Down Expand Up @@ -176,7 +176,6 @@ static void AddNamespacesFromPropertyType(HashSet<string> namespaces, ITypeSymbo
AssignmentExpression(
SyntaxKind.SimpleAssignmentExpression,
IdentifierName(propertySymbol.Name),

InvocationExpression(
MemberAccessExpression(
SyntaxKind.SimpleMemberAccessExpression,
Expand Down Expand Up @@ -404,7 +403,7 @@ private static MemberDeclarationSyntax[] GenerateTrackingProperties(IPropertySym
new[]
{
AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
}
)
)
Expand Down Expand Up @@ -432,7 +431,7 @@ private static MemberDeclarationSyntax[] GenerateTrackingProperties(IPropertySym
)
)
)
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken))
.WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
};
}

Expand All @@ -446,7 +445,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
node is (ClassDeclarationSyntax or RecordDeclarationSyntax)
and TypeDeclarationSyntax
{
BaseList: { } baseList
BaseList: { } baseList,
}
&& baseList.Types.Any(
z => z.Type is GenericNameSyntax qns && qns.Identifier.Text.EndsWith("IPropertyTracking", StringComparison.Ordinal)
Expand Down Expand Up @@ -476,12 +475,12 @@ and TypeDeclarationSyntax
);
}
)
.Where(x => x.symbol is not null && x.targetSymbol is not null);
.Where(x => x.symbol is { } && x.targetSymbol is { });

context.RegisterSourceOutput(
values,
// ReSharper disable once NullableWarningSuppressionIsUsed
static (productionContext, tuple) => GeneratePropertyTracking(productionContext, tuple.syntax, tuple.symbol, tuple.targetSymbol!)
);
}
}
}
11 changes: 8 additions & 3 deletions src/Analyzers/SymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ internal static class SymbolExtensions
{
public static AttributeData? GetAttribute(this ISymbol symbol, string attributeClassName)
{
return symbol.GetAttributes().FirstOrDefault(z => z.AttributeClass?.Name == attributeClassName || z.AttributeClass.GetFullMetadataName() == attributeClassName);
return symbol
.GetAttributes()
.FirstOrDefault(z => z.AttributeClass?.Name == attributeClassName || z.AttributeClass.GetFullMetadataName() == attributeClassName);
}

public static SyntaxList<UsingDirectiveSyntax> AddDistinctUsingStatements(this SyntaxList<UsingDirectiveSyntax> usingDirectiveSyntax, IEnumerable<string> namespaces)
public static SyntaxList<UsingDirectiveSyntax> AddDistinctUsingStatements(
this SyntaxList<UsingDirectiveSyntax> usingDirectiveSyntax,
IEnumerable<string> namespaces
)
{
foreach (var additionalUsing in namespaces.Where(z => !string.IsNullOrWhiteSpace(z)))
{
Expand All @@ -21,4 +26,4 @@ public static SyntaxList<UsingDirectiveSyntax> AddDistinctUsingStatements(this S

return usingDirectiveSyntax;
}
}
}
Loading

0 comments on commit 17e6931

Please sign in to comment.