Skip to content

Commit c997f54

Browse files
committed
**opt-in by default.**
1 parent 694fc57 commit c997f54

4 files changed

Lines changed: 122 additions & 28 deletions

File tree

Rules/InvalidMultiDotValue.cs

Lines changed: 67 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,23 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2222
/// which is likely an attempt to construct a version number (e.g., 1.2.3)
2323
/// that is not properly quoted and thus misinterpreted as a double with member access.
2424
/// </summary>
25-
public class InvalidMultiDotValue : IScriptRule
25+
public class InvalidMultiDotValue : ConfigurableRule
2626
{
27+
28+
/// <summary>
29+
/// Construct an object of InvalidMultiDotValue type.
30+
/// </summary>
31+
public InvalidMultiDotValue() {
32+
Enable = false;
33+
}
34+
2735
/// <summary>
28-
/// Analyzes the PowerShell unquoted values that contain multiple dots.
36+
/// Analyzes the given ast to find the [violation]
2937
/// </summary>
30-
/// <param name="ast">The PowerShell Abstract Syntax Tree to analyze.</param>
31-
/// <param name="fileName">The name of the file being analyzed (for diagnostic reporting).</param>
32-
/// <returns>A collection of diagnostic records for each violation.</returns>
33-
public IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
38+
/// <param name="ast">AST to be analyzed. This should be non-null</param>
39+
/// <param name="fileName">Name of file that corresponds to the input AST.</param>
40+
/// <returns>A an enumerable type containing the violations</returns>
41+
public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string fileName)
3442
{
3543
if (ast == null) throw new ArgumentNullException(Strings.NullAstErrorMessage);
3644

@@ -81,20 +89,66 @@ memberAst.Member is ConstantExpressionAst constantAst &&
8189
}
8290
}
8391

84-
public string GetCommonName() => Strings.InvalidMultiDotValueCommonName;
92+
/// <summary>
93+
/// Retrieves the common name of this rule.
94+
/// </summary>
95+
public override string GetCommonName()
96+
{
97+
return string.Format(CultureInfo.CurrentCulture, Strings.InvalidMultiDotValueCommonName);
98+
}
8599

86-
public string GetDescription() => Strings.InvalidMultiDotValueDescription;
100+
/// <summary>
101+
/// Retrieves the description of this rule.
102+
/// </summary>
103+
public override string GetDescription()
104+
{
105+
return string.Format(CultureInfo.CurrentCulture, Strings.InvalidMultiDotValueDescription);
106+
}
87107

88-
public string GetName() => string.Format(
108+
/// <summary>
109+
/// Retrieves the name of this rule.
110+
/// </summary>
111+
public override string GetName()
112+
{
113+
return string.Format(
89114
CultureInfo.CurrentCulture,
90115
Strings.NameSpaceFormat,
91116
GetSourceName(),
92117
Strings.InvalidMultiDotValueName);
118+
}
93119

94-
public RuleSeverity GetSeverity() => RuleSeverity.Error;
120+
/// <summary>
121+
/// Retrieves the severity of the rule: error, warning or information.
122+
/// </summary>
123+
public override RuleSeverity GetSeverity()
124+
{
125+
return RuleSeverity.Warning;
126+
}
95127

96-
public string GetSourceName() => Strings.SourceName;
128+
/// <summary>
129+
/// Gets the severity of the returned diagnostic record: error, warning, or information.
130+
/// </summary>
131+
/// <returns></returns>
132+
public DiagnosticSeverity GetDiagnosticSeverity()
133+
{
134+
return DiagnosticSeverity.Warning;
135+
}
97136

98-
public SourceType GetSourceType() => SourceType.Builtin;
137+
/// <summary>
138+
/// Retrieves the name of the module/assembly the rule is from.
139+
/// </summary>
140+
public override string GetSourceName()
141+
{
142+
return string.Format(CultureInfo.CurrentCulture, Strings.SourceName);
143+
}
144+
145+
/// <summary>
146+
/// Retrieves the type of the rule, Builtin, Managed or Module.
147+
/// </summary>
148+
public override SourceType GetSourceType()
149+
{
150+
return SourceType.Builtin;
151+
}
99152
}
100-
}
153+
}
154+

