Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ps3 syntax check #1395

Merged
merged 5 commits into from
Feb 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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