From b28f98f51c8e196581e0433f8b03df74edddfe4e Mon Sep 17 00:00:00 2001 From: GoodOlClint Date: Wed, 6 May 2015 21:14:01 -0500 Subject: [PATCH 1/2] Do not require Write-Verbose in functions without the CmdletBinding attribute --- Rules/ProvideVerboseMessage.cs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Rules/ProvideVerboseMessage.cs b/Rules/ProvideVerboseMessage.cs index a69447c0e..fb4b3f15b 100644 --- a/Rules/ProvideVerboseMessage.cs +++ b/Rules/ProvideVerboseMessage.cs @@ -12,10 +12,12 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Management.Automation.Language; using Microsoft.Windows.Powershell.ScriptAnalyzer.Generic; using System.ComponentModel.Composition; using System.Globalization; +using System.Management.Automation; namespace Microsoft.Windows.Powershell.ScriptAnalyzer.BuiltinRules { @@ -57,6 +59,15 @@ public override AstVisitAction VisitFunctionDefinition(FunctionDefinitionAst fun return AstVisitAction.SkipChildren; } + //Write-Verbose is not required for non-advanced functions + if (funcAst.Body == null || funcAst.Body.ParamBlock == null + || funcAst.Body.ParamBlock.Attributes == null || + funcAst.Body.ParamBlock.Parameters == null || + !funcAst.Body.ParamBlock.Attributes.Any(attr => attr.TypeName.GetReflectionType() == typeof(CmdletBindingAttribute))) + { + return AstVisitAction.Continue; + } + var commandAsts = funcAst.Body.FindAll(testAst => testAst is CommandAst, false); bool hasVerbose = false; From 2523307bbc76545b099f312c4ffe48fed2cb0866 Mon Sep 17 00:00:00 2001 From: GoodOlClint Date: Wed, 6 May 2015 21:14:52 -0500 Subject: [PATCH 2/2] Updated unit tests for PSProvideVerboseMessage test --- Tests/Rules/GoodCmdlet.ps1 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Tests/Rules/GoodCmdlet.ps1 b/Tests/Rules/GoodCmdlet.ps1 index 7c1dfa240..a294af4df 100644 --- a/Tests/Rules/GoodCmdlet.ps1 +++ b/Tests/Rules/GoodCmdlet.ps1 @@ -81,4 +81,10 @@ function Get-File if ($pscmdlet.ShouldContinue("Yes", "No")) { } } +} + +#Write-Verbose should not be required because this is not an advanced function +function Get-SimpleFunc +{ + } \ No newline at end of file