Skip to content

Commit f99b208

Browse files
author
Kapil Borle
committed
Add option for comment help placement
1 parent 6631983 commit f99b208

File tree

2 files changed

+133
-9
lines changed

2 files changed

+133
-9
lines changed

Rules/ProvideCommentHelp.cs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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;

Tests/Rules/ProvideCommentHelp.tests.ps1

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ $ruleSettings = @{
1010
Enable = $true
1111
ExportedOnly = $false
1212
BlockComment = $true
13+
Placement = "before"
1314
VSCodeSnippetCorrection = $false
1415
}
1516

@@ -214,6 +215,78 @@ Export-ModuleMember -Function foo
214215
Test-Correction $def $expectedCorrection $settings
215216
}
216217

218+
It "should return a help snippet correction with 2 parameters at the begining of function body" {
219+
$def = @'
220+
function foo {
221+
param($param1, $param2)
222+
}
223+
'@
224+
$expectedCorrection = @'
225+
{
226+
<#
227+
.SYNOPSIS
228+
Short description
229+
230+
.DESCRIPTION
231+
Long description
232+
233+
.PARAMETER param1
234+
Parameter description
235+
236+
.PARAMETER param2
237+
Parameter description
238+
239+
.EXAMPLE
240+
An example
241+
242+
.NOTES
243+
General notes
244+
#>
245+
246+
'@
247+
$ruleSettings.'ExportedOnly' = $false
248+
$ruleSettings.'BlockComment' = $true
249+
$ruleSettings.'VSCodeSnippetCorrection' = $false
250+
$ruleSettings.'Placement' = 'begin'
251+
Test-Correction $def $expectedCorrection $settings
252+
}
253+
254+
It "should return a help snippet correction with 2 parameters at the end of function body" {
255+
$def = @'
256+
function foo {
257+
param($param1, $param2)
258+
}
259+
'@
260+
$expectedCorrection = @'
261+
262+
<#
263+
.SYNOPSIS
264+
Short description
265+
266+
.DESCRIPTION
267+
Long description
268+
269+
.PARAMETER param1
270+
Parameter description
271+
272+
.PARAMETER param2
273+
Parameter description
274+
275+
.EXAMPLE
276+
An example
277+
278+
.NOTES
279+
General notes
280+
#>
281+
282+
'@
283+
$ruleSettings.'ExportedOnly' = $false
284+
$ruleSettings.'BlockComment' = $true
285+
$ruleSettings.'VSCodeSnippetCorrection' = $false
286+
$ruleSettings.'Placement' = 'end'
287+
Test-Correction $def $expectedCorrection $settings
288+
}
289+
217290
if ($PSVersionTable.PSVersion -ge [Version]'5.0.0') {
218291
It "Does not count violation in DSC class" {
219292
$dscViolations.Count | Should Be 0

0 commit comments

Comments
 (0)