5
5
using System ;
6
6
using System . Collections . Generic ;
7
7
using System . Collections . Immutable ;
8
- using System . Linq ;
9
- using SF = Microsoft . CodeAnalysis . CSharp . SyntaxFactory ;
10
8
11
9
namespace FluentAssertions . Analyzers
12
10
{
@@ -46,8 +44,6 @@ private void AnalyzeExpressionStatementSyntax(SyntaxNodeAnalysisContext context)
46
44
}
47
45
}
48
46
49
- protected virtual bool ShouldAnalyzeMethod ( MethodDeclarationSyntax method ) => true ;
50
-
51
47
protected virtual bool ShouldAnalyzeVariableType ( INamedTypeSymbol type , SemanticModel semanticModel ) => true ;
52
48
53
49
protected virtual Diagnostic AnalyzeExpression ( ExpressionSyntax expression , SemanticModel semanticModel )
@@ -77,7 +73,7 @@ protected virtual Diagnostic CreateDiagnostic(TCSharpSyntaxVisitor visitor, Expr
77
73
{
78
74
var properties = visitor . ToDiagnosticProperties ( )
79
75
. Add ( Constants . DiagnosticProperties . Title , Title ) ;
80
- var newRule = new DiagnosticDescriptor ( Rule . Id , Rule . Title , Rule . MessageFormat , Rule . Category , Rule . DefaultSeverity , true ,
76
+ var newRule = new DiagnosticDescriptor ( Rule . Id , Rule . Title , Rule . MessageFormat , Rule . Category , Rule . DefaultSeverity , true ,
81
77
helpLinkUri : properties . GetValueOrDefault ( Constants . DiagnosticProperties . HelpLink ) ) ;
82
78
return Diagnostic . Create (
83
79
descriptor : newRule ,
@@ -105,44 +101,10 @@ public abstract class FluentAssertionsAnalyzer : FluentAssertionsAnalyzer<Fluent
105
101
106
102
public abstract class TestingLibraryAnalyzerBase : FluentAssertionsAnalyzer
107
103
{
108
- protected abstract string TestingLibraryNamespace { get ; }
109
-
110
- protected override bool ShouldAnalyzeMethod ( MethodDeclarationSyntax method )
111
- {
112
- var compilation = method . FirstAncestorOrSelf < CompilationUnitSyntax > ( ) ;
113
-
114
- if ( compilation == null ) return false ;
115
-
116
- foreach ( var @using in compilation . Usings )
117
- {
118
- if ( @using . Name . NormalizeWhitespace ( ) . ToString ( ) . Equals ( TestingLibraryNamespace ) ) return true ;
119
- }
120
-
121
- var parentNamespace = method . FirstAncestorOrSelf < NamespaceDeclarationSyntax > ( ) ;
122
- if ( parentNamespace != null )
123
- {
124
- var namespaces = new List < NamespaceDeclarationSyntax > ( ) ;
125
- while ( parentNamespace != null )
126
- {
127
- namespaces . Add ( parentNamespace ) ;
128
- parentNamespace = parentNamespace . Parent as NamespaceDeclarationSyntax ;
129
- }
130
- namespaces . Reverse ( ) ;
131
-
132
- for ( int i = 0 ; i < namespaces . Count ; i ++ )
133
- {
134
- var baseNamespace = string . Join ( "." , namespaces . Take ( i + 1 ) . Select ( ns => ns . Name ) ) ;
135
- foreach ( var @using in namespaces [ i ] . Usings )
136
- {
137
- if ( @using . Name . NormalizeWhitespace ( ) . ToString ( ) . Equals ( TestingLibraryNamespace ) ) return true ;
138
-
139
- var fullUsing = SF . ParseName ( $ "{ baseNamespace } .{ @using . Name } ") . NormalizeWhitespace ( ) . ToString ( ) ;
140
- if ( fullUsing . Equals ( TestingLibraryNamespace ) ) return true ;
141
- }
142
- }
143
- }
104
+ protected abstract string TestingLibraryModule { get ; }
105
+ protected abstract string TestingLibraryAssertionType { get ; }
144
106
145
- return false ;
146
- }
107
+ protected override bool ShouldAnalyzeVariableType ( INamedTypeSymbol type , SemanticModel semanticModel )
108
+ => type . Name == TestingLibraryAssertionType && type . ContainingModule . Name == TestingLibraryModule + ".dll" ;
147
109
}
148
110
}
0 commit comments