Skip to content

Commit bec2128

Browse files
author
Kapil Borle
authored
Merge pull request #784 from PowerShell/kapilmb/fix-close-brace-rule
Update PlaceCloseBrace rule behavior for NewLineAfter switch
2 parents 57ad2b8 + 3bb1a08 commit bec2128

File tree

2 files changed

+50
-23
lines changed

2 files changed

+50
-23
lines changed

Rules/PlaceCloseBrace.cs

+29-23
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.BuiltinRules
2828
#endif
2929
public class PlaceCloseBrace : ConfigurableRule
3030
{
31+
private HashSet<Token> tokensToIgnore;
32+
private List<Func<Token[], int, int, string, DiagnosticRecord>> violationFinders
33+
= new List<Func<Token[], int, int, string, DiagnosticRecord>>();
34+
3135
/// <summary>
3236
/// Indicates if there should or should not be an empty line before a close brace.
3337
///
@@ -57,7 +61,28 @@ public class PlaceCloseBrace : ConfigurableRule
5761
[ConfigurableRuleProperty(defaultValue: true)]
5862
public bool NewLineAfter { get; protected set; }
5963

60-
private HashSet<Token> tokensToIgnore;
64+
/// <summary>
65+
/// Sets the configurable properties of this rule.
66+
/// </summary>
67+
/// <param name="paramValueMap">A dictionary that maps parameter name to it value. Must be non-null</param>
68+
public override void ConfigureRule(IDictionary<string, object> paramValueMap)
69+
{
70+
base.ConfigureRule(paramValueMap);
71+
violationFinders.Add(GetViolationForBraceShouldBeOnNewLine);
72+
if (NoEmptyLineBefore)
73+
{
74+
violationFinders.Add(GetViolationForBraceShouldNotFollowEmptyLine);
75+
}
76+
77+
if (NewLineAfter)
78+
{
79+
violationFinders.Add(GetViolationForBraceShouldHaveNewLineAfter);
80+
}
81+
else
82+
{
83+
violationFinders.Add(GetViolationsForUncuddledBranches);
84+
}
85+
}
6186

6287
/// <summary>
6388
/// Analyzes the given ast to find violations.
@@ -121,27 +146,10 @@ public override IEnumerable<DiagnosticRecord> AnalyzeScript(Ast ast, string file
121146
continue;
122147
}
123148

124-
AddToDiagnosticRecords(
125-
GetViolationForBraceShouldBeOnNewLine(tokens, k, openBracePos, fileName),
126-
ref diagnosticRecords);
127-
128-
if (NoEmptyLineBefore)
129-
{
130-
AddToDiagnosticRecords(
131-
GetViolationForBraceShouldNotFollowEmptyLine(tokens, k, openBracePos, fileName),
132-
ref diagnosticRecords);
133-
}
134-
135-
if (NewLineAfter)
136-
{
137-
AddToDiagnosticRecords(
138-
GetViolationForBraceShouldHaveNewLineAfter(tokens, k, openBracePos, fileName),
139-
ref diagnosticRecords);
140-
}
141-
else
149+
foreach (var violationFinder in violationFinders)
142150
{
143151
AddToDiagnosticRecords(
144-
GetViolationsForUncuddledBranches(tokens, k, openBracePos, fileName),
152+
violationFinder(tokens, k, openBracePos, fileName),
145153
ref diagnosticRecords);
146154
}
147155
}
@@ -320,9 +328,7 @@ private DiagnosticRecord GetViolationForBraceShouldHaveNewLineAfter(
320328
if (tokens.Length > 1 && tokens.Length > expectedNewLinePos)
321329
{
322330
var closeBraceToken = tokens[closeBracePos];
323-
if ((tokens[expectedNewLinePos].Kind == TokenKind.Else
324-
|| tokens[expectedNewLinePos].Kind == TokenKind.ElseIf)
325-
&& !tokensToIgnore.Contains(closeBraceToken))
331+
if (!tokensToIgnore.Contains(closeBraceToken) && IsBranchingStatementToken(tokens[expectedNewLinePos]))
326332
{
327333
return new DiagnosticRecord(
328334
GetError(Strings.PlaceCloseBraceErrorShouldFollowNewLine),

Tests/Rules/PlaceCloseBrace.tests.ps1

+21
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,27 @@ if (Test-Path "blah") {
177177
Test-CorrectionExtentFromContent @params
178178
}
179179

180+
It "Should find a violation for a close brace followed by a catch statement" {
181+
$def = @'
182+
try {
183+
"try"
184+
} catch {
185+
"catch"
186+
}
187+
188+
'@
189+
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
190+
$violations.Count | Should Be 1
191+
$params = @{
192+
RawContent = $def
193+
DiagnosticRecord = $violations[0]
194+
CorrectionsCount = 1
195+
ViolationText = '}'
196+
CorrectionText = '}' + [System.Environment]::NewLine
197+
}
198+
Test-CorrectionExtentFromContent @params
199+
200+
}
180201
It "Should not find a violation for a close brace followed by a comma in an array expression" {
181202
$def = @'
182203
Some-Command -Param1 @{

0 commit comments

Comments
 (0)