From a2c146e69d539b8f124e16af3b82cd073d5b9b94 Mon Sep 17 00:00:00 2001 From: Adi Date: Mon, 16 May 2016 09:15:48 +0100 Subject: [PATCH] fix for else if block --- .../checks/block/NeedBracesCheck.hx | 4 ++ test/checks/block/NeedBracesCheckTest.hx | 46 +++++++++++++++---- 2 files changed, 40 insertions(+), 10 deletions(-) diff --git a/src/checkstyle/checks/block/NeedBracesCheck.hx b/src/checkstyle/checks/block/NeedBracesCheck.hx index 37b072c7..1bb078f8 100644 --- a/src/checkstyle/checks/block/NeedBracesCheck.hx +++ b/src/checkstyle/checks/block/NeedBracesCheck.hx @@ -47,6 +47,10 @@ class NeedBracesCheck extends Check { switch (tok.tok) { case Kwd(KwdIf): checkIfChild(tok); + case Kwd(KwdElse): + var firstChild = tok.getFirstChild(); + if (firstChild.is(Kwd(KwdIf))) checkIfChild(firstChild); + else checkLastChild(tok); case Kwd(KwdFunction): checkFunctionChild(tok); case Kwd(KwdDo): diff --git a/test/checks/block/NeedBracesCheckTest.hx b/test/checks/block/NeedBracesCheckTest.hx index 27e68a62..0e2872ec 100644 --- a/test/checks/block/NeedBracesCheckTest.hx +++ b/test/checks/block/NeedBracesCheckTest.hx @@ -55,10 +55,11 @@ class NeedBracesCheckTest extends CheckTestCase { assertMsg(check, TEST9, MSG_SAME_LINE_FOR); assertMsg(check, TEST10, MSG_SAME_LINE_IF); assertMsg(check, TEST11, MSG_SAME_LINE_ELSE); - assertMsg(check, TEST12, MSG_ELSE); - assertMsg(check, TEST13, MSG_SAME_LINE_ELSE); - assertMsg(check, TEST14, MSG_ELSE); + assertMsg(check, TEST12, MSG_SAME_LINE_IF); + assertMsg(check, TEST13, MSG_SAME_LINE_IF); + assertNoMsg(check, TEST14); assertNoMsg(check, INTERFACE_DEF); + assertMsg(check, TEST16, MSG_SAME_LINE_ELSE); } public function testTokenFor() { @@ -102,16 +103,17 @@ class NeedBracesCheckTest extends CheckTestCase { assertNoMsg(check, TEST9); assertNoMsg(check, TEST10); assertMsg(check, TEST11, MSG_IF); - assertMsg(check, TEST12, MSG_ELSE); + assertNoMsg(check, TEST12); assertNoMsg(check, TEST13); - assertMsg(check, TEST14, MSG_ELSE); + assertNoMsg(check, TEST14); check.allowSingleLineStatement = false; assertMsg(check, TEST, MSG_SAME_LINE_IF); assertMsg(check, TEST10, MSG_SAME_LINE_IF); assertMsg(check, TEST11, MSG_SAME_LINE_ELSE); - assertMsg(check, TEST13, MSG_SAME_LINE_ELSE); - assertMsg(check, TEST14, MSG_ELSE); + assertMsg(check, TEST13, MSG_SAME_LINE_IF); + assertNoMsg(check, TEST14); + assertMsg(check, TEST16, MSG_SAME_LINE_ELSE); } public function testTokenElseIf() { @@ -138,9 +140,11 @@ class NeedBracesCheckTest extends CheckTestCase { assertMsg(check, TEST, MSG_SAME_LINE_IF); assertMsg(check, TEST10, MSG_SAME_LINE_IF); assertMsg(check, TEST11, MSG_SAME_LINE_ELSE); - assertMsg(check, TEST12, MSG_ELSE); - assertMsg(check, TEST13, MSG_SAME_LINE_ELSE); - assertMsg(check, TEST14, MSG_ELSE); + assertMsg(check, TEST12, MSG_SAME_LINE_IF); + assertMsg(check, TEST13, MSG_SAME_LINE_IF); + assertNoMsg(check, TEST14); + assertNoMsg(check, TEST15); + assertMsg(check, TEST16, MSG_SAME_LINE_ELSE); } public function testTokenWhile() { @@ -332,6 +336,28 @@ abstract NeedBracesCheckTests(String) to String { } }"; + var TEST15 = " + class Test { + public function test(a:Bool, b:Bool) { + if (a) { + + } + else if (!b) { + + } + } + }"; + + var TEST16 = " + class Test { + function test() { + if (true) { + return; + } + else return; + } + }"; + var INTERFACE_DEF = " interface Test { function test();