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 AssertIsTypeAnalyzer : XunitAnalyzer
16+ {
17+ public const string DiagnosticId = Constants . Tips . Xunit . AssertIsType ;
18+ public const string Category = Constants . Tips . Category ;
19+
20+ public const string Message = "Use .Should().BeOfType()." ;
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 AssertIsTypeGenericTypeSyntaxVisitor ( ) ,
27+ new AssertIsTypeTypeSyntaxVisitor ( )
28+ } ;
29+
30+ //public static T IsType<T>(object? @object)
31+ public class AssertIsTypeGenericTypeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
32+ {
33+ public AssertIsTypeGenericTypeSyntaxVisitor ( ) : base (
34+ MemberValidator . HasArguments ( "IsType" , 1 )
35+ )
36+ {
37+ }
38+ }
39+
40+ //public static T IsType(Type expectedType, object? @object)
41+ public class AssertIsTypeTypeSyntaxVisitor : FluentAssertionsCSharpSyntaxVisitor
42+ {
43+ public AssertIsTypeTypeSyntaxVisitor ( ) : base (
44+ MemberValidator . HasArguments ( "IsType" , 2 )
45+ )
46+ {
47+ }
48+ }
49+ }
50+
51+ [ ExportCodeFixProvider ( LanguageNames . CSharp , Name = nameof ( AssertIsTypeCodeFix ) ) , Shared ]
52+ public class AssertIsTypeCodeFix : XunitCodeFixProvider
53+ {
54+ public override ImmutableArray < string > FixableDiagnosticIds => ImmutableArray . Create ( AssertIsTypeAnalyzer . DiagnosticId ) ;
55+
56+ protected override ExpressionSyntax GetNewExpression (
57+ ExpressionSyntax expression ,
58+ FluentAssertionsDiagnosticProperties properties )
59+ {
60+ switch ( properties . VisitorName )
61+ {
62+ case nameof ( AssertIsTypeAnalyzer . AssertIsTypeGenericTypeSyntaxVisitor ) :
63+ return RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould ( expression , "IsType" , "BeOfType" ) ;
64+ case nameof ( AssertIsTypeAnalyzer . AssertIsTypeTypeSyntaxVisitor ) :
65+ var newExpression = RenameMethodAndReorderActualExpectedAndReplaceWithSubjectShould ( expression , "IsType" , "BeOfType" ) ;
66+ return ReplaceTypeOfArgumentWithGenericTypeIfExists ( newExpression , "BeOfType" ) ;
67+ default :
68+ throw new System . InvalidOperationException ( $ "Invalid visitor name - { properties . VisitorName } ") ;
69+ }
70+ }
71+ }
0 commit comments