@@ -33,9 +33,6 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
3333#endif
3434 public class ProvideCommentHelp : ConfigurableRule
3535 {
36-
37- // todo add option for comment type
38- // todo add option for help placement (beforeFuntion, BodyStart, BodyEnd:)
3936 [ ConfigurableRuleProperty ( defaultValue : true ) ]
4037 public bool ExportedOnly { get ; protected set ; }
4138
@@ -45,6 +42,10 @@ public class ProvideCommentHelp : ConfigurableRule
4542 [ ConfigurableRuleProperty ( defaultValue : false ) ]
4643 public bool VSCodeSnippetCorrection { get ; protected set ; }
4744
45+ // possible options: before, begin, end
46+ [ ConfigurableRuleProperty ( defaultValue : "before" ) ]
47+ public string Placement { get ; protected set ; }
48+
4849 public ProvideCommentHelp ( ) : base ( )
4950 {
5051 // Enable the rule by default
@@ -146,16 +147,66 @@ private IEnumerable<CorrectionExtent> GetCorrection(FunctionDefinitionAst funcDe
146147 helpBuilder . AddParameter ( paramAst . Name . VariablePath . UserPath ) ;
147148 }
148149
149- var correctionExtents = new List < CorrectionExtent > ( ) ;
150+ int startLine , endLine , startColumn , endColumn ;
151+ GetCorrectionPosition ( funcDefnAst , out startLine , out endLine , out startColumn , out endColumn ) ;
150152 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 ) ) ,
156161 funcDefnAst . Extent . File ) ;
157162 }
158163
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+
159210 private class ViolationFinder : AstVisitor
160211 {
161212 private HashSet < string > functionWhitelist ;
0 commit comments