Skip to content

Commit

Permalink
chore: format namespace (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
Meir017 authored Oct 8, 2023
1 parent 0441304 commit b9f16b0
Show file tree
Hide file tree
Showing 121 changed files with 3,941 additions and 4,062 deletions.
269 changes: 134 additions & 135 deletions src/FluentAssertions.Analyzers/Constants.cs

Large diffs are not rendered by default.

97 changes: 48 additions & 49 deletions src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,74 +9,73 @@
using System.Linq;
using System.Threading.Tasks;

namespace FluentAssertions.Analyzers
namespace FluentAssertions.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AsyncVoidAnalyzer : DiagnosticAnalyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class AsyncVoidAnalyzer : DiagnosticAnalyzer
{
public const string DiagnosticId = Constants.CodeSmell.AsyncVoid;
public const string Title = "Code Smell";
public const string Message = "The assertions might not be executed when assigning an async void lambda to a Action";
public const string DiagnosticId = Constants.CodeSmell.AsyncVoid;
public const string Title = "Code Smell";
public const string Message = "The assertions might not be executed when assigning an async void lambda to a Action";

public static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, Message, Constants.CodeSmell.Category, DiagnosticSeverity.Warning, true);
public static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor(DiagnosticId, Title, Message, Constants.CodeSmell.Category, DiagnosticSeverity.Warning, true);

Check warning on line 21 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FluentAssertions0801' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 21 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FluentAssertions0801' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 21 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FluentAssertions0801' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 21 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FluentAssertions0801' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 21 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

Enable analyzer release tracking for the analyzer project containing rule 'FluentAssertions0801' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

Check warning on line 21 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

Enable analyzer release tracking for the analyzer project containing rule 'FluentAssertions0801' (https://github.com/dotnet/roslyn-analyzers/blob/main/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md)

public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule);

public sealed override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCodeBlockAction(AnalyzeCodeBlock);
}
public sealed override void Initialize(AnalysisContext context)
{
context.EnableConcurrentExecution();
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None);
context.RegisterCodeBlockAction(AnalyzeCodeBlock);
}

private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
{
var method = context.CodeBlock as MethodDeclarationSyntax;
if (method == null) return;
private void AnalyzeCodeBlock(CodeBlockAnalysisContext context)
{
var method = context.CodeBlock as MethodDeclarationSyntax;
if (method == null) return;

if (method.Body != null)
if (method.Body != null)
{
foreach (var statement in method.Body.Statements.OfType<LocalDeclarationStatementSyntax>())
{
foreach (var statement in method.Body.Statements.OfType<LocalDeclarationStatementSyntax>())
{

var diagnostic = AnalyzeStatement(context.SemanticModel, statement);
if (diagnostic != null)
{
context.ReportDiagnostic(diagnostic);
}
var diagnostic = AnalyzeStatement(context.SemanticModel, statement);
if (diagnostic != null)
{
context.ReportDiagnostic(diagnostic);
}
return;
}
return;
}
}

protected virtual Diagnostic AnalyzeStatement(SemanticModel semanticModel, LocalDeclarationStatementSyntax statement)
{
var symbolInfo = semanticModel.GetSymbolInfo(statement.Declaration.Type);
if (symbolInfo.Symbol?.Name != nameof(Action)) return null;
protected virtual Diagnostic AnalyzeStatement(SemanticModel semanticModel, LocalDeclarationStatementSyntax statement)
{
var symbolInfo = semanticModel.GetSymbolInfo(statement.Declaration.Type);
if (symbolInfo.Symbol?.Name != nameof(Action)) return null;

foreach (var variable in statement.Declaration.Variables)
{
if (variable.Initializer == null) continue;
foreach (var variable in statement.Declaration.Variables)
{
if (variable.Initializer == null) continue;

if (!(variable.Initializer.Value is ParenthesizedLambdaExpressionSyntax lambda)) continue;
if (!(variable.Initializer.Value is ParenthesizedLambdaExpressionSyntax lambda)) continue;

if (lambda.AsyncKeyword.IsKind(SyntaxKind.AsyncKeyword))
{
return Diagnostic.Create(descriptor: Rule, location: statement.GetLocation());
}
if (lambda.AsyncKeyword.IsKind(SyntaxKind.AsyncKeyword))
{
return Diagnostic.Create(descriptor: Rule, location: statement.GetLocation());
}

return null;
}

return null;
}
}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AsyncVoidCodeFix)), Shared]
public class AsyncVoidCodeFix : CodeFixProvider
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(AsyncVoidCodeFix)), Shared]
public class AsyncVoidCodeFix : CodeFixProvider

