Skip to content
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

fix tokentree change of semicolon position #460

Merged
merged 2 commits into from
Aug 23, 2018
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 [#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)
Expand Down
27 changes: 23 additions & 4 deletions src/checkstyle/checks/block/EmptyBlockCheck.hx
Original file line number Diff line number Diff line change
@@ -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".
**/
Expand Down Expand Up @@ -112,18 +110,29 @@ 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) {
if (brOpen.children.length == 1) {
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;
Expand All @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion src/checkstyle/checks/block/LeftCurlyCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/checkstyle/checks/block/RightCurlyCheck.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/checkstyle/import.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;