1+ using System . Collections . Generic ;
2+ using System . Collections . Immutable ;
3+ using System . Composition ;
4+ using System . Linq ;
5+ using FluentAssertions . Analyzers . Utilities ;
6+ using Microsoft . CodeAnalysis ;
7+ using Microsoft . CodeAnalysis . CodeFixes ;
8+ using Microsoft . CodeAnalysis . CSharp . Syntax ;
9+ using Microsoft . CodeAnalysis . Diagnostics ;
10+ using SF = Microsoft . CodeAnalysis . CSharp . SyntaxFactory ;
11+
12+ namespace FluentAssertions . Analyzers . Xunit ;
13+
14+ [ DiagnosticAnalyzer ( LanguageNames . CSharp ) ]
15+ public class AssertIsNotTypeAnalyzer : XunitAnalyzer
16+ {
17+ public const string DiagnosticId = Constants . Tips . Xunit . AssertIsNotType ;
18+ public const string Category = Constants . Tips . Category ;
19+
20+ public const string Message = "Use .Should().NotBeOfType()." ;
21+
22+ protected override DiagnosticDescriptor Rule => new ( DiagnosticId , Title , Message , Category , DiagnosticSeverity . Info , true ) ;
23+
24+ protected override IEnumerable < FluentAssertionsCSharpSyntaxVisitor > Visitors => new FluentAssertionsCSharpSyntaxVisitor [ ]
25+ {
26+ new AssertIsNotTypeGenericTypeSyntaxVisitor ( ) ,
27+ new AssertIsNotTypeTypeSyntaxVisitor ( )
28+ } ;
29+
30+ //public static T IsNotType<T>(object? @object)
31+ public class AssertIsNotTypeGenericTypeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
32+ {
33+ public AssertIsNotTypeGenericTypeSyntaxVisitor ( ) : base (
34+ MemberValidator . HasArguments ( "IsNotType" , 1 )
35+ )
36+ {
37+ }
38+ }
39+
40+ //public static T IsNotType(Type expectedType, object? @object)
41+ public class AssertIsNotTypeTypeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
42+ {
43+ public AssertIsNotTypeTypeSyntaxVisitor ( ) : base (
44+ MemberValidator . HasArguments ( "IsNotType" , 2 )
45+ )
46+ {
47+ }
48+ }
49+ }
50+
51+ [ ExportCodeFixProvider ( LanguageNames . CSharp , Name = nameof ( AssertIsNotTypeCodeFix ) ) , Shared ]
52+ public class AssertIsNotTypeCodeFix : XunitCodeFixProvider
53+ {
54+ public override ImmutableArray < string > FixableDiagnosticIds => ImmutableArray . Create ( AssertIsNotTypeAnalyzer . DiagnosticId ) ;
55+
56+ protected override ExpressionSyntax GetNewExpression (
57+ ExpressionSyntax expression ,
58+ FluentAssertionsDiagnosticProperties properties )
59+ {
60+ switch ( properties . VisitorName )
61+ {
62+ case nameof ( AssertIsNotTypeAnalyzer . AssertIsNotTypeGenericTypeSyntaxVisitor ) :
63+ return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould ( expression , "IsNotType" , "NotBeOfType" ) ;
64+ case nameof ( AssertIsNotTypeAnalyzer . AssertIsNotTypeTypeSyntaxVisitor ) :
65+ var newExpression = RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould ( expression , "IsNotType" , "NotBeOfType" ) ;
66+ return ReplaceTypeOfArgumentWithGenericTypeIfExists ( newExpression , "NotBeOfType" ) ;
67+ default :
68+ throw new System . InvalidOperationException ( $ "Invalid visitor name - { properties . VisitorName } ") ;
69+ }
70+ }
71+ }
0 commit comments