@@ -28,20 +28,20 @@ public class Helper
28
28
29
29
private readonly CommandInvocationIntrinsics _invokeCommand ;
30
30
31
+ private readonly Lazy < CommandInfoCache > _commandInfoCache ;
32
+
31
33
private Dictionary < string , Dictionary < string , object > > _ruleArguments ;
32
34
33
35
private PSVersionTable _psVersionTable ;
34
36
35
- private Lazy < CommandInfoCache > _commandInfoCache ;
36
-
37
37
private CommandInfoCache CommandInfoCache => _commandInfoCache . Value ;
38
38
39
39
#endregion
40
40
41
41
#region Singleton
42
- private static object syncRoot = new Object ( ) ;
42
+ private static readonly object s_syncRoot = new object ( ) ;
43
43
44
- private static Helper instance ;
44
+ private static Helper s_instance ;
45
45
46
46
/// <summary>
47
47
/// The helper instance that handles utility functions
@@ -50,20 +50,20 @@ public static Helper Instance
50
50
{
51
51
get
52
52
{
53
- if ( instance == null )
53
+ if ( s_instance == null )
54
54
{
55
55
Instance = new Helper ( ) ;
56
56
}
57
57
58
- return instance ;
58
+ return s_instance ;
59
59
}
60
60
internal set
61
61
{
62
- lock ( syncRoot )
62
+ lock ( s_syncRoot )
63
63
{
64
- if ( instance == null )
64
+ if ( s_instance == null )
65
65
{
66
- instance = value ;
66
+ s_instance = value ;
67
67
}
68
68
}
69
69
}
@@ -76,14 +76,14 @@ internal set
76
76
/// <summary>
77
77
/// Dictionary contains mapping of cmdlet to alias
78
78
/// </summary>
79
- private Dictionary < String , List < String > > CmdletToAliasDictionary ;
79
+ private readonly Dictionary < string , List < string > > _cmdletToAliasDictionary ;
80
80
81
81
/// <summary>
82
82
/// Dictionary contains mapping of alias to cmdlet
83
83
/// </summary>
84
- private Dictionary < String , String > AliasToCmdletDictionary ;
84
+ private readonly Dictionary < string , string > _aliasToCmdletDictionary ;
85
85
86
- internal TupleComparer tupleComparer = new TupleComparer ( ) ;
86
+ internal TupleComparer TupleComparer { get ; } = new TupleComparer ( ) ;
87
87
88
88
/// <summary>
89
89
/// My Tokens
@@ -96,24 +96,30 @@ internal set
96
96
/// the starting position of the open curly brace and the second item
97
97
/// is the closing position of the closing curly brace.
98
98
/// </summary>
99
- private Dictionary < String , List < Tuple < int , int > > > KeywordBlockDictionary ;
99
+ private readonly Dictionary < string , List < Tuple < int , int > > > _keywordBlockDictionary ;
100
100
101
101
/// <summary>
102
102
/// Key of dictionary is ast, value is the corresponding variableanalysis
103
103
/// </summary>
104
- private Dictionary < Ast , VariableAnalysis > VariableAnalysisDictionary ;
104
+ private readonly Dictionary < Ast , VariableAnalysis > VariableAnalysisDictionary ;
105
+
106
+ private readonly IReadOnlyList < string > functionScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" } ;
105
107
106
- private string [ ] functionScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" } ;
108
+ private readonly IReadOnlyList < string > variableScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" , "variable:" , " :"} ;
107
109
108
- private string [ ] variableScopes = new string [ ] { "global:" , "local:" , "script:" , "private:" , "variable:" , ":" } ;
109
110
#endregion
110
111
111
112
/// <summary>
112
113
/// Initializes the Helper class.
113
114
/// </summary>
114
115
private Helper ( )
115
116
{
116
-
117
+ _ruleArguments = new Dictionary < string , Dictionary < string , object > > ( StringComparer . OrdinalIgnoreCase ) ;
118
+ _commandInfoCache = new Lazy < CommandInfoCache > ( ( ) => new CommandInfoCache ( ) ) ;
119
+ _cmdletToAliasDictionary = new Dictionary < string , List < string > > ( StringComparer . OrdinalIgnoreCase ) ;
120
+ _aliasToCmdletDictionary = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
121
+ _keywordBlockDictionary = new Dictionary < string , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
122
+ VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
117
123
}
118
124
119
125
/// <summary>
@@ -123,37 +129,34 @@ private Helper()
123
129
/// A CommandInvocationIntrinsics instance for use in gathering
124
130
/// information about available commands and aliases.
125
131
/// </param>
126
- public Helper ( CommandInvocationIntrinsics invokeCommand )
132
+ public Helper ( CommandInvocationIntrinsics invokeCommand ) : base ( )
127
133
{
128
134
_invokeCommand = invokeCommand ;
129
- _ruleArguments = new Dictionary < string , Dictionary < string , object > > ( StringComparer . OrdinalIgnoreCase ) ;
130
- _commandInfoCache = new Lazy < CommandInfoCache > ( ( ) => new CommandInfoCache ( ) ) ;
131
- CmdletToAliasDictionary = new Dictionary < string , List < string > > ( StringComparer . OrdinalIgnoreCase ) ;
132
- AliasToCmdletDictionary = new Dictionary < string , string > ( StringComparer . OrdinalIgnoreCase ) ;
133
- KeywordBlockDictionary = new Dictionary < string , List < Tuple < int , int > > > ( StringComparer . OrdinalIgnoreCase ) ;
134
- VariableAnalysisDictionary = new Dictionary < Ast , VariableAnalysis > ( ) ;
135
135
}
136
136
137
137
#region Methods
138
138
/// <summary>
139
- /// Initialize : Initializes dictionary of alias .
139
+ /// Creates the alias dictionary from the current PowerShell session .
140
140
/// </summary>
141
141
public void Initialize ( )
142
142
{
143
- IEnumerable < CommandInfo > aliases = _invokeCommand . GetCommands ( "*" , CommandTypes . Alias , nameIsPattern : true ) ;
144
-
145
- foreach ( AliasInfo aliasInfo in aliases )
143
+ if ( _invokeCommand != null )
146
144
{
147
- if ( ! CmdletToAliasDictionary . ContainsKey ( aliasInfo . Definition ) )
148
- {
149
- CmdletToAliasDictionary . Add ( aliasInfo . Definition , new List < string > ( ) { aliasInfo . Name } ) ;
150
- }
151
- else
145
+ IEnumerable < CommandInfo > aliases = _invokeCommand . GetCommands ( "*" , CommandTypes . Alias , nameIsPattern : true ) ;
146
+
147
+ foreach ( AliasInfo aliasInfo in aliases )
152
148
{
153
- CmdletToAliasDictionary [ aliasInfo . Definition ] . Add ( aliasInfo . Name ) ;
154
- }
149
+ if ( ! _cmdletToAliasDictionary . ContainsKey ( aliasInfo . Definition ) )
150
+ {
151
+ _cmdletToAliasDictionary . Add ( aliasInfo . Definition , new List < string > ( ) { aliasInfo . Name } ) ;
152
+ }
153
+ else
154
+ {
155
+ _cmdletToAliasDictionary [ aliasInfo . Definition ] . Add ( aliasInfo . Name ) ;
156
+ }
155
157
156
- AliasToCmdletDictionary . Add ( aliasInfo . Name , aliasInfo . Definition ) ;
158
+ _aliasToCmdletDictionary . Add ( aliasInfo . Name , aliasInfo . Definition ) ;
159
+ }
157
160
}
158
161
}
159
162
@@ -221,9 +224,9 @@ public List<String> CmdletNameAndAliases(String Cmdlet)
221
224
List < String > results = new List < String > ( ) ;
222
225
results . Add ( Cmdlet ) ;
223
226
224
- if ( CmdletToAliasDictionary . ContainsKey ( Cmdlet ) )
227
+ if ( _cmdletToAliasDictionary . ContainsKey ( Cmdlet ) )
225
228
{
226
- results . AddRange ( CmdletToAliasDictionary [ Cmdlet ] ) ;
229
+ results . AddRange ( _cmdletToAliasDictionary [ Cmdlet ] ) ;
227
230
}
228
231
229
232
return results ;
@@ -236,9 +239,9 @@ public List<String> CmdletNameAndAliases(String Cmdlet)
236
239
/// <returns></returns>
237
240
public string GetCmdletNameFromAlias ( String Alias )
238
241
{
239
- if ( AliasToCmdletDictionary . ContainsKey ( Alias ) )
242
+ if ( _aliasToCmdletDictionary . ContainsKey ( Alias ) )
240
243
{
241
- return AliasToCmdletDictionary [ Alias ] ;
244
+ return _aliasToCmdletDictionary [ Alias ] ;
242
245
}
243
246
244
247
return String . Empty ;
@@ -525,7 +528,7 @@ item is TypeDefinitionAst
525
528
return false ;
526
529
}
527
530
528
- private string NameWithoutScope ( string name , string [ ] scopes )
531
+ private string NameWithoutScope ( string name , IReadOnlyList < string > scopes )
529
532
{
530
533
if ( String . IsNullOrWhiteSpace ( name ) || scopes == null )
531
534
{
@@ -765,14 +768,14 @@ public bool SkipBlock(string keyword, Ast namedBlockAst)
765
768
766
769
FindClosingParenthesis ( keyword ) ;
767
770
768
- List < Tuple < int , int > > listTuples = KeywordBlockDictionary [ keyword ] ;
771
+ List < Tuple < int , int > > listTuples = _keywordBlockDictionary [ keyword ] ;
769
772
770
773
if ( listTuples == null || listTuples . Count == 0 )
771
774
{
772
775
return false ;
773
776
}
774
777
775
- int index = listTuples . BinarySearch ( Tuple . Create ( namedBlockAst . Extent . StartOffset , namedBlockAst . Extent . EndOffset ) , tupleComparer ) ;
778
+ int index = listTuples . BinarySearch ( Tuple . Create ( namedBlockAst . Extent . StartOffset , namedBlockAst . Extent . EndOffset ) , TupleComparer ) ;
776
779
777
780
if ( index < 0 || index >= Tokens . Length )
778
781
{
@@ -828,12 +831,12 @@ private void FindClosingParenthesis(string keyword)
828
831
}
829
832
830
833
// Only do this one time per script. The keywordblockdictionary is cleared everytime we run a new script
831
- if ( KeywordBlockDictionary . ContainsKey ( keyword ) )
834
+ if ( _keywordBlockDictionary . ContainsKey ( keyword ) )
832
835
{
833
836
return ;
834
837
}
835
838
836
- KeywordBlockDictionary [ keyword ] = new List < Tuple < int , int > > ( ) ;
839
+ _keywordBlockDictionary [ keyword ] = new List < Tuple < int , int > > ( ) ;
837
840
838
841
int [ ] tokenIndices = Tokens
839
842
. Select ( ( token , index ) =>
@@ -888,7 +891,7 @@ private void FindClosingParenthesis(string keyword)
888
891
continue ;
889
892
}
890
893
891
- KeywordBlockDictionary [ keyword ] . Add ( Tuple . Create ( Tokens [ openCurly ] . Extent . StartOffset ,
894
+ _keywordBlockDictionary [ keyword ] . Add ( Tuple . Create ( Tokens [ openCurly ] . Extent . StartOffset ,
892
895
Tokens [ closeCurly ] . Extent . EndOffset ) ) ;
893
896
}
894
897
}
0 commit comments