-
Notifications
You must be signed in to change notification settings - Fork 394
Add rule for missing process block when command supports the pipeline #1373
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
Changes from 17 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1ef888d
Add rule for missing process block #1368
mattmcnabb 1ad72e7
Fix build by regenerating strongly typed files by running '.\New-Stro…
bergmeister 794366b
Update strongly typed resources via Visual Studio
bergmeister 86673bf
minimise diff in resx file
bergmeister dd42829
refactor UseProcessBlockForPipelineCommands rule
mattmcnabb 32916c2
add tests and docs for UseProcessBlockForPipelineCommands rule
mattmcnabb fe2c8e7
Fix logic non-pipeline function and add test case
mattmcnabb 585a735
fixing some test failures
mattmcnabb b499d9e
incrememt number of expected rules
mattmcnabb 2df5e70
fix style suggestions for PR #1373
mattmcnabb fbe8bc4
fix rule readme.md
mattmcnabb 5921add
clean diff for Strings.resx to avoid future merge conflicts
79aaf0f
Fix last 2 rule documentation test failures by fixing markdown link
850b457
Update Rules/Strings.resx
mattmcnabb 61220e7
Update Rules/Strings.resx
mattmcnabb fcae260
Update Rules/Strings.resx
mattmcnabb 504fdd6
empty-commit to re-trigger CI due to sporadic failure
a593ff7
Update Rules/Strings.resx
mattmcnabb 4a358a0
Update Rules/Strings.resx
mattmcnabb 707f02d
corrections to use process block rule and tests - candidate for merge
mattmcnabb c7a2531
More style fixes for #1373
mattmcnabb a7ed122
update localized strings
mattmcnabb a28b4d2
replace implicitly typed vars
mattmcnabb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# UseProcessBlockForPipelineCommand | ||
|
||
**Severity Level: Warning** | ||
|
||
## Description | ||
|
||
Functions that support pipeline input should always handle parameter input in a process block. Unexpected behavior can result if input is handled directly in the body of a function where parameters declare pipeline support. | ||
|
||
## Example | ||
|
||
### Wrong | ||
|
||
``` PowerShell | ||
Function Get-Number | ||
{ | ||
[CmdletBinding()] | ||
Param( | ||
[Parameter(ValueFromPipeline)] | ||
[int] | ||
$Number | ||
) | ||
|
||
$Number | ||
} | ||
``` | ||
|
||
#### Result | ||
|
||
``` | ||
PS C:\> 1..5 | Get-Number | ||
5 | ||
``` | ||
|
||
### Correct | ||
|
||
``` PowerShell | ||
Function Get-Number | ||
{ | ||
[CmdletBinding()] | ||
Param( | ||
[Parameter(ValueFromPipeline)] | ||
[int] | ||
$Number | ||
) | ||
|
||
process | ||
{ | ||
$Number | ||
} | ||
} | ||
``` | ||
|
||
#### Result | ||
|
||
``` | ||
PS C:\> 1..5 | Get-Number | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
``` |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
using System.Collections.Generic; | ||
using System.Management.Automation.Language; | ||
using Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic; | ||
#if !CORECLR | ||
using System.ComponentModel.Composition; | ||
#endif | ||
using System.Globalization; | ||
|
||
namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules | ||
{ | ||
#if !CORECLR | ||
[Export(typeof(IScriptRule))] | ||
#endif | ||
public class UseProcessBlockForPipelineCommand : IScriptRule | ||
{ | ||
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | ||
{ | ||
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
IEnumerable<Ast> functionAsts = ast.FindAll(testAst => testAst is FunctionDefinitionAst, true); | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
foreach (FunctionDefinitionAst funcAst in functionAsts) | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
( | ||
funcAst.Body.ParamBlock == null | ||
|| funcAst.Body.ParamBlock.Attributes == null | ||
|| funcAst.Body.ParamBlock.Parameters == null | ||
|| funcAst.Body.ProcessBlock != null | ||
) | ||
{ continue; } | ||
|
||
foreach (var paramAst in funcAst.Body.ParamBlock.Parameters) | ||
{ | ||
foreach (var paramAstAttribute in paramAst.Attributes) | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (!(paramAstAttribute is AttributeAst)) { continue; } | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var namedArguments = (paramAstAttribute as AttributeAst).NamedArguments; | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (namedArguments == null) { continue; } | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
foreach (NamedAttributeArgumentAst namedArgument in namedArguments) | ||
{ | ||
if | ||
( | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
!namedArgument.ArgumentName.Equals("valuefrompipeline", StringComparison.OrdinalIgnoreCase) | ||
&& !namedArgument.ArgumentName.Equals("valuefrompipelinebypropertyname", StringComparison.OrdinalIgnoreCase) | ||
) | ||
{ continue; } | ||
|
||
yield return new DiagnosticRecord( | ||
string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandError, paramAst.Name.VariablePath.UserPath), | ||
paramAst.Name.Extent, | ||
GetName(), | ||
DiagnosticSeverity.Warning, | ||
fileName, | ||
paramAst.Name.VariablePath.UserPath | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public string GetName() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.UseProcessBlockForPipelineCommandName); | ||
} | ||
|
||
public string GetCommonName() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandCommonName); | ||
} | ||
|
||
public string GetDescription() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.UseProcessBlockForPipelineCommandDescription); | ||
} | ||
|
||
public SourceType GetSourceType() | ||
{ | ||
return SourceType.Builtin; | ||
} | ||
|
||
public RuleSeverity GetSeverity() | ||
{ | ||
return RuleSeverity.Warning; | ||
} | ||
|
||
public string GetSourceName() | ||
{ | ||
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
Describe "UseProcessBlockForPipelineCommand" { | ||
BeforeAll { | ||
$RuleName = 'PSUseProcessBlockForPipelineCommand' | ||
$NoProcessBlock = 'function BadFunc1 { [CmdletBinding()] param ([Parameter(ValueFromPipeline)]$Param1) }' | ||
$NoProcessBlockByPropertyName = 'function $BadFunc2 { [CmdletBinding()] param ([Parameter(ValueFromPipelineByPropertyName)]$Param1) }' | ||
$HasProcessBlock = 'function GoodFunc1 { [CmdletBinding()] param ([Parameter(ValueFromPipeline)]$Param1) process { } }' | ||
$HasProcessBlockByPropertyName = 'function GoodFunc2 { [CmdletBinding()] param ([Parameter(ValueFromPipelineByPropertyName)]$Param1) process { } }' | ||
$NoAttribute = 'function GoodFunc3 { [CmdletBinding()] param ([Parameter()]$Param1) }' | ||
$HasTypeDeclaration = 'function GoodFunc3 { [CmdletBinding()] param ([Parameter()][string]$Param1) }' | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
Context "When there are violations" { | ||
$Cases = @( | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@{ScriptDefinition = $NoProcessBlock; Name = "NoProcessBlock"} | ||
@{ScriptDefinition = $NoProcessBlockByPropertyName; Name = "NoProcessBlockByPropertyName"} | ||
) | ||
It "has 1 violation for function <Name>" { | ||
param ($ScriptDefinition) | ||
|
||
Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName | Should -Not -BeNullOrEmpty | ||
} -TestCases $Cases | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
Context "When there are no violations" { | ||
$Cases = @( | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@{ScriptDefinition = $HasProcessBlock; Name = "HasProcessBlock" } | ||
@{ScriptDefinition = $HasProcessBlockByPropertyName; Name = "HasProcessBlockByPropertyName" } | ||
@{ScriptDefinition = $NoAttribute; Name = "NoAttribute" } | ||
@{ScriptDefinition = $HasTypeDeclaration; Name = "HasTypeDeclaration"} | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
It "has no violations for function <Name>" { | ||
param ($ScriptDefinition) | ||
|
||
Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName | Should -BeNullOrEmpty | ||
} -TestCases $Cases | ||
mattmcnabb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.