1
1
using Microsoft . CodeAnalysis ;
2
2
using Microsoft . CodeAnalysis . CodeFixes ;
3
- using Microsoft . CodeAnalysis . CSharp ;
4
3
using Microsoft . CodeAnalysis . CSharp . Syntax ;
5
4
using Microsoft . CodeAnalysis . Diagnostics ;
6
5
using System . Collections . Generic ;
@@ -27,14 +26,14 @@ public class CollectionShouldContainPropertyAnalyzer : FluentAssertionsAnalyzer
27
26
}
28
27
}
29
28
30
- private class AnyShouldBeTrueSyntaxVisitor : FluentAssertionsWithLambdaArgumentCSharpSyntaxVisitor
29
+ public class AnyShouldBeTrueSyntaxVisitor : FluentAssertionsWithLambdaArgumentCSharpSyntaxVisitor
31
30
{
32
31
protected override string MethodContainingLambda => "Any" ;
33
32
public AnyShouldBeTrueSyntaxVisitor ( ) : base ( "Any" , "Should" , "BeTrue" )
34
33
{
35
34
}
36
35
}
37
- private class WhereShouldNotBeEmptySyntaxVisitor : FluentAssertionsWithLambdaArgumentCSharpSyntaxVisitor
36
+ public class WhereShouldNotBeEmptySyntaxVisitor : FluentAssertionsWithLambdaArgumentCSharpSyntaxVisitor
38
37
{
39
38
protected override string MethodContainingLambda => "Where" ;
40
39
public WhereShouldNotBeEmptySyntaxVisitor ( ) : base ( "Where" , "Should" , "NotBeEmpty" )
@@ -47,8 +46,24 @@ public WhereShouldNotBeEmptySyntaxVisitor() : base("Where", "Should", "NotBeEmpt
47
46
public class CollectionShouldContainPropertyCodeFix : FluentAssertionsCodeFixProvider
48
47
{
49
48
public override ImmutableArray < string > FixableDiagnosticIds => ImmutableArray . Create ( CollectionShouldContainPropertyAnalyzer . DiagnosticId ) ;
49
+
50
+ protected override StatementSyntax GetNewStatement ( ExpressionStatementSyntax statement , FluentAssertionsDiagnosticProperties properties )
51
+ {
52
+ if ( properties . VisitorName == nameof ( CollectionShouldContainPropertyAnalyzer . AnyShouldBeTrueSyntaxVisitor ) )
53
+ {
54
+ var remove = new NodeReplacement . RemoveAndExtractArgumentsNodeReplacement ( "Any" ) ;
55
+ var newStatement = GetNewStatement ( statement , remove ) ;
50
56
51
- protected override StatementSyntax GetNewStatement ( FluentAssertionsDiagnosticProperties properties )
52
- => SyntaxFactory . ParseStatement ( $ "{ properties . VariableName } .Should().Contain({ properties . CombineWithBecauseArgumentsString ( properties . LambdaString ) } );") ;
57
+ return GetNewStatement ( newStatement , new NodeReplacement . RenameAndPrependArgumentsNodeReplacement ( "BeTrue" , "Contain" , remove . Arguments ) ) ;
58
+ }
59
+ else if ( properties . VisitorName == nameof ( CollectionShouldContainPropertyAnalyzer . WhereShouldNotBeEmptySyntaxVisitor ) )
60
+ {
61
+ var remove = new NodeReplacement . RemoveAndExtractArgumentsNodeReplacement ( "Where" ) ;
62
+ var newStatement = GetNewStatement ( statement , remove ) ;
63
+
64
+ return GetNewStatement ( newStatement , new NodeReplacement . RenameAndPrependArgumentsNodeReplacement ( "NotBeEmpty" , "Contain" , remove . Arguments ) ) ;
65
+ }
66
+ throw new System . InvalidOperationException ( $ "Invalid visitor name - { properties . VisitorName } ") ;
67
+ }
53
68
}
54
69
}
0 commit comments