Skip to content

Only Trigger PSProvideVerboseMessage in Advanced Scripts or Functions #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Rules/ProvideVerboseMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions Tests/Rules/GoodCmdlet.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

}