From 63d78e19650c92072f3f0d7f009934056ec2ed3d Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Fri, 16 Jun 2017 19:03:08 -0700 Subject: [PATCH 1/2] Fix PlaceCloseBrace rule behavior for NewLineAfter switch Whenever the switch is set to false, we check if the following keyword is one of the branching statements (else, elseif, catch, finally) and if so then raise a violation. This makes the switch behavior exactly opposite to that of when it set to true. --- Rules/PlaceCloseBrace.cs | 4 +--- Tests/Rules/PlaceCloseBrace.tests.ps1 | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Rules/PlaceCloseBrace.cs b/Rules/PlaceCloseBrace.cs index 8c6e70dab..74d86a749 100644 --- a/Rules/PlaceCloseBrace.cs +++ b/Rules/PlaceCloseBrace.cs @@ -320,9 +320,7 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter( if (tokens.Length > 1 && tokens.Length > expectedNewLinePos) { var closeBraceToken = tokens[closeBracePos]; - if ((tokens[expectedNewLinePos].Kind == TokenKind.Else - || tokens[expectedNewLinePos].Kind == TokenKind.ElseIf) - && !tokensToIgnore.Contains(closeBraceToken)) + if (!tokensToIgnore.Contains(closeBraceToken) && IsBranchingStatementToken(tokens[expectedNewLinePos])) { return new DiagnosticRecord( GetError(Strings.PlaceCloseBraceErrorShouldFollowNewLine), diff --git a/Tests/Rules/PlaceCloseBrace.tests.ps1 b/Tests/Rules/PlaceCloseBrace.tests.ps1 index 7b6f6203d..88e035e4c 100644 --- a/Tests/Rules/PlaceCloseBrace.tests.ps1 +++ b/Tests/Rules/PlaceCloseBrace.tests.ps1 @@ -177,6 +177,27 @@ if (Test-Path "blah") { Test-CorrectionExtentFromContent @params } + It "Should find a violation for a close brace followed by a catch statement" { + $def = @' +try { + "try" +} catch { + "catch" +} + +'@ + $violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings + $violations.Count | Should Be 1 + $params = @{ + RawContent = $def + DiagnosticRecord = $violations[0] + CorrectionsCount = 1 + ViolationText = '}' + CorrectionText = '}' + [System.Environment]::NewLine + } + Test-CorrectionExtentFromContent @params + + } It "Should not find a violation for a close brace followed by a comma in an array expression" { $def = @' Some-Command -Param1 @{ From 3bb1a08f05c22e98f32553c3c5228ead6b40cfae Mon Sep 17 00:00:00 2001 From: Kapil Borle Date: Fri, 16 Jun 2017 19:16:01 -0700 Subject: [PATCH 2/2] Setup PlaceCloseBrace methods during configuration stage --- Rules/PlaceCloseBrace.cs | 48 +++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/Rules/PlaceCloseBrace.cs b/Rules/PlaceCloseBrace.cs index 74d86a749..ba66362da 100644 --- a/Rules/PlaceCloseBrace.cs +++ b/Rules/PlaceCloseBrace.cs @@ -28,6 +28,10 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules #endif public class PlaceCloseBrace : ConfigurableRule { + private HashSet tokensToIgnore; + private List> violationFinders + = new List>(); + /// /// Indicates if there should or should not be an empty line before a close brace. /// @@ -57,7 +61,28 @@ public class PlaceCloseBrace : ConfigurableRule [ConfigurableRuleProperty(defaultValue: true)] public bool NewLineAfter { get; protected set; } - private HashSet tokensToIgnore; + /// + /// Sets the configurable properties of this rule. + /// + /// A dictionary that maps parameter name to it value. Must be non-null + public override void ConfigureRule(IDictionary paramValueMap) + { + base.ConfigureRule(paramValueMap); + violationFinders.Add(GetViolationForBraceShouldBeOnNewLine); + if (NoEmptyLineBefore) + { + violationFinders.Add(GetViolationForBraceShouldNotFollowEmptyLine); + } + + if (NewLineAfter) + { + violationFinders.Add(GetViolationForBraceShouldHaveNewLineAfter); + } + else + { + violationFinders.Add(GetViolationsForUncuddledBranches); + } + } /// /// Analyzes the given ast to find violations. @@ -121,27 +146,10 @@ public override IEnumerable AnalyzeScript(Ast ast, string file continue; } - AddToDiagnosticRecords( - GetViolationForBraceShouldBeOnNewLine(tokens, k, openBracePos, fileName), - ref diagnosticRecords); - - if (NoEmptyLineBefore) - { - AddToDiagnosticRecords( - GetViolationForBraceShouldNotFollowEmptyLine(tokens, k, openBracePos, fileName), - ref diagnosticRecords); - } - - if (NewLineAfter) - { - AddToDiagnosticRecords( - GetViolationForBraceShouldHaveNewLineAfter(tokens, k, openBracePos, fileName), - ref diagnosticRecords); - } - else + foreach (var violationFinder in violationFinders) { AddToDiagnosticRecords( - GetViolationsForUncuddledBranches(tokens, k, openBracePos, fileName), + violationFinder(tokens, k, openBracePos, fileName), ref diagnosticRecords); } }