@@ -40,15 +40,22 @@ class UseCompatibleCmdlets : AstVisitor, IScriptRule
40
40
private Dictionary < string , bool > curCmdletCompatibilityMap ;
41
41
private Dictionary < string , dynamic > platformSpecMap ;
42
42
private string scriptPath ;
43
+ private bool IsInitialized ;
43
44
44
45
public UseCompatibleCmdlets ( )
46
+ {
47
+ validParameters = new List < string > { "mode" , "uri" , "compatibility" } ;
48
+ IsInitialized = false ;
49
+ }
50
+
51
+ private void Initialize ( )
45
52
{
46
53
diagnosticRecords = new List < DiagnosticRecord > ( ) ;
47
54
psCmdletMap = new Dictionary < string , HashSet < string > > ( ) ;
48
- validParameters = new List < string > { "mode" , "uri" , "compatibility" } ;
49
55
curCmdletCompatibilityMap = new Dictionary < string , bool > ( StringComparer . OrdinalIgnoreCase ) ;
50
56
platformSpecMap = new Dictionary < string , dynamic > ( StringComparer . OrdinalIgnoreCase ) ;
51
57
SetupCmdletsDictionary ( ) ;
58
+ IsInitialized = true ;
52
59
}
53
60
54
61
private void SetupCmdletsDictionary ( )
@@ -134,7 +141,14 @@ private void SetupCmdletsDictionary()
134
141
var settingsPath = Path . Combine ( Path . GetDirectoryName ( path ) , "Settings" ) ;
135
142
if ( ! Directory . Exists ( settingsPath ) )
136
143
{
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
+ }
138
152
}
139
153
ProcessDirectory ( settingsPath ) ;
140
154
}
@@ -248,6 +262,13 @@ private bool RuleParamsValid(Dictionary<string, object> ruleArgs)
248
262
/// <returns>A an enumerable type containing the violations</returns>
249
263
public IEnumerable < DiagnosticRecord > AnalyzeScript ( Ast ast , string fileName )
250
264
{
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
+
251
272
if ( ast == null )
252
273
{
253
274
throw new ArgumentNullException ( "ast" ) ;
0 commit comments