From 5a42e285df9dde4aca03bf70ea82bb4d95122c6c Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Wed, 15 Jan 2020 17:20:58 -0800 Subject: [PATCH 1/5] Remove erroneous ps3 dynamic member warning --- .../CompatibilityRules/UseCompatibleSyntax.cs | 28 ------------------- 1 file changed, 28 deletions(-) diff --git a/Rules/CompatibilityRules/UseCompatibleSyntax.cs b/Rules/CompatibilityRules/UseCompatibleSyntax.cs index fc063f3ff..ca02eb217 100644 --- a/Rules/CompatibilityRules/UseCompatibleSyntax.cs +++ b/Rules/CompatibilityRules/UseCompatibleSyntax.cs @@ -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 From 0a74ee6c88207362fb40a28129bbc3628ef41168 Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Wed, 15 Jan 2020 17:21:57 -0800 Subject: [PATCH 2/5] Upgrade compatible syntax rule to emit errors --- Rules/CompatibilityRules/UseCompatibleSyntax.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Rules/CompatibilityRules/UseCompatibleSyntax.cs b/Rules/CompatibilityRules/UseCompatibleSyntax.cs index ca02eb217..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; } /// From 8bde76d2e528b0d94b0f57b68b6795f4e288f9af Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Wed, 15 Jan 2020 21:07:36 -0800 Subject: [PATCH 3/5] Fix tests --- Tests/Rules/UseCompatibleSyntax.Tests.ps1 | 33 +---------------------- 1 file changed, 1 insertion(+), 32 deletions(-) diff --git a/Tests/Rules/UseCompatibleSyntax.Tests.ps1 b/Tests/Rules/UseCompatibleSyntax.Tests.ps1 index 68b5800db..a953aee87 100644 --- a/Tests/Rules/UseCompatibleSyntax.Tests.ps1 +++ b/Tests/Rules/UseCompatibleSyntax.Tests.ps1 @@ -3,42 +3,11 @@ $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) } From 4e1bfeb7851cbed34f5533df0e8c3740c8e4f89c Mon Sep 17 00:00:00 2001 From: Robert Holt Date: Wed, 15 Jan 2020 21:11:17 -0800 Subject: [PATCH 4/5] Fix last test --- Tests/Rules/UseCompatibleSyntax.Tests.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/Rules/UseCompatibleSyntax.Tests.ps1 b/Tests/Rules/UseCompatibleSyntax.Tests.ps1 index a953aee87..dd6497fb3 100644 --- a/Tests/Rules/UseCompatibleSyntax.Tests.ps1 +++ b/Tests/Rules/UseCompatibleSyntax.Tests.ps1 @@ -11,7 +11,7 @@ Describe "PSUseCompatibleSyntax" { @{ 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) } ) From fc0ccbe98cf756e608d74ef40d3f5998af50236d Mon Sep 17 00:00:00 2001 From: Rob Holt Date: Mon, 3 Feb 2020 16:14:10 -0800 Subject: [PATCH 5/5] Fix tests to account for severity change --- Tests/Engine/GetScriptAnalyzerRule.tests.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 } }