Check warning on line 74 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Debug)

'AsyncVoidCodeFix' registers one or more code fixes, but does not override the method 'CodeFixProvider.GetFixAllProvider'. Override this method and provide a non-null FixAllProvider for FixAll support, potentially 'WellKnownFixAllProviders.BatchFixer', or 'null' to explicitly disable FixAll support.

Check warning on line 74 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu-latest, Release)

'AsyncVoidCodeFix' registers one or more code fixes, but does not override the method 'CodeFixProvider.GetFixAllProvider'. Override this method and provide a non-null FixAllProvider for FixAll support, potentially 'WellKnownFixAllProviders.BatchFixer', or 'null' to explicitly disable FixAll support.

Check warning on line 74 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Debug)

'AsyncVoidCodeFix' registers one or more code fixes, but does not override the method 'CodeFixProvider.GetFixAllProvider'. Override this method and provide a non-null FixAllProvider for FixAll support, potentially 'WellKnownFixAllProviders.BatchFixer', or 'null' to explicitly disable FixAll support.

Check warning on line 74 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (windows-latest, Release)

'AsyncVoidCodeFix' registers one or more code fixes, but does not override the method 'CodeFixProvider.GetFixAllProvider'. Override this method and provide a non-null FixAllProvider for FixAll support, potentially 'WellKnownFixAllProviders.BatchFixer', or 'null' to explicitly disable FixAll support.

Check warning on line 74 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Debug)

'AsyncVoidCodeFix' registers one or more code fixes, but does not override the method 'CodeFixProvider.GetFixAllProvider'. Override this method and provide a non-null FixAllProvider for FixAll support, potentially 'WellKnownFixAllProviders.BatchFixer', or 'null' to explicitly disable FixAll support.

Check warning on line 74 in src/FluentAssertions.Analyzers/Tips/AsyncVoid.cs

View workflow job for this annotation

GitHub Actions / build (macos-latest, Release)

'AsyncVoidCodeFix' registers one or more code fixes, but does not override the method 'CodeFixProvider.GetFixAllProvider'. Override this method and provide a non-null FixAllProvider for FixAll support, potentially 'WellKnownFixAllProviders.BatchFixer', or 'null' to explicitly disable FixAll support.
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AsyncVoidAnalyzer.DiagnosticId);
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(AsyncVoidAnalyzer.DiagnosticId);
public override Task RegisterCodeFixesAsync(CodeFixContext context)
{
return Task.CompletedTask;
}
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
using FluentAssertions.Analyzers.Utilities;
using Microsoft.CodeAnalysis;

namespace FluentAssertions.Analyzers
namespace FluentAssertions.Analyzers;

public abstract class CollectionAnalyzer : FluentAssertionsAnalyzer
{
public abstract class CollectionAnalyzer : FluentAssertionsAnalyzer
protected override bool ShouldAnalyzeVariableNamedType(INamedTypeSymbol type, SemanticModel semanticModel)
{
protected override bool ShouldAnalyzeVariableNamedType(INamedTypeSymbol type, SemanticModel semanticModel)
{
return type.SpecialType != SpecialType.System_String
&& type.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_Generic_IEnumerable_T);
}

override protected bool ShouldAnalyzeVariableType(ITypeSymbol type, SemanticModel semanticModel)
{
return type.SpecialType != SpecialType.System_String
&& type.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_Generic_IEnumerable_T);
}
return type.SpecialType != SpecialType.System_String
&& type.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_Generic_IEnumerable_T);
}