Tests/Rules/InvalidMultiDotValue.tests.ps1

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ BeforeAll {
1111
}
1212

1313
Describe "InvalidMultiDotValue" {
14+
15+
BeforeAll {
16+
$Settings = @{
17+
IncludeRules = @($ruleName)
18+
Rules = @{ $ruleName = @{ Enable = $true } }
19+
}
20+
}
21+
1422
Context "Violates" {
1523
It "3 version components" {
1624
$scriptDefinition = { $version = 1.2.3 }.ToString()
17-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
25+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
1826
$violations.Count | Should -Be 1
1927
$violations.Severity | Should -Be Error
2028
$violations.Extent.Text | Should -Be '1.2.3'
@@ -26,7 +34,7 @@ Describe "InvalidMultiDotValue" {
2634

2735
It "4 version components" {
2836
$scriptDefinition = { $version = 1.2.3.4 }.ToString()
29-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
37+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
3038
$violations.Count | Should -Be 1
3139
$violations.Severity | Should -Be Error
3240
$violations.Extent.Text | Should -Be '1.2.3.4'
@@ -39,7 +47,7 @@ Describe "InvalidMultiDotValue" {
3947

4048
It "With class initializer" {
4149
$scriptDefinition = { $version = [Version]1.2.3 }.ToString()
42-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
50+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
4351
$violations.Count | Should -Be 1
4452
$violations.Severity | Should -Be Error
4553
$violations.Extent.Text | Should -Be '1.2.3'
@@ -56,7 +64,7 @@ Describe "InvalidMultiDotValue" {
5664
)
5765
Write-Verbose $version
5866
}.ToString()
59-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
67+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
6068
$violations.Count | Should -Be 1
6169
$violations.Severity | Should -Be Error
6270
$violations.Extent.Text | Should -Be '1.2.3'
@@ -71,7 +79,7 @@ Describe "InvalidMultiDotValue" {
7179
# is expected because this is the more commonly used type.
7280
It "IP Address" {
7381
$scriptDefinition = { $IP = [System.Net.IPAddress]127.0.0.1 }.ToString()
74-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
82+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
7583
$violations.Count | Should -Be 1
7684
$violations.Severity | Should -Be Error
7785
$violations.Extent.Text | Should -Be '127.0.0.1'
@@ -85,19 +93,19 @@ Describe "InvalidMultiDotValue" {
8593
Context "Compliant" {
8694
It "From string" {
8795
$scriptDefinition = { $Version = [Version]'1.2.3' }.ToString()
88-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
96+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
8997
$violations | Should -BeNullOrEmpty
9098
}
9199

92100
It "From version components" {
93101
$scriptDefinition = { $Version = [Version]::new(1, 2, 3, 4) }.ToString()
94-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
102+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
95103
$violations | Should -BeNullOrEmpty
96104
}
97105

98106
It "From (bare) double" {
99107
$scriptDefinition = { $Version = [Version]1.2 }.ToString()
100-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
108+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
101109
$violations | Should -BeNullOrEmpty
102110
}
103111

@@ -108,7 +116,23 @@ Describe "InvalidMultiDotValue" {
108116
$intKeys = @{ 1 = @{ 2 = @{ 3 = @{ 4 = 'test' } } } }
109117
$intKeys.1.2.3.4
110118
}.ToString()
111-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
119+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
120+
$violations | Should -BeNullOrEmpty
121+
}
122+
}
123+
124+
Context "Disabled" {
125+
126+
BeforeAll {
127+
$Settings = @{
128+
IncludeRules = @($ruleName)
129+
Rules = @{ $ruleName = @{ Enable = $false } }
130+
}
131+
}
132+
133+
It "ConvertFrom-SecureString -AsPlainText" {
134+
$scriptDefinition = { $version = 1.2.3 }.ToString()
135+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
112136
$violations | Should -BeNullOrEmpty
113137
}
114138
}
@@ -121,7 +145,7 @@ Describe "InvalidMultiDotValue" {
121145
$version = 1.2.3
122146
$IP = [System.Net.IPAddress]127.0.0.1
123147
}.ToString()
124-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
148+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
125149
$violations | Should -BeNullOrEmpty
126150
}
127151

@@ -131,7 +155,7 @@ Describe "InvalidMultiDotValue" {
131155
param()
132156
$version = 1.2.3
133157
}.ToString()
134-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
158+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
135159
$violations | Should -BeNullOrEmpty
136160
}
137161

@@ -141,7 +165,7 @@ Describe "InvalidMultiDotValue" {
141165
param()
142166
$IP = [System.Net.IPAddress]127.0.0.1
143167
}.ToString()
144-
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -IncludeRule @($ruleName)
168+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $scriptDefinition -Settings $Settings
145169
$violations | Should -BeNullOrEmpty
146170
}
147171
}
@@ -154,13 +178,13 @@ Describe "InvalidMultiDotValue" {
154178

155179
It "Version" {
156180
Set-Content -LiteralPath $tempFile -Value {$version = 1.2.3}.ToString() -NoNewLine
157-
$violations = Invoke-ScriptAnalyzer -Path $tempFile -fix
181+
$violations = Invoke-ScriptAnalyzer -Path $tempFile -Settings $Settings -fix
158182
Get-Content -LiteralPath $tempFile -Raw | Should -Be {$version = '1.2.3'}.ToString()
159183
}
160184

161185
It "IP Address" {
162186
Set-Content -LiteralPath $tempFile -Value {$IP = [System.Net.IPAddress]127.0.0.1}.ToString() -NoNewLine
163-
$violations = Invoke-ScriptAnalyzer -Path $tempFile -fix
187+
$violations = Invoke-ScriptAnalyzer -Path $tempFile -Settings $Settings -fix
164188
Get-Content -LiteralPath $tempFile -Raw | Should -Be {$IP = [System.Net.IPAddress]'127.0.0.1'}.ToString()
165189
}
166190
}

docs/Rules/InvalidMultiDotValue.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,19 @@ $version = [Version]'1.2.3'
4444
# Use type constructor method
4545
$version = [Version]::new(1, 2, 3)
4646
```
47+
48+
## Configuration
49+
50+
```powershell
51+
Rules = @{
52+
PSAvoidExclaimOperator = @{
53+
Enable = $true
54+
}
55+
}
56+
```
57+
58+
### Parameters
59+
60+
- `Enable`: **bool** (Default value is `$false`)
61+
62+
Enable or disable the rule during ScriptAnalyzer invocation.

docs/Rules/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ The PSScriptAnalyzer contains the following rule definitions.
4848
| [DSCUseIdenticalMandatoryParametersForDSC](./DSCUseIdenticalMandatoryParametersForDSC.md) | Error | Yes | |
4949
| [DSCUseIdenticalParametersForDSC](./DSCUseIdenticalParametersForDSC.md) | Error | Yes | |
5050
| [DSCUseVerboseMessageInDSCResource](./DSCUseVerboseMessageInDSCResource.md) | Error | Yes | |
51-
| [InvalidMultiDotValue](./InvalidMultiDotValue.md) | Error | Yes | |
51+
| [InvalidMultiDotValue](./InvalidMultiDotValue.md) | Error | No | Yes |
5252
| [MisleadingBacktick](./MisleadingBacktick.md) | Warning | Yes | |
5353
| [MissingModuleManifestField](./MissingModuleManifestField.md) | Warning | Yes | |
5454
| [PlaceCloseBrace](./PlaceCloseBrace.md) | Warning | No | Yes |

0 commit comments

Comments
 (0)