Skip to content

Commit 1de9cfe

Browse files
author
Kapil Borle
committed
Add a switch to analyze exported function only
If the switch `ExportedOnly` is on, the rule finds help comment violations for exported functions only. If the switch is off, then the rule finds violations for all functions.
1 parent da033a4 commit 1de9cfe

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

Rules/ProvideCommentHelp.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
3333
public class ProvideCommentHelp : ConfigurableRule
3434
{
3535

36+
[ConfigurableRuleProperty(defaultValue: true)]
37+
public bool ExportedOnly { get; protected set; }
38+
3639
public ProvideCommentHelp() : base()
3740
{
3841
// Enable the rule by default
@@ -50,7 +53,7 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
5053
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
5154

5255
var exportedFunctions = Helper.Instance.GetExportedFunction(ast);
53-
var violationFinder = new ViolationFinder(exportedFunctions);
56+
var violationFinder = new ViolationFinder(exportedFunctions, ExportedOnly);
5457
ast.Visit(violationFinder);
5558
foreach (var functionDefinitionAst in violationFinder.FunctionDefinitionAsts)
5659
{
@@ -146,12 +149,13 @@ private IEnumerable<CorrectionExtent> GetCorrection(FunctionDefinitionAst funcDe
146149

147150
private class ViolationFinder : AstVisitor
148151
{
149-
private HashSet<string> exportedFunctions;
152+
private HashSet<string> functionWhitelist;
150153
private List<FunctionDefinitionAst> functionDefinitionAsts;
154+
private bool useFunctionWhitelist;
151155

152156
public ViolationFinder()
153157
{
154-
exportedFunctions = new HashSet<string>();
158+
functionWhitelist = new HashSet<string>();
155159
functionDefinitionAsts = new List<FunctionDefinitionAst>();
156160
}
157161

@@ -162,7 +166,12 @@ public ViolationFinder(HashSet<string> exportedFunctions) : this()
162166
throw new ArgumentNullException(nameof(exportedFunctions));
163167
}
164168

165-
this.exportedFunctions = exportedFunctions;
169+
this.functionWhitelist = exportedFunctions;
170+
}
171+
172+
public ViolationFinder(HashSet<string> exportedFunctions, bool exportedOnly) : this(exportedFunctions)
173+
{
174+
this.useFunctionWhitelist = exportedOnly;
166175
}
167176

168177
public IEnumerable<FunctionDefinitionAst> FunctionDefinitionAsts { get { return functionDefinitionAsts; } }
@@ -174,7 +183,7 @@ public ViolationFinder(HashSet<string> exportedFunctions) : this()
174183
/// <returns></returns>
175184
public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst funcAst)
176185
{
177-
if (exportedFunctions.Contains(funcAst.Name)
186+
if ((!useFunctionWhitelist || functionWhitelist.Contains(funcAst.Name))
178187
&& funcAst.GetHelpContent() == null)
179188
{
180189
functionDefinitionAsts.Add(funcAst);

0 commit comments

Comments
 (0)