Skip to content

Commit

Permalink
fixed warnings (#2072)
Browse files Browse the repository at this point in the history
* fixed warnings

* Automatically linting code

---------

Co-authored-by: Rocket Understudy <[email protected]>
  • Loading branch information
david-driscoll and rsg-bot authored Jun 16, 2024
1 parent 7463f61 commit 65b0873
Show file tree
Hide file tree
Showing 195 changed files with 642 additions and 307 deletions.
25 changes: 11 additions & 14 deletions src/Analyzers/ControllerActionBodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -720,20 +720,17 @@ private void GenerateMethods(
.AddDistinctUsingStatements(additionalUsings.Where(z => !string.IsNullOrWhiteSpace(z)));

var cu = CompilationUnit(
List<ExternAliasDirectiveSyntax>(),
List(usings),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
symbol.ContainingNamespace.IsGlobalNamespace
? newClass.ReparentDeclaration(context, syntax)
: NamespaceDeclaration(ParseName(symbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(newClass.ReparentDeclaration(context, syntax)))
)
)
.WithLeadingTrivia()
.WithTrailingTrivia()
.WithLeadingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))
.WithTrailingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.RestoreKeyword), true)), CarriageReturnLineFeed);
List<ExternAliasDirectiveSyntax>(),
List(usings),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
symbol.ContainingNamespace.IsGlobalNamespace
? newClass.ReparentDeclaration(context, syntax)
: NamespaceDeclaration(ParseName(symbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(newClass.ReparentDeclaration(context, syntax)))
)
)
.AddSharedTrivia();

context.AddSourceRelativeTo(syntax, "ControllerMethods", cu.NormalizeWhitespace().GetText(Encoding.UTF8));
}
Expand Down
25 changes: 11 additions & 14 deletions src/Analyzers/GraphqlMutationActionBodyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,20 +389,17 @@ private void GenerateMethods(
.AddDistinctUsingStatements(additionalUsings.Where(z => !string.IsNullOrWhiteSpace(z)));

var cu = CompilationUnit(
List<ExternAliasDirectiveSyntax>(),
List(usings),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
symbol.ContainingNamespace.IsGlobalNamespace
? newClass.ReparentDeclaration(context, syntax)
: NamespaceDeclaration(ParseName(symbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(newClass.ReparentDeclaration(context, syntax)))
)
)
.WithLeadingTrivia()
.WithTrailingTrivia()
.WithLeadingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))
.WithTrailingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.RestoreKeyword), true)), CarriageReturnLineFeed);
List<ExternAliasDirectiveSyntax>(),
List(usings),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
symbol.ContainingNamespace.IsGlobalNamespace
? newClass.ReparentDeclaration(context, syntax)
: NamespaceDeclaration(ParseName(symbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(newClass.ReparentDeclaration(context, syntax)))
)
)
.AddSharedTrivia();

context.AddSourceRelativeTo(syntax, "Mutations", cu.NormalizeWhitespace().GetText(Encoding.UTF8));

Expand Down
25 changes: 11 additions & 14 deletions src/Analyzers/GraphqlOptionalPropertyTrackingGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,20 +243,17 @@ INamedTypeSymbol targetSymbol
classToInherit = classToInherit.WithMembers(List(classToInherit.Members.Select(z => z.WithAttributeLists(SingletonList(Helpers.CompilerAttributes)))));

var cu = CompilationUnit(
List<ExternAliasDirectiveSyntax>(),
List(usings),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
symbol.ContainingNamespace.IsGlobalNamespace
? classToInherit.ReparentDeclaration(context, declaration)
: NamespaceDeclaration(ParseName(symbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(classToInherit.ReparentDeclaration(context, declaration)))
)
)
.WithLeadingTrivia()
.WithTrailingTrivia()
.WithLeadingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))
.WithTrailingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.RestoreKeyword), true)), CarriageReturnLineFeed);
List<ExternAliasDirectiveSyntax>(),
List(usings),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
symbol.ContainingNamespace.IsGlobalNamespace
? classToInherit.ReparentDeclaration(context, declaration)
: NamespaceDeclaration(ParseName(symbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(classToInherit.ReparentDeclaration(context, declaration)))
)
)
.AddSharedTrivia();

context.AddSourceRelativeTo(declaration, "Optionals", cu.NormalizeWhitespace().GetText(Encoding.UTF8));
return;
Expand Down
212 changes: 22 additions & 190 deletions src/Analyzers/InheritFromGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -189,20 +189,17 @@ AttributeData[] attributes
}

