@@ -40,15 +40,22 @@ class UseCompatibleCmdlets : AstVisitor, IScriptRule
4040 private Dictionary < string , bool > curCmdletCompatibilityMap ;
4141 private Dictionary < string , dynamic > platformSpecMap ;
4242 private string scriptPath ;
43+ private bool IsInitialized ;
4344
4445 public UseCompatibleCmdlets ( )
46+ {
47+ validParameters = new List < string > { "mode" , "uri" , "compatibility" } ;
48+ IsInitialized = false ;
49+ }
50+
51+ private void Initialize ( )
4552 {
4653 diagnosticRecords = new List < DiagnosticRecord > ( ) ;
4754 psCmdletMap = new Dictionary < string , HashSet < string > > ( ) ;
48- validParameters = new List < string > { "mode" , "uri" , "compatibility" } ;
4955 curCmdletCompatibilityMap = new Dictionary < string , bool > ( StringComparer . OrdinalIgnoreCase ) ;
5056 platformSpecMap = new Dictionary < string , dynamic > ( StringComparer . OrdinalIgnoreCase ) ;
5157 SetupCmdletsDictionary ( ) ;
58+ IsInitialized = true ;
5259 }
5360
5461 private void SetupCmdletsDictionary ( )
@@ -134,7 +141,14 @@ private void SetupCmdletsDictionary()
134141 var settingsPath = Path . Combine ( Path . GetDirectoryName ( path ) , "Settings" ) ;
135142 if ( ! Directory . Exists ( settingsPath ) )
136143 {
137- return ;
144+ // try one level down as the PSScriptAnalyzer module structure is not consistent
145+ // CORECLR binaries are in PSScriptAnalyzer/coreclr/, PowerShell v3 binaries are in PSScriptAnalyzer/PSv3/
146+ // and PowerShell v5 binaries are in PSScriptAnalyzer/
147+ settingsPath = Path . Combine ( Path . GetDirectoryName ( Path . GetDirectoryName ( path ) ) , "Settings" ) ;
148+ if ( ! Directory . Exists ( settingsPath ) )
149+ {
150+ return ;
151+ }
138152 }
139153 ProcessDirectory ( settingsPath ) ;
140154 }
@@ -248,6 +262,13 @@ private bool RuleParamsValid(Dictionary<string, object> ruleArgs)
248262 /// <returns>A an enumerable type containing the violations</returns>
249263 public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName )
250264 {
265+ // we do not want to initialize the data structures if the rule is not being used for analysis
266+ // hence we initialize when this method is called for the first time
267+ if ( ! IsInitialized )
268+ {
269+ Initialize ( ) ;
270+ }
271+
251272 if ( ast == null )
252273 {
253274 throw new ArgumentNullException ( "ast" ) ;
0 commit comments