@@ -11,6 +11,8 @@ namespace Flow.Launcher.Localization.Analyzers.Localize
1111 [ DiagnosticAnalyzer ( LanguageNames . CSharp ) ]
1212 public class ContextAvailabilityAnalyzer : DiagnosticAnalyzer
1313 {
14+ #region DiagnosticAnalyzer
15+
1416 public override ImmutableArray < DiagnosticDescriptor > SupportedDiagnostics => ImmutableArray . Create (
1517 AnalyzerDiagnostics . ContextIsAField ,
1618 AnalyzerDiagnostics . ContextIsNotStatic ,
@@ -25,47 +27,55 @@ public override void Initialize(AnalysisContext context)
2527 context . RegisterSyntaxNodeAction ( AnalyzeNode , SyntaxKind . ClassDeclaration ) ;
2628 }
2729
30+ #endregion
31+
32+ #region Analyze Methods
33+
2834 private static void AnalyzeNode ( SyntaxNodeAnalysisContext context )
2935 {
3036 var configOptions = context . Options . AnalyzerConfigOptionsProvider ;
3137 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+ }
3543
3644 var classDeclaration = ( ClassDeclarationSyntax ) context . Node ;
3745 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+ }
4552
46- if ( contextProperty != null )
53+ // Context property is found, check if it's a valid property
54+ if ( pluginClassInfo . PropertyName != null )
4755 {
48- if ( ! contextProperty . IsStatic )
56+ if ( ! pluginClassInfo . IsStatic )
4957 {
5058 context . ReportDiagnostic ( Diagnostic . Create (
5159 AnalyzerDiagnostics . ContextIsNotStatic ,
52- contextProperty . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) . GetLocation ( )
60+ pluginClassInfo . CodeFixLocation
5361 ) ) ;
5462 return ;
5563 }
5664
57- if ( contextProperty . DeclaredAccessibility is Accessibility . Private || contextProperty . DeclaredAccessibility is Accessibility . Protected )
65+ if ( pluginClassInfo . IsPrivate || pluginClassInfo . IsProtected )
5866 {
5967 context . ReportDiagnostic ( Diagnostic . Create (
6068 AnalyzerDiagnostics . ContextAccessIsTooRestrictive ,
61- contextProperty . DeclaringSyntaxReferences [ 0 ] . GetSyntax ( ) . GetLocation ( )
69+ pluginClassInfo . CodeFixLocation
6270 ) ) ;
6371 return ;
6472 }
6573
74+ // If the context property is valid, we don't need to check for anything else
6675 return ;
6776 }
6877
78+ // Context property is not found, check if it's declared as a field
6979 var fieldDeclaration = classDeclaration . Members
7080 . OfType < FieldDeclarationSyntax > ( )
7181 . SelectMany ( f => f . Declaration . Variables )
@@ -75,7 +85,6 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
7585 ? . DeclaringSyntaxReferences [ 0 ]
7686 . GetSyntax ( )
7787 . FirstAncestorOrSelf < FieldDeclarationSyntax > ( ) ;
78-
7988 if ( parentSyntax != null )
8089 {
8190 context . ReportDiagnostic ( Diagnostic . Create (
@@ -85,13 +94,13 @@ private static void AnalyzeNode(SyntaxNodeAnalysisContext context)
8594 return ;
8695 }
8796
97+ // Context property is not found, report an error
8898 context . ReportDiagnostic ( Diagnostic . Create (
8999 AnalyzerDiagnostics . ContextIsNotDeclared ,
90100 classDeclaration . Identifier . GetLocation ( )
91101 ) ) ;
92102 }
93103
94- private static bool IsPluginEntryClass ( INamedTypeSymbol namedTypeSymbol ) =>
95- namedTypeSymbol ? . Interfaces . Any ( i => i . Name == Constants . PluginInterfaceName ) ?? false ;
104+ #endregion
96105 }
97106}
0 commit comments