var cu = CompilationUnit(
List<ExternAliasDirectiveSyntax>(),
List(namespaces),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
targetSymbol.ContainingNamespace.IsGlobalNamespace
? classToInherit.ReparentDeclaration(context, declaration)
: NamespaceDeclaration(ParseName(targetSymbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(classToInherit.ReparentDeclaration(context, declaration)))
)
)
.WithLeadingTrivia()
.WithTrailingTrivia()
.WithLeadingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))
.WithTrailingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.RestoreKeyword), true)), CarriageReturnLineFeed);
List<ExternAliasDirectiveSyntax>(),
List(namespaces),
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
targetSymbol.ContainingNamespace.IsGlobalNamespace
? classToInherit.ReparentDeclaration(context, declaration)
: NamespaceDeclaration(ParseName(targetSymbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(classToInherit.ReparentDeclaration(context, declaration)))
)
)
.AddSharedTrivia();

context.AddSourceRelativeTo(declaration, "InheritFrom", cu.NormalizeWhitespace().GetText(Encoding.UTF8));
}
Expand Down Expand Up @@ -679,184 +676,19 @@ ImmutableArray<AttributeData> attributes
classToInherit.WithMembers(List(classToInherit.Members.Select(z => z.WithAttributeLists(SingletonList(Helpers.CompilerAttributes)))));

var cu = CompilationUnit(
List<ExternAliasDirectiveSyntax>(),
namespaces,
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
targetSymbol.ContainingNamespace.IsGlobalNamespace
? classToInherit.ReparentDeclaration(context, declaration)
: NamespaceDeclaration(ParseName(targetSymbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(classToInherit.ReparentDeclaration(context, declaration)))
)
)
.WithLeadingTrivia()
.WithTrailingTrivia()
.WithLeadingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.EnableKeyword), true)))
.WithTrailingTrivia(Trivia(NullableDirectiveTrivia(Token(SyntaxKind.RestoreKeyword), true)), CarriageReturnLineFeed);
List<ExternAliasDirectiveSyntax>(),
namespaces,
List<AttributeListSyntax>(),
SingletonList<MemberDeclarationSyntax>(
targetSymbol.ContainingNamespace.IsGlobalNamespace
? classToInherit.ReparentDeclaration(context, declaration)
: NamespaceDeclaration(ParseName(targetSymbol.ContainingNamespace.ToDisplayString()))
.WithMembers(SingletonList<MemberDeclarationSyntax>(classToInherit.ReparentDeclaration(context, declaration)))
)
)
.AddSharedTrivia();

context.AddSourceRelativeTo(declaration, "InheritFrom_Validator", cu.NormalizeWhitespace().GetText(Encoding.UTF8));
}
}
}

