@@ -11,6 +11,8 @@ namespace Flow.Launcher.Localization.Analyzers.Localize
11
11
[ DiagnosticAnalyzer ( LanguageNames . CSharp ) ]
12
12
public class ContextAvailabilityAnalyzer : DiagnosticAnalyzer
13
13
{
14
+ #region DiagnosticAnalyzer
15
+
14
16
public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics => ImmutableArray . Create (
15
17
AnalyzerDiagnostics . ContextIsAField ,
16
18
AnalyzerDiagnostics . ContextIsNotStatic ,
@@ -25,47 +27,55 @@ public override void Initialize(AnalysisContext context)
25
27
context . RegisterSyntaxNodeAction ( AnalyzeNode , SyntaxKind . ClassDeclaration ) ;
26
28
}
27
29
30
+ #endregion
31
+
32
+ #region Analyze Methods
33
+
28
34
private static void AnalyzeNode ( SyntaxNodeAnalysisContext context )
29
35
{
30
36
var configOptions = context . Options . AnalyzerConfigOptionsProvider ;
31
37
var useDI = configOptions . GetFLLUseDependencyInjection ( ) ;
32
-
33
- // If we use dependency injection, we don't need to check for this context property
34
- if ( useDI ) return ;
38
+ if ( useDI )
39
+ {
40
+ // If we use dependency injection, we don't need to check for this context property
41
+ return ;
42
+ }
35
43
36
44
var classDeclaration = ( ClassDeclarationSyntax ) context . Node ;
37
45
var semanticModel = context . SemanticModel ;
38
- var classSymbol = semanticModel . GetDeclaredSymbol ( classDeclaration ) ;
39
-
40
- if ( ! IsPluginEntryClass ( classSymbol ) ) return ;
41
-
42
- var contextProperty = classDeclaration . Members . OfType < PropertyDeclarationSyntax > ( )
43
- . Select ( p => semanticModel . GetDeclaredSymbol ( p ) )
44
- . FirstOrDefault ( p => p ? . Type . Name is Constants . PluginContextTypeName ) ;
46
+ var pluginClassInfo = Helper . GetPluginClassInfo ( classDeclaration , semanticModel , context . CancellationToken ) ;
47
+ if ( pluginClassInfo == null )
48
+ {
49
+ // Cannot find class that implements IPluginI18n
50
+ return ;
51
+ }
45
52
46
- if ( contextProperty != null )
53
+ // Context property is found, check if it's a valid property
54
+ if ( pluginClassInfo . PropertyName != null )
47
55
{
48
- if ( ! contextProperty . IsStatic )
56
+ if ( ! pluginClassInfo . IsStatic )
49
57
{
50
58
context . ReportDiagnostic ( Diagnostic . Create (
51
59
AnalyzerDiagnostics . ContextIsNotStatic ,
52
- contextProperty . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) . GetLocation ( )
60
+ pluginClassInfo . CodeFixLocation
53
61
) ) ;
54
62
return ;
55
63
}
56
64
57
- if ( contextProperty . DeclaredAccessibility is Accessibility . Private || contextProperty . DeclaredAccessibility is Accessibility . Protected )
65
+ if ( pluginClassInfo . IsPrivate || pluginClassInfo . IsProtected )
58
66
{
59
67
context . ReportDiagnostic ( Diagnostic . Create (
60
68
AnalyzerDiagnostics . ContextAccessIsTooRestrictive ,
61
- contextProperty . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) . GetLocation ( )
69
+ pluginClassInfo . CodeFixLocation
62
70
) ) ;
63
71
return ;
64
72
}
65
73
74
+ // If the context property is valid, we don't need to check for anything else
66
75
return ;
67
76
}
68
77
78
+ // Context property is not found, check if it's declared as a field
69
79
var fieldDeclaration = classDeclaration . Members
70
80
. OfType < FieldDeclarationSyntax > ( )
71
81
. SelectMany ( f => f . Declaration . Variables )
@@ -75,7 +85,6 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
75
85
? . DeclaringSyntaxReferences [ 0 ]
76
86
. GetSyntax ( )
77
87
. FirstAncestorOrSelf < FieldDeclarationSyntax > ( ) ;
78
-
79
88
if ( parentSyntax != null )
80
89
{
81
90
context . ReportDiagnostic ( Diagnostic . Create (
@@ -85,13 +94,13 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
85
94
return ;
86
95
}
87
96
97
+ // Context property is not found, report an error
88
98
context . ReportDiagnostic ( Diagnostic . Create (
89
99
AnalyzerDiagnostics . ContextIsNotDeclared ,
90
100
classDeclaration . Identifier . GetLocation ( )
91
101
) ) ;
92
102
}
93
103
94
- private static bool IsPluginEntryClass ( INamedTypeSymbol namedTypeSymbol ) =>
95
- namedTypeSymbol ? . Interfaces . Any ( i => i . Name == Constants . PluginInterfaceName ) ?? false ;
104
+ #endregion
96
105
}
97
106
}
0 commit comments