From a415198be5e0f6d577a736e736e36394a1c48115 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Mon, 3 Feb 2020 22:42:26 -0800 Subject: [PATCH] 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 --- .../CompatibilityRules/UseCompatibleSyntax.cs | 32 ++--------------- Tests/Engine/GetScriptAnalyzerRule.tests.ps1 | 6 ++-- Tests/Rules/UseCompatibleSyntax.Tests.ps1 | 35 ++----------------- 3 files changed, 7 insertions(+), 66 deletions(-) diff --git a/Rules/CompatibilityRules/UseCompatibleSyntax.cs b/Rules/CompatibilityRules/UseCompatibleSyntax.cs index fc063f3ff..2863d1718 100644 --- a/Rules/CompatibilityRules/UseCompatibleSyntax.cs +++ b/Rules/CompatibilityRules/UseCompatibleSyntax.cs @@ -49,7 +49,7 @@ public class UseCompatibleSyntax : ConfigurableRule /// /// The severity of diagnostics generated by this rule. /// - public DiagnosticSeverity Severity => DiagnosticSeverity.Warning; + public DiagnosticSeverity Severity => DiagnosticSeverity.Error; /// /// Analyze the given PowerShell AST for incompatible syntax usage. @@ -103,7 +103,7 @@ public override string GetName() /// public override RuleSeverity GetSeverity() { - return RuleSeverity.Warning; + return RuleSeverity.Error; } /// @@ -179,34 +179,6 @@ public IEnumerable GetDiagnosticRecords() return _diagnosticAccumulator; } - public override AstVisitAction VisitMemberExpression(MemberExpressionAst memberExpressionAst) - { - if (!_targetVersions.Contains(s_v3)) - { - return AstVisitAction.Continue; - } - - if (!(memberExpressionAst.Member is StringConstantExpressionAst)) - { - string message = string.Format( - CultureInfo.CurrentCulture, - Strings.UseCompatibleSyntaxError, - "dynamic member invocation", - memberExpressionAst.Extent.Text, - "3"); - - _diagnosticAccumulator.Add(new DiagnosticRecord( - message, - memberExpressionAst.Extent, - _rule.GetName(), - _rule.Severity, - _analyzedFilePath - )); - } - - return AstVisitAction.Continue; - } - public override AstVisitAction VisitInvokeMemberExpression(InvokeMemberExpressionAst methodCallAst) { // Look for [typename]::new(...) and [typename]::$dynamicMethodName syntax diff --git a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 index 404998d43..eac588eb4 100644 --- a/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 +++ b/Tests/Engine/GetScriptAnalyzerRule.tests.ps1 @@ -152,17 +152,17 @@ Describe "Test RuleExtension" { Describe "TestSeverity" { It "filters rules based on the specified rule severity" { $rules = Get-ScriptAnalyzerRule -Severity Error - $rules.Count | Should -Be 6 + $rules.Count | Should -Be 7 } It "filters rules based on multiple severity inputs"{ $rules = Get-ScriptAnalyzerRule -Severity Error,Information - $rules.Count | Should -Be 16 + $rules.Count | Should -Be 17 } It "takes lower case inputs" { $rules = Get-ScriptAnalyzerRule -Severity error - $rules.Count | Should -Be 6 + $rules.Count | Should -Be 7 } } diff --git a/Tests/Rules/UseCompatibleSyntax.Tests.ps1 b/Tests/Rules/UseCompatibleSyntax.Tests.ps1 index 68b5800db..dd6497fb3 100644 --- a/Tests/Rules/UseCompatibleSyntax.Tests.ps1 +++ b/Tests/Rules/UseCompatibleSyntax.Tests.ps1 @@ -3,46 +3,15 @@ $script:RuleName = 'PSUseCompatibleSyntax' -$script:ScriptDefinition = @' -class MyClass -{ - [string]$Hi = "Hello" - - [string]GetString() - { - return $this.Hi - } -} - -enum MyEnum -{ - One, - Two -} - -$x = [MyClass]::new() - -$member = 'Hi' -Write-Host $x.$member - -Write-Output 'Banana' - -$method = 'GetString' -$x.$method() - -$enumVal = "One" -[MyEnum]::$enumVal -'@ - Describe "PSUseCompatibleSyntax" { BeforeAll { $testCases = @( @{ Script = '$x = [MyClass]::new()'; Versions = @(3,4) } - @{ Script = '$member = "Hi"; $x.$member'; Versions = @(3) } + @{ Script = '$member = "Hi"; $x.$member'; Versions = @() } @{ Script = 'Write-Host "Banana"'; Versions = @() } @{ Script = '[System.VeryInnocuousType]::RunApiMethod($obj)'; Versions = @() } @{ Script = '$y.$methodWithAVeryLongName()'; Versions = @(3) } - @{ Script = '$typeExpression::$staticMember'; Versions = @(3) } + @{ Script = '$typeExpression::$staticMember'; Versions = @() } @{ Script = '$typeExpression::$dynamicStaticMethodName()'; Versions = @(3) } )