@@ -33,9 +33,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
33
33
#endif
34
34
public class ProvideCommentHelp : ConfigurableRule
35
35
{
36
-
37
- // todo add option for comment type
38
- // todo add option for help placement (beforeFuntion, BodyStart, BodyEnd:)
39
36
[ ConfigurableRuleProperty ( defaultValue : true ) ]
40
37
public bool ExportedOnly { get ; protected set ; }
41
38
@@ -45,6 +42,10 @@ public class ProvideCommentHelp : ConfigurableRule
45
42
[ ConfigurableRuleProperty ( defaultValue : false ) ]
46
43
public bool VSCodeSnippetCorrection { get ; protected set ; }
47
44
45
+ // possible options: before, begin, end
46
+ [ ConfigurableRuleProperty ( defaultValue : "before" ) ]
47
+ public string Placement { get ; protected set ; }
48
+
48
49
public ProvideCommentHelp ( ) : base ( )
49
50
{
50
51
// Enable the rule by default
@@ -146,16 +147,66 @@ private IEnumerable<CorrectionExtent> GetCorrection(FunctionDefinitionAst funcDe
146
147
helpBuilder . AddParameter ( paramAst . Name . VariablePath . UserPath ) ;
147
148
}
148
149
149
- var correctionExtents = new List < CorrectionExtent > ( ) ;
150
+ int startLine , endLine , startColumn , endColumn ;
151
+ GetCorrectionPosition ( funcDefnAst , out startLine , out endLine , out startColumn , out endColumn ) ;
150
152
yield return new CorrectionExtent (
151
- funcDefnAst . Extent . StartLineNumber ,
152
- funcDefnAst . Extent . StartLineNumber ,
153
- funcDefnAst . Extent . StartColumnNumber ,
154
- funcDefnAst . Extent . StartColumnNumber ,
155
- helpBuilder . GetCommentHelp ( BlockComment , VSCodeSnippetCorrection ) + Environment . NewLine ,
153
+ startLine ,
154
+ endLine ,
155
+ startColumn ,
156
+ endColumn ,
157
+ ProcessCorrectionForPlacement (
158
+ helpBuilder . GetCommentHelp (
159
+ BlockComment ,
160
+ VSCodeSnippetCorrection ) ) ,
156
161
funcDefnAst . Extent . File ) ;
157
162
}
158
163
164
+ private string ProcessCorrectionForPlacement ( string correction )
165
+ {
166
+ switch ( Placement . ToLower ( ) )
167
+ {
168
+ case "begin" :
169
+ return "{" + Environment . NewLine + correction + Environment . NewLine ;
170
+ case "end" :
171
+ return Environment . NewLine + correction + Environment . NewLine ;
172
+ default :
173
+ return correction + Environment . NewLine ;
174
+ }
175
+ }
176
+
177
+ private void GetCorrectionPosition (
178
+ FunctionDefinitionAst funcDefnAst ,
179
+ out int startLine ,
180
+ out int endLine ,
181
+ out int startColumn ,
182
+ out int endColumn )
183
+ {
184
+ // the better thing to do is get the line/column from corresponding tokens
185
+ switch ( Placement . ToLower ( ) )
186
+ {
187
+ case "begin" :
188
+ startLine = funcDefnAst . Body . Extent . StartLineNumber ;
189
+ endLine = startLine ;
190
+ startColumn = funcDefnAst . Body . Extent . StartColumnNumber ;
191
+ endColumn = startColumn + 1 ;
192
+ break ;
193
+
194
+ case "end" :
195
+ startLine = funcDefnAst . Body . Extent . EndLineNumber ;
196
+ endLine = startLine ;
197
+ startColumn = funcDefnAst . Body . Extent . EndColumnNumber - 1 ;
198
+ endColumn = startColumn ;
199
+ break ;
200
+
201
+ default : // before
202
+ startLine = funcDefnAst . Extent . StartLineNumber ;
203
+ endLine = startLine ;
204
+ startColumn = funcDefnAst . Extent . StartColumnNumber ;
205
+ endColumn = startColumn ;
206
+ break ;
207
+ }
208
+ }
209
+
159
210
private class ViolationFinder : AstVisitor
160
211
{
161
212
private HashSet < string > functionWhitelist ;
0 commit comments