-
Notifications
You must be signed in to change notification settings - Fork 395
add AvoidMultipleTypesParameter Rule #1705
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
Changes from 1 commit
9cac970
6f3e303
5498863
00b03cc
83c286e
2c5c3e8
28caac8
9f8aa7e
1d3705f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,50 @@ | ||||||
# AvoidMultipleTypesParameter | ||||||
|
||||||
**Severity Level: Warning** | ||||||
|
||||||
## Description | ||||||
|
||||||
Parameter should not have more than one type specifier. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we also add a description why we do not want to have more than one type, i.e. what is the impact? \Just a runtime error or potentially also unpredictable or unintuitive behavior? |
||||||
|
||||||
## How | ||||||
|
||||||
Make each parameter has only 1 type spcifier. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
## Example | ||||||
|
||||||
### Wrong | ||||||
|
||||||
``` PowerShell | ||||||
function Test-Script | ||||||
{ | ||||||
[CmdletBinding()] | ||||||
Param | ||||||
( | ||||||
[String] | ||||||
$Param1, | ||||||
|
||||||
[switch] | ||||||
[boolean] | ||||||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
$Switch=$True | ||||||
) | ||||||
... | ||||||
} | ||||||
``` | ||||||
|
||||||
### Correct | ||||||
|
||||||
``` PowerShell | ||||||
function Test-Script | ||||||
{ | ||||||
[CmdletBinding()] | ||||||
Param | ||||||
( | ||||||
[String] | ||||||
$Param1, | ||||||
|
||||||
[switch] | ||||||
$Switch=$False | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) | ||||||
... | ||||||
} | ||||||
``` |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -13,6 +13,7 @@ | |||||
|[AvoidGlobalVars](./AvoidGlobalVars.md) | Warning | | | ||||||
|[AvoidInvokingEmptyMembers](./AvoidInvokingEmptyMembers.md) | Warning | | | ||||||
|[AvoidLongLines](./AvoidLongLines.md) | Warning | | | ||||||
|[AvoidMultipleTypesParameter](./AvoidMultipleTypesParameter.md) | Warning | | | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|[AvoidOverwritingBuiltInCmdlets](./AvoidOverwritingBuiltInCmdlets.md) | Warning | | | ||||||
|[AvoidNullOrEmptyHelpMessageAttribute](./AvoidNullOrEmptyHelpMessageAttribute.md) | Warning | | | ||||||
|[AvoidShouldContinueWithoutForce](./AvoidShouldContinueWithoutForce.md) | Warning | | | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,98 @@ | ||||||||||||||||||||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||||||||||||||||||||
// Licensed under the MIT License. | ||||||||||||||||||||
|
||||||||||||||||||||
using System; | ||||||||||||||||||||
using System.Collections.Generic; | ||||||||||||||||||||
using System.Linq; | ||||||||||||||||||||
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 | ||||||||||||||||||||
{ | ||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// AvoidMultipleTypesParameter: Check that parameter does not be assigned to multiple types. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
#if !CORECLR | ||||||||||||||||||||
[Export(typeof(IScriptRule))] | ||||||||||||||||||||
#endif | ||||||||||||||||||||
public sealed class AvoidMultipleTypesParameter : IScriptRule | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
{ | ||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// AvoidMultipleTypesParameter: Check that parameter does not be assigned to multiple types. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | ||||||||||||||||||||
{ | ||||||||||||||||||||
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
|
||||||||||||||||||||
// Finds all ParamAsts. | ||||||||||||||||||||
IEnumerable<Ast> paramAsts = ast.FindAll(testAst => testAst is ParameterAst, true); | ||||||||||||||||||||
hankyi95 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||
|
||||||||||||||||||||
// Iterates all ParamAsts and check the number of its types. | ||||||||||||||||||||
foreach (ParameterAst paramAst in paramAsts) | ||||||||||||||||||||
{ | ||||||||||||||||||||
if (paramAst.Attributes.Where(typeAst => typeAst is TypeConstraintAst).Count() > 1) | ||||||||||||||||||||
{ | ||||||||||||||||||||
yield return new DiagnosticRecord( | ||||||||||||||||||||
String.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypesParameterError, paramAst.Name), | ||||||||||||||||||||
paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName); | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// GetName: Retrieves the name of this rule. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
/// <returns>The name of this rule</returns> | ||||||||||||||||||||
public string GetName() | ||||||||||||||||||||
{ | ||||||||||||||||||||
return string.Format(CultureInfo.CurrentCulture, Strings.NameSpaceFormat, GetSourceName(), Strings.AvoidMultipleTypesParameterName); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// GetCommonName: Retrieves the common name of this rule. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
/// <returns>The common name of this rule</returns> | ||||||||||||||||||||
public string GetCommonName() | ||||||||||||||||||||
{ | ||||||||||||||||||||
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypesParameterCommonName); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// GetDescription: Retrieves the description of this rule. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
/// <returns>The description of this rule</returns> | ||||||||||||||||||||
public string GetDescription() | ||||||||||||||||||||
{ | ||||||||||||||||||||
return string.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypesParameterDescription); | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// GetSourceType: Retrieves the type of the rule, Builtin, Managed or Module. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
public SourceType GetSourceType() | ||||||||||||||||||||
{ | ||||||||||||||||||||
return SourceType.Builtin; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// GetSeverity: Retrieves the severity of the rule: error, warning or information. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
/// <returns></returns> | ||||||||||||||||||||
public RuleSeverity GetSeverity() | ||||||||||||||||||||
{ | ||||||||||||||||||||
return RuleSeverity.Warning; | ||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
/// <summary> | ||||||||||||||||||||
/// GetSourceName: Retrieves the name of the module/assembly the rule is from. | ||||||||||||||||||||
/// </summary> | ||||||||||||||||||||
public string GetSourceName() | ||||||||||||||||||||
{ | ||||||||||||||||||||
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName); | ||||||||||||||||||||
} | ||||||||||||||||||||
} | ||||||||||||||||||||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -1152,4 +1152,16 @@ | |||||||
<data name="InvalidSyntaxAroundProcessBlockError" xml:space="preserve"> | ||||||||
<value>When using an explicit process block, no preceding code is allowed, only begin, end and dynamicparams blocks.</value> | ||||||||
</data> | ||||||||
</root> | ||||||||
<data name="AvoidMultipleTypesParameterCommonName" xml:space="preserve"> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
<value>Avoid Multiple Types Parameter</value> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
</data> | ||||||||
<data name="AvoidMultipleTypesParameterDescription" xml:space="preserve"> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
<value>Prameter should not have more than one type specifier.</value> | ||||||||
</data> | ||||||||
<data name="AvoidMultipleTypesParameterError" xml:space="preserve"> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
<value>Parameter '{0}' has more than one type specifier.</value> | ||||||||
</data> | ||||||||
<data name="AvoidMultipleTypesParameterName" xml:space="preserve"> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
<value>AvoidMultipleTypesParameter</value> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
</data> | ||||||||
</root> | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,7 @@ | ||||||||
# Double typing is not allowed | ||||||||
function F10 ([int] [switch] $s1, [int] $p1){} | ||||||||
|
||||||||
# Double typing is not allowed even for switch and boolean, because: | ||||||||
# switch maps to System.Management.Automation.SwitchParameter | ||||||||
# boolean maps to System.Boolean | ||||||||
function F11 ([switch][boolean] $s1, [int] $p1){} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,25 @@ | ||||||||
BeforeAll { | ||||||||
$violationMessage = 'Parameter ''\$s1'' has more than one type specifier.' | ||||||||
$violationName = "PSAvoidMultipleTypesParameter" | ||||||||
$violations = Invoke-ScriptAnalyzer $PSScriptRoot\AvoidMultipleTypesParameter.ps1 | Where-Object {$_.RuleName -eq $violationName} | ||||||||
$noViolations = Invoke-ScriptAnalyzer $PSScriptRoot\AvoidMultipleTypesParameterNoViolations.ps1 | Where-Object {$_.RuleName -eq $violationName} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than using files and running the analyzer in
|
||||||||
} | ||||||||
|
||||||||
Describe "AvoidMultipleTypesParameter" { | ||||||||
Context "When there are violations" { | ||||||||
It "has two AvoidMultipleTypesParameter violations" { | ||||||||
$violations.Count | Should -Be 2 | ||||||||
} | ||||||||
|
||||||||
It "has the correct description message" { | ||||||||
$violations[0].Message | Should -Match $violationMessage | ||||||||
$violations[1].Message | Should -Match $violationMessage | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
Context "When there are no violations" { | ||||||||
It "returns no violations" { | ||||||||
$noViolations.Count | Should -Be 0 | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,3 @@ | ||||||||
function F10 ([int] $s1, [int] $p1){} | ||||||||
|
||||||||
function F11 ([switch] $s1, [int] $p1){} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.