override protected bool ShouldAnalyzeVariableType(ITypeSymbol type, SemanticModel semanticModel)
{
return type.SpecialType != SpecialType.System_String
&& type.IsTypeOrConstructedFromTypeOrImplementsType(SpecialType.System_Collections_Generic_IEnumerable_T);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,83 +7,82 @@
using System.Collections.Immutable;
using System.Composition;

namespace FluentAssertions.Analyzers
namespace FluentAssertions.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CollectionShouldBeEmptyAnalyzer : CollectionAnalyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CollectionShouldBeEmptyAnalyzer : CollectionAnalyzer
{
public const string DiagnosticId = Constants.Tips.Collections.CollectionsShouldBeEmpty;
public const string Category = Constants.Tips.Category;
public const string DiagnosticId = Constants.Tips.Collections.CollectionsShouldBeEmpty;
public const string Category = Constants.Tips.Category;

public const string Message = "Use .Should().BeEmpty() instead.";
public const string Message = "Use .Should().BeEmpty() instead.";

protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
{
get
{
get
{
yield return new AnyShouldBeFalseSyntaxVisitor();
yield return new ShouldHaveCount0SyntaxVisitor();
}
yield return new AnyShouldBeFalseSyntaxVisitor();
yield return new ShouldHaveCount0SyntaxVisitor();
}
}

public class AnyShouldBeFalseSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
public class AnyShouldBeFalseSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public AnyShouldBeFalseSyntaxVisitor() : base(MemberValidator.MethodNotContainingLambda("Any"), MemberValidator.Should, new MemberValidator("BeFalse"))
{
public AnyShouldBeFalseSyntaxVisitor() : base(MemberValidator.MethodNotContainingLambda("Any"), MemberValidator.Should, new MemberValidator("BeFalse"))
{
}
}
}

public class ShouldHaveCount0SyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
public class ShouldHaveCount0SyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public ShouldHaveCount0SyntaxVisitor() : base(MemberValidator.Should, new MemberValidator("HaveCount", HaveCountArgumentsValidator))
{
public ShouldHaveCount0SyntaxVisitor() : base(MemberValidator.Should, new MemberValidator("HaveCount", HaveCountArgumentsValidator))
{
}
}

private static bool HaveCountArgumentsValidator(SeparatedSyntaxList<ArgumentSyntax> arguments, SemanticModel semanticModel)
{
if (!arguments.Any()) return false;
private static bool HaveCountArgumentsValidator(SeparatedSyntaxList<ArgumentSyntax> arguments, SemanticModel semanticModel)
{
if (!arguments.Any()) return false;

return arguments[0].Expression is LiteralExpressionSyntax literal
&& literal.Token.Value is int argument
&& argument == 0;
}
return arguments[0].Expression is LiteralExpressionSyntax literal
&& literal.Token.Value is int argument
&& argument == 0;
}
}
}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldBeEmptyCodeFix)), Shared]
public class CollectionShouldBeEmptyCodeFix : FluentAssertionsCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeEmptyAnalyzer.DiagnosticId);
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldBeEmptyCodeFix)), Shared]
public class CollectionShouldBeEmptyCodeFix : FluentAssertionsCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeEmptyAnalyzer.DiagnosticId);

protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
{
switch (properties.VisitorName)
{
switch (properties.VisitorName)
{
case nameof(CollectionShouldBeEmptyAnalyzer.AnyShouldBeFalseSyntaxVisitor):
return GetNewExpression(expression, NodeReplacement.Remove("Any"), NodeReplacement.Rename("BeFalse", "BeEmpty"));
case nameof(CollectionShouldBeEmptyAnalyzer.ShouldHaveCount0SyntaxVisitor):
return GetNewExpression(expression, new HaveCountNodeReplacement());
default:
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
}
case nameof(CollectionShouldBeEmptyAnalyzer.AnyShouldBeFalseSyntaxVisitor):
return GetNewExpression(expression, NodeReplacement.Remove("Any"), NodeReplacement.Rename("BeFalse", "BeEmpty"));
case nameof(CollectionShouldBeEmptyAnalyzer.ShouldHaveCount0SyntaxVisitor):
return GetNewExpression(expression, new HaveCountNodeReplacement());
default:
throw new System.InvalidOperationException($"Invalid visitor name - {properties.VisitorName}");
}
}

