add AvoidMultipleTypesParameter Rule#1705
Conversation
|
@hankyi95 please fill out the PR description template |
Rules/Strings.resx
Outdated
| <data name="AvoidMultipleTypesParameterName" xml:space="preserve"> | ||
| <value>AvoidMultipleTypesParameter</value> | ||
| </data> | ||
| </root> No newline at end of file |
There was a problem hiding this comment.
| </root> | |
| </root> | |
| # 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){} No newline at end of file |
There was a problem hiding this comment.
| function F11 ([switch][boolean] $s1, [int] $p1){} | |
| function F11 ([switch][boolean] $s1, [int] $p1){} | |
| $noViolations.Count | Should -Be 0 | ||
| } | ||
| } | ||
| } No newline at end of file |
| @@ -0,0 +1,3 @@ | |||
| function F10 ([int] $s1, [int] $p1){} | |||
|
|
|||
| function F11 ([switch] $s1, [int] $p1){} No newline at end of file | |||
There was a problem hiding this comment.
| function F11 ([switch] $s1, [int] $p1){} | |
| function F11 ([switch] $s1, [int] $p1){} | |
| $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.
Rather than using files and running the analyzer in BeforeAll, I would:
- Use inline scripts and use the
-ScriptDefinitionparameter - Use the
-TestCasesparameter onItto parameterise the diagnostics you expect
Co-authored-by: Robert Holt <rjmholt@gmail.com>
|
|
||
| ## How | ||
|
|
||
| Make each parameter has only 1 type spcifier. |
There was a problem hiding this comment.
| Make each parameter has only 1 type spcifier. | |
| Ensure each parameter has only 1 type specifier. |
|
|
||
| ## Description | ||
|
|
||
| Parameter should not have more than one type specifier. |
There was a problem hiding this comment.
| Parameter should not have more than one type specifier. | |
| Parameters should not have more than one type specifier. |
Rules/AvoidMultipleTypesParameter.cs
Outdated
| /// </summary> | ||
| public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | ||
| { | ||
| if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); |
There was a problem hiding this comment.
| if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage); | |
| if (ast is null) | |
| { | |
| throw new ArgumentNullException(Strings.NullAstErrorMessage); | |
| } |
Rules/AvoidMultipleTypesParameter.cs
Outdated
| 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.
| yield return new DiagnosticRecord( | |
| String.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypesParameterError, paramAst.Name), | |
| paramAst.Name.Extent, GetName(), DiagnosticSeverity.Warning, fileName); | |
| yield return new DiagnosticRecord( | |
| String.Format(CultureInfo.CurrentCulture, Strings.AvoidMultipleTypesParameterError, paramAst.Name), | |
| paramAst.Name.Extent, | |
| GetName(), | |
| DiagnosticSeverity.Warning, | |
| fileName); |
Rules/Strings.resx
Outdated
| <value>When using an explicit process block, no preceding code is allowed, only begin, end and dynamicparams blocks.</value> | ||
| </data> | ||
| <data name="AvoidMultipleTypesParameterCommonName" xml:space="preserve"> | ||
| <value>Avoid Multiple Types Parameter</value> |
There was a problem hiding this comment.
| <value>Avoid Multiple Types Parameter</value> | |
| <value>Avoid multiple type specifiers on parameters.</value> |
| } | ||
| } | ||
|
|
||
| Describe 'AvoidMultipleTypesParameter' { |
There was a problem hiding this comment.
So I would rewrite your test structure like this:
- No
Contexts - Each test scenario is an
It Invoke-ScriptAnalyzeris done in theItrather than in aBeforeAll- The
Ittests the count and properties of all the violations
We could also use a few more tests:
- When no type specifiers are supplied
- When three are
- When
[ref]is used - When an attribute like
[ValidateSet()]is also used
| $Param1, | ||
|
|
||
| [switch] | ||
| $Switch=$False |
There was a problem hiding this comment.
| $Switch=$False | |
| $Switch |
|
|
||
| ## Description | ||
|
|
||
| Parameter should not have more than one type specifier. |
There was a problem hiding this comment.
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?
bergmeister
left a comment
There was a problem hiding this comment.
Just a few comments but looks good from a high level, Rob already pointed out some low level code things to address, I'd be happy to merge after that
…ub.com/hankyi95/PSScriptAnalyzer into hankyi/feature/avoidmultipletypesparam
Rules/AvoidMultipleTypesParameter.cs
Outdated
| /// </summary> | ||
| public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName) | ||
| { | ||
| if (ast == null) |
There was a problem hiding this comment.
| if (ast == null) | |
| if (ast is null) |
| } | ||
|
|
||
| Describe 'AvoidMultipleTypesParameter' { | ||
| it 'Should find 3 violations for paramters have more than one type spceifiers' { |
There was a problem hiding this comment.
| it 'Should find 3 violations for paramters have more than one type spceifiers' { | |
| It 'Should find 3 violations for paramters have more than one type spceifiers' { |
Below as well
| $def = @' | ||
| function F1 ($s1, $p1){} | ||
| function F2 ([int] $s2, [int] $p2){} | ||
| function F3 ([int][switch] $s3, [int] $p3){} | ||
| function F4 ([int][ref] $s4, [int] $p4){} | ||
| function F5 ([int][switch][boolean] $s5, [int] $p5){} | ||
| '@ |
There was a problem hiding this comment.
This is the kind of thing that the -TestCases parameter on It is designed for.
Here's a simple example, and a more sophisticated example.
Each example should be its own test, but each test should assert (with Should):
- The number of violations
- What rule reported the violation
- What the violation is
rjmholt
left a comment
There was a problem hiding this comment.
Just realised the rule name AvoidMultipleTypesParameter doesn't quite work.
My suggestion is AvoidMultipleTypeAttributes (means we could generalise the rule later), but doesn't have to be that. Some other possibilities:
AvoidMultipleTypeSpecifiersAvoidConflictingVariableAttributes(rule could be enhanced to deal withValidateSettoo...)
| @@ -0,0 +1,50 @@ | |||
| # AvoidMultipleTypesParameter | |||
There was a problem hiding this comment.
| # AvoidMultipleTypesParameter | |
| # AvoidMultipleTypeAttributes |
RuleDocumentation/README.md
Outdated
| |[AvoidGlobalVars](./AvoidGlobalVars.md) | Warning | | | ||
| |[AvoidInvokingEmptyMembers](./AvoidInvokingEmptyMembers.md) | Warning | | | ||
| |[AvoidLongLines](./AvoidLongLines.md) | Warning | | | ||
| |[AvoidMultipleTypesParameter](./AvoidMultipleTypesParameter.md) | Warning | | |
There was a problem hiding this comment.
| |[AvoidMultipleTypesParameter](./AvoidMultipleTypesParameter.md) | Warning | | | |
| |[AvoidMultipleTypeAttributes](./AvoidMultipleTypesParameter.md) | Warning | | |
Rules/AvoidMultipleTypesParameter.cs
Outdated
| #if !CORECLR | ||
| [Export(typeof(IScriptRule))] | ||
| #endif | ||
| public sealed class AvoidMultipleTypesParameter : IScriptRule |
There was a problem hiding this comment.
| public sealed class AvoidMultipleTypesParameter : IScriptRule | |
| public sealed class AvoidMultipleTypeAttributesRule: IScriptRule |
Rules/Strings.resx
Outdated
| <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> | ||
| <data name="AvoidMultipleTypesParameterCommonName" xml:space="preserve"> |
There was a problem hiding this comment.
| <data name="AvoidMultipleTypesParameterCommonName" xml:space="preserve"> | |
| <data name="AvoidMultipleTypeAttributesCommonName" xml:space="preserve"> |
Rules/Strings.resx
Outdated
| <data name="AvoidMultipleTypesParameterCommonName" xml:space="preserve"> | ||
| <value>Avoid multiple type specifiers on parameters</value> | ||
| </data> | ||
| <data name="AvoidMultipleTypesParameterDescription" xml:space="preserve"> |
There was a problem hiding this comment.
| <data name="AvoidMultipleTypesParameterDescription" xml:space="preserve"> | |
| <data name="AvoidMultipleTypeAttributesDescription" xml:space="preserve"> |
Rules/Strings.resx
Outdated
| <data name="AvoidMultipleTypesParameterDescription" xml:space="preserve"> | ||
| <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.
| <data name="AvoidMultipleTypesParameterError" xml:space="preserve"> | |
| <data name="AvoidMultipleTypeAttributesError" xml:space="preserve"> |
Rules/Strings.resx
Outdated
| <data name="AvoidMultipleTypesParameterError" xml:space="preserve"> | ||
| <value>Parameter '{0}' has more than one type specifier.</value> | ||
| </data> | ||
| <data name="AvoidMultipleTypesParameterName" xml:space="preserve"> |
There was a problem hiding this comment.
| <data name="AvoidMultipleTypesParameterName" xml:space="preserve"> | |
| <data name="AvoidMultipleTypeAttributesName" xml:space="preserve"> |
Rules/Strings.resx
Outdated
| <value>Parameter '{0}' has more than one type specifier.</value> | ||
| </data> | ||
| <data name="AvoidMultipleTypesParameterName" xml:space="preserve"> | ||
| <value>AvoidMultipleTypesParameter</value> |
There was a problem hiding this comment.
| <value>AvoidMultipleTypesParameter</value> | |
| <value>AvoidMultipleTypeAttributes</value> |
| # Licensed under the MIT License. | ||
|
|
||
| BeforeAll { | ||
| $ruleName = "PSAvoidMultipleTypesParameter" |
There was a problem hiding this comment.
| $ruleName = "PSAvoidMultipleTypesParameter" | |
| $ruleName = "PSAvoidMultipleTypeAttributes" |
…ub.com/hankyi95/PSScriptAnalyzer into hankyi/feature/avoidmultipletypesparam
PR Summary
PR Checklist
.cs,.ps1and.psm1files have the correct copyright headerWIP:to the beginning of the title and remove the prefix when the PR is ready.