Skip to content

Commit 710d5ab

Browse files
author
Kapil Borle
committed
changes severity level, added a voilation condition and other minor changes.
1 parent f37c383 commit 710d5ab

File tree

4 files changed

+76
-14
lines changed

4 files changed

+76
-14
lines changed

RuleDocumentation/AvoidNullOrEmptyHelpMessageAttribute.md

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#AvoidNullOrEmtpyHelpMessageAttribute
2-
**Severity Level: Error**
2+
**Severity Level: Warning**
33

44

55
##Description
@@ -12,12 +12,32 @@ To fix a violation of this rule, please set its value to a non-empty string.
1212

1313
##Example
1414

15-
Wrong
15+
Wrong:
1616

17-
Function BadFuncEmtpyHelpMessage
17+
Function BadFuncEmtpyHelpMessageEmpty
1818
{
1919
Param(
20-
[Parameter(HelpMessage="")]
20+
[Parameter(HelpMessage='')]
21+
[String] $Param
22+
)
23+
24+
$Param
25+
}
26+
27+
Function BadFuncEmtpyHelpMessageNull
28+
{
29+
Param(
30+
[Parameter(HelpMessage=$null)]
31+
[String] $Param
32+
)
33+
34+
$Param
35+
}
36+
37+
Function BadFuncEmtpyHelpMessageNoAssignment
38+
{
39+
Param(
40+
[Parameter(HelpMessage)]
2141
[String] $Param
2242
)
2343

@@ -30,7 +50,7 @@ Correct:
3050
Function GoodFuncEmtpyHelpMessage
3151
{
3252
Param(
33-
[Parameter(HelpMessage="This is help.")]
53+
[Parameter(HelpMessage='This is helpful')]
3454
[String] $Param
3555
)
3656

Rules/AvoidNullOrEmptyHelpMessageAttribute.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2222
{
2323
/// <summary>
24-
/// AvoidUsingNullOrEmptyHelpMessageParameter: Check if the HelpMessage parameter is set to a non-empty string.
24+
/// AvoidUsingNullOrEmptyHelpMessageAttribute: Check if the HelpMessage parameter is set to a non-empty string.
2525
/// </summary>
2626
[Export(typeof(IScriptRule))]
2727
public class AvoidNullOrEmptyHelpMessageAttribute : IScriptRule
2828
{
2929
/// <summary>
30-
/// AvoidUsingNullOrEmptyHelpMessageParameter: Check if the HelpMessage parameter is set to a non-empty string.
30+
/// AvoidUsingNullOrEmptyHelpMessageAttribute: Check if the HelpMessage parameter is set to a non-empty string.
3131
/// </summary>
3232
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3333
{
@@ -39,32 +39,41 @@ public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3939
foreach (FunctionDefinitionAst funcAst in functionAsts)
4040
{
4141
if (funcAst.Body == null || funcAst.Body.ParamBlock == null
42-
|| funcAst.Body.ParamBlock.Attributes == null || funcAst.Body.ParamBlock.Parameters == null)
42+
|| funcAst.Body.ParamBlock.Attributes == null || funcAst.Body.ParamBlock.Parameters == null)
43+
{
4344
continue;
45+
}
4446

4547
foreach (var paramAst in funcAst.Body.ParamBlock.Parameters)
4648
{
4749
foreach (var paramAstAttribute in paramAst.Attributes)
4850
{
4951
if (!(paramAstAttribute is AttributeAst))
52+
{
5053
continue;
54+
}
5155

5256
var namedArguments = (paramAstAttribute as AttributeAst).NamedArguments;
5357

5458
if (namedArguments == null)
59+
{
5560
continue;
61+
}
5662

5763
foreach (NamedAttributeArgumentAst namedArgument in namedArguments)
5864
{
59-
if (!(String.Equals(namedArgument.ArgumentName, "HelpMessage", StringComparison.OrdinalIgnoreCase))
60-
|| namedArgument.ExpressionOmitted)
65+
if (!(namedArgument.ArgumentName.Equals("HelpMessage", StringComparison.OrdinalIgnoreCase)))
66+
{
6167
continue;
68+
}
6269

6370
string errCondition;
64-
if (namedArgument.Argument.Extent.Text.Equals("\"\""))
71+
if (namedArgument.ExpressionOmitted
72+
|| namedArgument.Argument.Extent.Text.Equals("\"\"")
73+
|| namedArgument.Argument.Extent.Text.Equals("\'\'"))
6574
{
6675
errCondition = "empty";
67-
}
76+
}
6877
else if (namedArgument.Argument.Extent.Text.Equals("$null", StringComparison.OrdinalIgnoreCase))
6978
{
7079
errCondition = "null";
@@ -134,7 +143,7 @@ public SourceType GetSourceType()
134143
/// <returns></returns>
135144
public RuleSeverity GetSeverity()
136145
{
137-
return RuleSeverity.Error;
146+
return RuleSeverity.Warning;
138147
}
139148

140149
/// <summary>

Tests/Rules/AvoidNullOrEmptyHelpMessageAttribute.ps1

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,37 @@ function BadFuncEmptyHelpMessageNoCmdletBinding
6767
)
6868
$Param1
6969
$Param2 = "test"
70+
}
71+
72+
73+
# same as BadFunc but this one has no cmdletbinding
74+
function BadFuncEmptyHelpMessageNoCmdletBindingSingleQoutes
75+
{
76+
param(
77+
# this has an empty string
78+
[Parameter(HelpMessage='')]
79+
[string] $Param1="String",
80+
81+
# this parameter has no default value
82+
[Parameter(HelpMessage="This is helpful.")]
83+
[string] $Param2
84+
)
85+
$Param1
86+
$Param2 = "test"
87+
}
88+
89+
# same as BadFunc but this one has no cmdletbinding
90+
function BadFuncEmptyHelpMessageNoCmdletBindingNoAssignment
91+
{
92+
param(
93+
# this has an empty string
94+
[Parameter(HelpMessage)]
95+
[string] $Param1="String",
96+
97+
# this parameter has no default value
98+
[Parameter(HelpMessage="This is helpful.")]
99+
[string] $Param2
100+
)
101+
$Param1
102+
$Param2 = "test"
70103
}

Tests/Rules/AvoidNullOrEmptyHelpMessageAttribute.tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ $noViolations = Invoke-ScriptAnalyzer "$directory\AvoidNullOrEmptyHelpMessageAtt
88
Describe "AvoidNullOrEmptyHelpMessageAttribute" {
99
Context "When there are violations" {
1010
It "detects the violations" {
11-
$violations.Count | Should Be 4
11+
$violations.Count | Should Be 6
1212
}
1313

1414
It "has the correct description message" {

0 commit comments

Comments
 (0)