@@ -747,5 +747,64 @@ private ConcurrentBag<Declaration> FindEventHandlers(IEnumerable<Declaration> de
747
747
748
748
return new ConcurrentBag < Declaration > ( handlers ) ;
749
749
}
750
+
751
+ public IEnumerable < Declaration > GetDeclarationsAccessibleToScope ( Declaration target , IEnumerable < Declaration > declarations )
752
+ {
753
+
754
+ object obj = new object ( ) ;
755
+ lock ( obj )
756
+ {
757
+ if ( target == null ) { return Enumerable . Empty < Declaration > ( ) ; }
758
+
759
+ return declarations
760
+ . Where ( candidateDeclaration =>
761
+ (
762
+ IsDeclarationInTheSameProcedure ( candidateDeclaration , target )
763
+ || IsDeclarationChildOfTheScope ( candidateDeclaration , target )
764
+ || IsModuleLevelDeclarationOfTheScope ( candidateDeclaration , target )
765
+ || IsProjectGlobalDeclaration ( candidateDeclaration , target )
766
+ ) ) . Distinct ( ) ;
767
+ }
768
+ }
769
+
770
+ private bool IsDeclarationInTheSameProcedure ( Declaration candidateDeclaration , Declaration scopingDeclaration )
771
+ {
772
+ return candidateDeclaration . ParentScope == scopingDeclaration . ParentScope ;
773
+ }
774
+
775
+ private bool IsDeclarationChildOfTheScope ( Declaration candidateDeclaration , Declaration scopingDeclaration )
776
+ {
777
+ return scopingDeclaration == candidateDeclaration . ParentDeclaration ;
778
+ }
779
+
780
+ private bool IsModuleLevelDeclarationOfTheScope ( Declaration candidateDeclaration , Declaration scopingDeclaration )
781
+ {
782
+ if ( candidateDeclaration . ParentDeclaration == null )
783
+ {
784
+ return false ;
785
+ }
786
+ return candidateDeclaration . ComponentName == scopingDeclaration . ComponentName
787
+ && ! IsDeclaredWithinMethodOrProperty ( candidateDeclaration . ParentDeclaration . Context ) ;
788
+ }
789
+
790
+ private bool IsProjectGlobalDeclaration ( Declaration candidateDeclaration , Declaration scopingDeclaration )
791
+ {
792
+ return candidateDeclaration . ProjectName == scopingDeclaration . ProjectName
793
+ && ! ( candidateDeclaration . ParentScopeDeclaration is ClassModuleDeclaration )
794
+ && ( candidateDeclaration . Accessibility == Accessibility . Public
795
+ || ( ( candidateDeclaration . Accessibility == Accessibility . Implicit )
796
+ && ( candidateDeclaration . ParentScopeDeclaration is ProceduralModuleDeclaration ) ) ) ;
797
+ }
798
+
799
+ private bool IsDeclaredWithinMethodOrProperty ( RuleContext procedureContextCandidate )
800
+ {
801
+ if ( procedureContextCandidate == null ) { return false ; }
802
+
803
+ return ( procedureContextCandidate is VBAParser . SubStmtContext )
804
+ || ( procedureContextCandidate is VBAParser . FunctionStmtContext )
805
+ || ( procedureContextCandidate is VBAParser . PropertyLetStmtContext )
806
+ || ( procedureContextCandidate is VBAParser . PropertyGetStmtContext )
807
+ || ( procedureContextCandidate is VBAParser . PropertySetStmtContext ) ;
808
+ }
750
809
}
751
810
}
0 commit comments