From fc788ef776ff46261d18607c5e1bcd4cd7b60d9b Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Thu, 23 Aug 2018 17:46:49 +0200 Subject: [PATCH 1/2] fix tokentree change of semicolon position --- CHANGELOG.md | 1 + .../checks/block/EmptyBlockCheck.hx | 27 ++++++++++++++++--- src/checkstyle/checks/block/LeftCurlyCheck.hx | 2 +- .../checks/block/RightCurlyCheck.hx | 2 +- src/checkstyle/import.hx | 1 + 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0c742bf3..d58eb463 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Fixed indentation in brackets [#456](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/456) - Fixed return indentation [#457](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/457) - Fixed indentation of arrow [#459](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/459) +- Fixed position change of semicolon - Changed return block indentation [#453](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/453) ## version 2.4.2 (2018-07-01) diff --git a/src/checkstyle/checks/block/EmptyBlockCheck.hx b/src/checkstyle/checks/block/EmptyBlockCheck.hx index 45be96e4..e18041e0 100644 --- a/src/checkstyle/checks/block/EmptyBlockCheck.hx +++ b/src/checkstyle/checks/block/EmptyBlockCheck.hx @@ -1,7 +1,5 @@ package checkstyle.checks.block; -import tokentree.TokenTreeAccessHelper; - /** Checks for empty blocks. The policy to verify is specified using the property "option". **/ @@ -112,6 +110,11 @@ class EmptyBlockCheck extends Check { logPos("Empty block should contain a comment or a statement", brOpen.getPos()); return; } + var lastChild:TokenTree = brOpen.getLastChild(); + if ((brOpen.children.length == 2) && lastChild.is(Semicolon)){ + logPos("Empty block should contain a comment or a statement", brOpen.getPos()); + return; + } } function checkForStatement(brOpen:TokenTree) { @@ -119,11 +122,17 @@ class EmptyBlockCheck extends Check { logPos("Empty block should contain a statement", brOpen.getPos()); return; } + var lastChild:TokenTree = brOpen.getLastChild(); + if ((brOpen.children.length == 2) && lastChild.is(Semicolon)){ + logPos("Empty block should contain a statement", brOpen.getPos()); + return; + } var onlyComments:Bool = true; for (child in brOpen.children) { switch (child.tok) { case Comment(_), CommentLine(_): case BrClose: + break; default: onlyComments = false; break; @@ -133,8 +142,18 @@ class EmptyBlockCheck extends Check { } function checkForEmpty(brOpen:TokenTree) { - if ((brOpen.children == null) || (brOpen.children.length > 1)) return; - var brClose:TokenTree = TokenTreeAccessHelper.access(brOpen).firstChild().is(BrClose).token; + if (brOpen.children == null) return; + if (brOpen.children.length <= 0) return; + + var lastChild:TokenTree = brOpen.getLastChild(); + if (brOpen.access().lastChild().is(Semicolon).exists()) { + if (brOpen.children.length > 2) return; + } + else { + if (brOpen.children.length > 1) return; + } + + var brClose:TokenTree = brOpen.access().firstChild().is(BrClose).token; if (brClose == null) return; if (brOpen.pos.max != brClose.pos.min) logPos('Empty block should be written as "{}"', brOpen.getPos()); } diff --git a/src/checkstyle/checks/block/LeftCurlyCheck.hx b/src/checkstyle/checks/block/LeftCurlyCheck.hx index 4e088ed4..6bec5a25 100644 --- a/src/checkstyle/checks/block/LeftCurlyCheck.hx +++ b/src/checkstyle/checks/block/LeftCurlyCheck.hx @@ -79,7 +79,7 @@ class LeftCurlyCheck extends Check { } function isSingleLine(brOpen:TokenTree):Bool { - var brClose:TokenTree = brOpen.getLastChild(); + var brClose:TokenTree = brOpen.access().firstOf(BrClose).token; return (brClose != null && brOpen.pos.max == brClose.pos.min); } diff --git a/src/checkstyle/checks/block/RightCurlyCheck.hx b/src/checkstyle/checks/block/RightCurlyCheck.hx index fb49cdf4..bb1086cd 100644 --- a/src/checkstyle/checks/block/RightCurlyCheck.hx +++ b/src/checkstyle/checks/block/RightCurlyCheck.hx @@ -171,7 +171,7 @@ class RightCurlyCheck extends Check { logErrorIf(singleLine && (option != ALONE_OR_SINGLELINE), "Right curly should not be on same line as left curly", curlyPos); if (singleLine) return; - var curlyAlone:Bool = ~/^\s*\}[\)\],;\s]*(|\/\/.*)$/.match(line); + var curlyAlone:Bool = ~/^\s*\}(|\..*|\).*|\].*|,\s*|;\s*)(|\/\/.*)$/.match(line); logErrorIf(!curlyAlone && (option == ALONE_OR_SINGLELINE || option == ALONE), "Right curly should be alone on a new line", curlyPos); logErrorIf(curlyAlone && needsSameOption, "Right curly should be alone on a new line", curlyPos); logErrorIf(needsSameOption && (option != SAME), "Right curly must not be on same line as following block", curlyPos); diff --git a/src/checkstyle/import.hx b/src/checkstyle/import.hx index a96b1cad..ead69622 100644 --- a/src/checkstyle/import.hx +++ b/src/checkstyle/import.hx @@ -19,4 +19,5 @@ using checkstyle.utils.ArrayUtils; using checkstyle.utils.FieldUtils; using checkstyle.utils.ExprUtils; using checkstyle.utils.StringUtils; +using tokentree.TokenTreeAccessHelper; using StringTools; \ No newline at end of file From 8761aa681fb85d754ac997ccd594f82ef5459f9d Mon Sep 17 00:00:00 2001 From: AlexHaxe Date: Thu, 23 Aug 2018 17:49:43 +0200 Subject: [PATCH 2/2] updated CHANGELOG --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d58eb463..a52bd821 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ - Fixed indentation in brackets [#456](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/456) - Fixed return indentation [#457](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/457) - Fixed indentation of arrow [#459](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/459) -- Fixed position change of semicolon +- Fixed position change of semicolon [#460](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/460) - Changed return block indentation [#453](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/453) ## version 2.4.2 (2018-07-01)