private class HaveCountNodeReplacement : NodeReplacement
private class HaveCountNodeReplacement : NodeReplacement
{
public override bool IsValidNode(LinkedListNode<MemberAccessExpressionSyntax> listNode) => listNode.Value.Name.Identifier.Text == "HaveCount";
public override SyntaxNode ComputeOld(LinkedListNode<MemberAccessExpressionSyntax> listNode) => listNode.Value.Parent;
public override SyntaxNode ComputeNew(LinkedListNode<MemberAccessExpressionSyntax> listNode)
{
public override bool IsValidNode(LinkedListNode<MemberAccessExpressionSyntax> listNode) => listNode.Value.Name.Identifier.Text == "HaveCount";
public override SyntaxNode ComputeOld(LinkedListNode<MemberAccessExpressionSyntax> listNode) => listNode.Value.Parent;
public override SyntaxNode ComputeNew(LinkedListNode<MemberAccessExpressionSyntax> listNode)
{
var invocation = (InvocationExpressionSyntax)listNode.Value.Parent;
var invocation = (InvocationExpressionSyntax)listNode.Value.Parent;

invocation = invocation.ReplaceNode(listNode.Value, listNode.Value.WithName(SyntaxFactory.IdentifierName("BeEmpty")));
invocation = invocation.ReplaceNode(listNode.Value, listNode.Value.WithName(SyntaxFactory.IdentifierName("BeEmpty")));

// remove the 0 argument
var arguments = invocation.ArgumentList.Arguments.RemoveAt(0);
// remove the 0 argument
var arguments = invocation.ArgumentList.Arguments.RemoveAt(0);

return invocation.WithArgumentList(invocation.ArgumentList.WithArguments(arguments));
}
return invocation.WithArgumentList(invocation.ArgumentList.WithArguments(arguments));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,45 @@
using System.Collections.Immutable;
using System.Composition;

namespace FluentAssertions.Analyzers
namespace FluentAssertions.Analyzers;

[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CollectionShouldBeInAscendingOrderAnalyzer : CollectionAnalyzer
{
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public class CollectionShouldBeInAscendingOrderAnalyzer : CollectionAnalyzer
{
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldBeInAscendingOrder;
public const string Category = Constants.Tips.Category;
public const string DiagnosticId = Constants.Tips.Collections.CollectionShouldBeInAscendingOrder;
public const string Category = Constants.Tips.Category;

public const string Message = "Use .Should().BeInAscendingOrder() instead.";
public const string Message = "Use .Should().BeInAscendingOrder() instead.";

protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
protected override DiagnosticDescriptor Rule => new DiagnosticDescriptor(DiagnosticId, Title, Message, Category, DiagnosticSeverity.Info, true);
protected override IEnumerable<FluentAssertionsCSharpSyntaxVisitor> Visitors
{
get
{
get
{
yield return new OrderByShouldEqualSyntaxVisitor();
}
yield return new OrderByShouldEqualSyntaxVisitor();
}
}

public class OrderByShouldEqualSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
public class OrderByShouldEqualSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
{
public OrderByShouldEqualSyntaxVisitor() : base(MemberValidator.MethodContainingLambda("OrderBy"), MemberValidator.Should, MemberValidator.ArgumentIsVariable("Equal"))
{
public OrderByShouldEqualSyntaxVisitor() : base(MemberValidator.MethodContainingLambda("OrderBy"), MemberValidator.Should, MemberValidator.ArgumentIsVariable("Equal"))
{
}
}
}
}

[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldBeInAscendingOrderCodeFix)), Shared]
public class CollectionShouldBeInAscendingOrderCodeFix : FluentAssertionsCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeInAscendingOrderAnalyzer.DiagnosticId);
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(CollectionShouldBeInAscendingOrderCodeFix)), Shared]
public class CollectionShouldBeInAscendingOrderCodeFix : FluentAssertionsCodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds => ImmutableArray.Create(CollectionShouldBeInAscendingOrderAnalyzer.DiagnosticId);

protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
{
var remove = NodeReplacement.RemoveAndExtractArguments("OrderBy");
var newExpression = GetNewExpression(expression, remove);
protected override ExpressionSyntax GetNewExpression(ExpressionSyntax expression, FluentAssertionsDiagnosticProperties properties)
{
var remove = NodeReplacement.RemoveAndExtractArguments("OrderBy");
var newExpression = GetNewExpression(expression, remove);

newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInAscendingOrder"));
newExpression = GetNewExpression(newExpression, NodeReplacement.RenameAndRemoveFirstArgument("Equal", "BeInAscendingOrder"));

return GetNewExpression(newExpression, NodeReplacement.PrependArguments("BeInAscendingOrder", remove.Arguments));
}
return GetNewExpression(newExpression, NodeReplacement.PrependArguments("BeInAscendingOrder", remove.Arguments));
}
}
Loading

0 comments on commit b9f16b0

Please sign in to comment.