Skip to content

Commit

Permalink
Fix ps3 syntax check (#1395)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rjmholt authored Feb 4, 2020
1 parent d5cb76f commit a415198
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 66 deletions.
32 changes: 2 additions & 30 deletions Rules/CompatibilityRules/UseCompatibleSyntax.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public class UseCompatibleSyntax : ConfigurableRule
/// <summary>
/// The severity of diagnostics generated by this rule.
/// </summary>
public DiagnosticSeverity Severity => DiagnosticSeverity.Warning;
public DiagnosticSeverity Severity => DiagnosticSeverity.Error;

/// <summary>
/// Analyze the given PowerShell AST for incompatible syntax usage.
Expand Down Expand Up @@ -103,7 +103,7 @@ public override string GetName()
/// </summary>
public override RuleSeverity GetSeverity()
{
return RuleSeverity.Warning;
return RuleSeverity.Error;
}

/// <summary>
Expand Down Expand Up @@ -179,34 +179,6 @@ public IEnumerable<DiagnosticRecord> 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
Expand Down
6 changes: 3 additions & 3 deletions Tests/Engine/GetScriptAnalyzerRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
35 changes: 2 additions & 33 deletions Tests/Rules/UseCompatibleSyntax.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
)

Expand Down

0 comments on commit a415198

Please sign in to comment.