Skip to content

Fix violations for one line switch conditionals #709

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

Merged
merged 1 commit into from
Feb 10, 2017
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
3 changes: 2 additions & 1 deletion Rules/PlaceOpenBrace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,8 @@ private IEnumerable<DiagnosticRecord> FindViolationsForBraceShouldBeOnSameLine(
for (int k = 2; k < tokens.Length; k++)
{
if (tokens[k].Kind == TokenKind.LCurly
&& tokens[k - 1].Kind == TokenKind.NewLine)
&& tokens[k - 1].Kind == TokenKind.NewLine
&& !tokensToIgnore.Contains(tokens[k]))
{
yield return new DiagnosticRecord(
GetError(Strings.PlaceOpenBraceErrorShouldBeOnSameLine),
Expand Down
18 changes: 18 additions & 0 deletions Tests/Rules/PlaceOpenBrace.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ function foo ($param1)
}
}

Context "When an open brace must be on the same line in a switch statement" {
BeforeAll {
$def = @'
switch ($x) {
{"b"} {"b"; break;}
{"a"} {"a"; break;}
}
'@
$ruleConfiguration.'OnSameLine' = $true
$ruleConfiguration.'IgnoreOneLineBlock' = $true
$violations = Invoke-ScriptAnalyzer -ScriptDefinition $def -Settings $settings
}

It "Should not find a violation" {
$violations.Count | Should Be 0
}
}

Context "When an open brace must be on a new line" {
BeforeAll {
$def = @'
Expand Down