-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AvoidMultipleTypeAttributes rule (#1705)
- Loading branch information
Showing
7 changed files
with
247 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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,50 @@ | ||
# AvoidMultipleTypeAttributes | ||
|
||
**Severity Level: Warning** | ||
|
||
## Description | ||
|
||
Parameters should not have more than one type specifier. Multiple type specifiers on parameters will cause a runtime error. | ||
|
||
## How | ||
|
||
Ensure each parameter has only 1 type specifier. | ||
|
||
## Example | ||
|
||
### Wrong | ||
|
||
``` PowerShell | ||
function Test-Script | ||
{ | ||
[CmdletBinding()] | ||
Param | ||
( | ||
[String] | ||
$Param1, | ||
[switch] | ||
[bool] | ||
$Switch | ||
) | ||
... | ||
} | ||
``` | ||
|
||
### Correct | ||
|
||
``` PowerShell | ||
function Test-Script | ||
{ | ||
[CmdletBinding()] | ||
Param | ||
( | ||
[String] | ||
$Param1, | ||
[switch] | ||
$Switch | ||
) | ||
... | ||
} | ||
``` |
This file contains 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 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,104 @@ | ||
// 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> | ||
/// AvoidMultipleTypeAttributes: Check that parameter does not be assigned to multiple types. | ||
/// </summary> | ||
#if !CORECLR | ||
[Export(typeof(IScriptRule))] | ||
#endif | ||
public sealed class AvoidMultipleTypeAttributes : IScriptRule | ||
{ | ||
/// <summary> | ||
/// AvoidMultipleTypeAttributes: Check that parameter does not be assigned to multiple types. | ||
/// </summary> | ||
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | ||
{ | ||
if (ast is null) | ||
{ | ||
throw new ArgumentNullException(Strings.NullAstErrorMessage); | ||
} | ||
|
||
// Finds all ParamAsts. | ||
IEnumerable<Ast> paramAsts = ast.FindAll(testAst => testAst is ParameterAst, searchNestedScriptBlocks: true); | ||
|
||
// 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.AvoidMultipleTypeAttributesError, paramAst.Name), | ||
paramAst.Name.Extent, | ||
GetName(), | ||
DiagnosticSeverity.Warning, | ||
fileName); | ||
} | ||
} | ||
} | ||
|
||
/// <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.AvoidMultipleTypeAttributesName); | ||
} | ||
|
||
/// <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.AvoidMultipleTypeAttributesCommonName); | ||
} | ||
|
||
/// <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.AvoidMultipleTypeAttributesDescription); | ||
} | ||
|
||
/// <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.
Oops, something went wrong.
This file contains 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 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 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,42 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
BeforeAll { | ||
$ruleName = "AvoidMultipleTypeAttributes" | ||
|
||
$settings = @{ | ||
IncludeRules = @($ruleName) | ||
} | ||
} | ||
|
||
Describe 'AvoidMultipleTypeAttributes' { | ||
It 'Correctly diagnoses and corrects <Script>' -TestCases @( | ||
@{ Script = 'function F1 ($s1, $p1){}' } | ||
@{ Script = 'function F2 ([int] $s2, [int] $p2){}' } | ||
@{ Script = 'function F3 ([int][switch] $s3, [int] $p3){}';Extent = @{ StartCol = 28; EndCol = 31 }; Message = 'Parameter ''$s3'' has more than one type specifier.' } | ||
@{ Script = 'function F4 ([int][ref] $s4, [int] $p4){}';Extent = @{ StartCol = 25; EndCol = 28 }; Message = 'Parameter ''$s4'' has more than one type specifier.' } | ||
@{ Script = 'function F5 ([int][switch][boolean] $s5, [int] $p5){}';Extent = @{ StartCol = 37; EndCol = 40 }; Message = 'Parameter ''$s5'' has more than one type specifier.' } | ||
@{ Script = 'function F6 ([ValidateSet()][int] $s6, [int] $p6){}' } | ||
@{ Script = 'function F7 ([Parameter(Mandatory=$true)][ValidateSet()][int] $s7, [int] $p7){}' } | ||
) { | ||
param([string]$Script, $Extent, $Message) | ||
|
||
$diagnostics = Invoke-ScriptAnalyzer -ScriptDefinition $Script | ||
|
||
if (-not $Extent) | ||
{ | ||
$diagnostics | Should -BeNullOrEmpty | ||
return | ||
} | ||
|
||
$expectedStartLine = if ($Extent.StartLine) { $Extent.StartLine } else { 1 } | ||
$expectedEndLine = if ($Extent.EndLine) { $Extent.EndLine } else { 1 } | ||
|
||
$diagnostics.Extent.StartLineNumber | Should -BeExactly $expectedStartLine | ||
$diagnostics.Extent.EndLineNumber | Should -BeExactly $expectedEndLine | ||
$diagnostics.Extent.StartColumnNumber | Should -BeExactly $Extent.StartCol | ||
$diagnostics.Extent.EndColumnNumber | Should -BeExactly $Extent.EndCol | ||
|
||
$diagnostics.Message | Should -BeExactly $Message | ||
} | ||
} |