internal class RuleExpressionVisitor(ImmutableHashSet<string> excludedMembers) : CSharpSyntaxWalker
{
private readonly ImmutableArray<InvocationExpressionSyntax>.Builder _results = ImmutableArray.CreateBuilder<InvocationExpressionSyntax>();
public ImmutableArray<InvocationExpressionSyntax> Results => _results.ToImmutable();

public override void VisitInvocationExpression(InvocationExpressionSyntax node)
{
SyntaxNode? parent = node;
while (parent is { } and not { Parent: StatementSyntax, })
{
parent = parent.Parent;
}

if (parent is not InvocationExpressionSyntax parentNode) throw new("Unable to find invocation node");
switch (node)
{
case
{
Expression: SimpleNameSyntax { Identifier.ValueText: "RuleFor" or "RuleForEach", }
or MemberAccessExpressionSyntax { Name.Identifier.Text: "RuleFor" or "RuleForEach", },
ArgumentList.Arguments: [{ Expression: SimpleLambdaExpressionSyntax { Body: MemberAccessExpressionSyntax memberAccessExpressionSyntax, }, },],
}:
HandleRuleFor(parentNode, memberAccessExpressionSyntax);
break;
case
{
Expression: SimpleNameSyntax { Identifier.ValueText: "RuleSet", } or MemberAccessExpressionSyntax { Name.Identifier.Text: "RuleSet", },
ArgumentList.Arguments: [_, var action,],
}:
HandleRuleSet(parentNode, action);
break;
case
{
Expression: SimpleNameSyntax { Identifier.ValueText: "When" or "WhenAsync", }
or MemberAccessExpressionSyntax { Name.Identifier.Text: "When" or "WhenAsync", },
ArgumentList.Arguments: [var predicate, var whenAction,],
}:
HandleWhen(parentNode, predicate, whenAction, null);
break;
case
{
Expression: SimpleNameSyntax { Identifier.ValueText: "Otherwise", }
or MemberAccessExpressionSyntax { Name.Identifier.Text: "Otherwise", },
ArgumentList.Arguments: [var otherwiseAction2,],
}:
( var predicate2, var whenAction2 ) = parentNode
.DescendantNodesAndSelf()
.OfType<InvocationExpressionSyntax>()
.Select(
static z => z is
{
Expression: SimpleNameSyntax { Identifier.ValueText: "When" or "WhenAsync", }
or MemberAccessExpressionSyntax { Name.Identifier.Text: "When" or "WhenAsync", },
ArgumentList.Arguments: [var p, var action,],
}
? ( p, action )
: ( null!, null! )
)
.First(z => z is { action: { }, p: { }, });

HandleWhen(parentNode, predicate2, whenAction2, otherwiseAction2);
break;
default:
base.VisitInvocationExpression(node);
break;
}
}

private void HandleRuleFor(InvocationExpressionSyntax parent, MemberAccessExpressionSyntax memberAccessExpressionSyntax)
{
if (excludedMembers.Contains(memberAccessExpressionSyntax.Name.Identifier.Text)) return;
_results.Add(parent);
}

private void HandleRuleSet(InvocationExpressionSyntax parent, ArgumentSyntax action)
{
// TODO: Support methods?
if (HandleNestedAction(action) is { } updatedAction) _results.Add(parent.ReplaceNode(action, updatedAction));
}

private void HandleWhen(InvocationExpressionSyntax parent, ArgumentSyntax predicate, ArgumentSyntax whenAction, ArgumentSyntax? otherwiseAction)
{
// TODO: this needs it's own visitor
// var visitor = new RuleExpressionVisitor(excludedMembers);
// visitor.Visit(predicate);
var parameter = predicate.DescendantNodes().OfType<ParameterSyntax>().First();
var predicateContainsExcludedMember = predicate
.DescendantNodes()
.OfType<MemberAccessExpressionSyntax>()
.Where(
z => z is { Expression: SimpleNameSyntax expression, Name: { } name, }
&& expression.Identifier.Text == parameter.Identifier.Text
)
.Any(z => excludedMembers.Contains(z.Name.Identifier.Text));
if (predicateContainsExcludedMember) return;

if (otherwiseAction is null && HandleNestedAction(whenAction) is { } updatedWhenAction)
{
_results.Add(parent.ReplaceNode(whenAction, updatedWhenAction));
return;
}

if (otherwiseAction is null) return;

switch ( HandleNestedAction(whenAction), HandleNestedAction(otherwiseAction) )
{
case (null, null):
return;
case ({ } updatedWhenAction2, null):
_results.Add(
parent
.RemoveNode(otherwiseAction, SyntaxRemoveOptions.KeepEndOfLine)!
.ReplaceNode(whenAction, updatedWhenAction2)
);
break;
case (null, { } updatedOtherwiseAction2):
parent = parent.ReplaceNodes(
[whenAction, otherwiseAction,],
(syntax, argumentSyntax) => syntax == otherwiseAction
? HandleNestedAction(otherwiseAction) ?? argumentSyntax.WithExpression(ParenthesizedLambdaExpression().WithBlock(Block()))
: syntax == whenAction
? argumentSyntax.WithExpression(ParenthesizedLambdaExpression().WithBlock(Block()))
: argumentSyntax
);
_results.Add(parent);
break;
case ({ }, { }):
parent = parent.ReplaceNodes(
[whenAction, otherwiseAction,],
(syntax, argumentSyntax) =>
{
if (syntax == otherwiseAction)
return HandleNestedAction(otherwiseAction) ?? argumentSyntax.WithExpression(ParenthesizedLambdaExpression().WithBlock(Block()));
if (syntax == whenAction)
return HandleNestedAction(whenAction) ?? argumentSyntax.WithExpression(ParenthesizedLambdaExpression().WithBlock(Block()));
return argumentSyntax;
}
);
_results.Add(parent);
break;
}
}

private ArgumentSyntax? HandleNestedAction(ArgumentSyntax? action)
{
if (action is null) return null;
// TODO: Support methods?
var visitor = new RuleExpressionVisitor(excludedMembers);
visitor.Visit(action);
if (visitor.Results.Length == 0) return null;

var removeNodes = action
.DescendantNodes()
.OfType<InvocationExpressionSyntax>()
.Where(z => z.Parent is StatementSyntax)
.Except(visitor.Results)
.Select(z => z.Parent)
.ToImmutableArray();
return action.RemoveNodes(removeNodes, SyntaxRemoveOptions.KeepEndOfLine)!;
}
}
Loading

0 comments on commit 65b0873

Please sign in to comment.