Skip to content

Commit 974a255

Browse files
committed
Merge pull request #102 from PowerShell/revert-101-revert-100-BugFixes
Revert "Revert "Only Trigger PSProvideVerboseMessage in Advanced Scri…
2 parents c488f10 + 3aa8ee8 commit 974a255

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

Rules/ProvideVerboseMessage.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212

1313
using System;
1414
using System.Collections.Generic;
15+
using System.Linq;
1516
using System.Management.Automation.Language;
1617
using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic;
1718
using System.ComponentModel.Composition;
1819
using System.Globalization;
20+
using System.Management.Automation;
1921

2022
namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules
2123
{
@@ -33,11 +35,11 @@ public class ProvideVerboseMessage : SkipNamedBlock, IScriptRule
3335
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3436
{
3537
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
36-
38+
3739
ClearList();
3840
this.AddNames(new List<string>() { "Configuration", "Workflow" });
3941
DiagnosticRecords.Clear();
40-
42+
4143
this.fileName = fileName;
4244
//We only check that advanced functions should have Write-Verbose
4345
ast.Visit(this);
@@ -57,6 +59,17 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun
5759
return AstVisitAction.SkipChildren;
5860
}
5961

62+
//Write-Verbose is not required for non-advanced functions
63+
if (funcAst.Body != null && funcAst.Body.ParamBlock != null
64+
&& funcAst.Body.ParamBlock.Attributes != null &&
65+
funcAst.Body.ParamBlock.Parameters != null)
66+
{
67+
if (!funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute)))
68+
{
69+
return AstVisitAction.Continue;
70+
}
71+
}
72+
6073
var commandAsts = funcAst.Body.FindAll(testAst => testAst is CommandAst, false);
6174
bool hasVerbose = false;
6275

0 commit comments

Comments
 (0)