Skip to content

Commit a415198

Browse files
authored
Fix ps3 syntax check (#1395)
* Remove erroneous ps3 dynamic member warning * Upgrade compatible syntax rule to emit errors * Fix tests * Fix last test * Fix tests to account for severity change
1 parent d5cb76f commit a415198

File tree

3 files changed

+7
-66
lines changed

3 files changed

+7
-66
lines changed

Rules/CompatibilityRules/UseCompatibleSyntax.cs

+2-30
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class UseCompatibleSyntax : ConfigurableRule
4949
/// <summary>
5050
/// The severity of diagnostics generated by this rule.
5151
/// </summary>
52-
public DiagnosticSeverity Severity => DiagnosticSeverity.Warning;
52+
public DiagnosticSeverity Severity => DiagnosticSeverity.Error;
5353

5454
/// <summary>
5555
/// Analyze the given PowerShell AST for incompatible syntax usage.
@@ -103,7 +103,7 @@ public override string GetName()
103103
/// </summary>
104104
public override RuleSeverity GetSeverity()
105105
{
106-
return RuleSeverity.Warning;
106+
return RuleSeverity.Error;
107107
}
108108

109109
/// <summary>
@@ -179,34 +179,6 @@ public IEnumerable<DiagnosticRecord> GetDiagnosticRecords()
179179
return _diagnosticAccumulator;
180180
}
181181

182-
public override AstVisitAction VisitMemberExpression(MemberExpressionAst memberExpressionAst)
183-
{
184-
if (!_targetVersions.Contains(s_v3))
185-
{
186-
return AstVisitAction.Continue;
187-
}
188-
189-
if (!(memberExpressionAst.Member is StringConstantExpressionAst))
190-
{
191-
string message = string.Format(
192-
CultureInfo.CurrentCulture,
193-
Strings.UseCompatibleSyntaxError,
194-
"dynamic member invocation",
195-
memberExpressionAst.Extent.Text,
196-
"3");
197-
198-
_diagnosticAccumulator.Add(new DiagnosticRecord(
199-
message,
200-
memberExpressionAst.Extent,
201-
_rule.GetName(),
202-
_rule.Severity,
203-
_analyzedFilePath
204-
));
205-
}
206-
207-
return AstVisitAction.Continue;
208-
}
209-
210182
public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressionAst methodCallAst)
211183
{
212184
// Look for [typename]::new(...) and [typename]::$dynamicMethodName syntax

Tests/Engine/GetScriptAnalyzerRule.tests.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -152,17 +152,17 @@ Describe "Test RuleExtension" {
152152
Describe "TestSeverity" {
153153
It "filters rules based on the specified rule severity" {
154154
$rules = Get-ScriptAnalyzerRule -Severity Error
155-
$rules.Count | Should -Be 6
155+
$rules.Count | Should -Be 7
156156
}
157157

158158
It "filters rules based on multiple severity inputs"{
159159
$rules = Get-ScriptAnalyzerRule -Severity Error,Information
160-
$rules.Count | Should -Be 16
160+
$rules.Count | Should -Be 17
161161
}
162162

163163
It "takes lower case inputs" {
164164
$rules = Get-ScriptAnalyzerRule -Severity error
165-
$rules.Count | Should -Be 6
165+
$rules.Count | Should -Be 7
166166
}
167167
}
168168

Tests/Rules/UseCompatibleSyntax.Tests.ps1

+2-33
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,15 @@
33

44
$script:RuleName = 'PSUseCompatibleSyntax'
55

6-
$script:ScriptDefinition = @'
7-
class MyClass
8-
{
9-
[string]$Hi = "Hello"
10-
11-
[string]GetString()
12-
{
13-
return $this.Hi
14-
}
15-
}
16-
17-
enum MyEnum
18-
{
19-
One,
20-
Two
21-
}
22-
23-
$x = [MyClass]::new()
24-
25-
$member = 'Hi'
26-
Write-Host $x.$member
27-
28-
Write-Output 'Banana'
29-
30-
$method = 'GetString'
31-
$x.$method()
32-
33-
$enumVal = "One"
34-
[MyEnum]::$enumVal
35-
'@
36-
376
Describe "PSUseCompatibleSyntax" {
387
BeforeAll {
398
$testCases = @(
409
@{ Script = '$x = [MyClass]::new()'; Versions = @(3,4) }
41-
@{ Script = '$member = "Hi"; $x.$member'; Versions = @(3) }
10+
@{ Script = '$member = "Hi"; $x.$member'; Versions = @() }
4211
@{ Script = 'Write-Host "Banana"'; Versions = @() }
4312
@{ Script = '[System.VeryInnocuousType]::RunApiMethod($obj)'; Versions = @() }
4413
@{ Script = '$y.$methodWithAVeryLongName()'; Versions = @(3) }
45-
@{ Script = '$typeExpression::$staticMember'; Versions = @(3) }
14+
@{ Script = '$typeExpression::$staticMember'; Versions = @() }
4615
@{ Script = '$typeExpression::$dynamicStaticMethodName()'; Versions = @(3) }
4716
)
4817

0 commit comments

Comments
 (0)