Skip to content

Commit

Permalink
fixed whitespace after else with ifPolicy, fixes #543 (#544)
Browse files Browse the repository at this point in the history
* fixed whitespace after else with ifPolicy, fixes #543
* fixed emptyline afterReturn when return is body of function
  • Loading branch information
AlexHaxe authored Dec 13, 2019
1 parent a5068fd commit 303f906
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- Fixed extra indentation for parens after `=`, fixes [#534](https://github.com/HaxeCheckstyle/haxe-formatter/issues/534) ([#535](https://github.com/HaxeCheckstyle/haxe-formatter/issues/535))
- Fixed method chain with comments detection and wrapping ([#536](https://github.com/HaxeCheckstyle/haxe-formatter/issues/536))
- Fixed whitespace before metadata ([#537](https://github.com/HaxeCheckstyle/haxe-formatter/issues/537))
- Fixed whitespace after else with ifPolicy, fixes [#543](https://github.com/HaxeCheckstyle/haxe-formatter/issues/543) ([#544](https://github.com/HaxeCheckstyle/haxe-formatter/issues/544))
- Fixed emptyline afterReturn when return is body of function ([#544](https://github.com/HaxeCheckstyle/haxe-formatter/issues/544))
- Refactored build system to use lix ([#537](https://github.com/HaxeCheckstyle/haxe-formatter/issues/537) + [#539](https://github.com/HaxeCheckstyle/haxe-formatter/issues/539))

## version 1.9.1 (2019-09-12)
Expand Down
18 changes: 18 additions & 0 deletions src/formatter/marker/MarkEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,9 @@ class MarkEmptyLines extends MarkerBase {
function markReturn() {
var returns:Array<TokenTree> = parsedCode.root.filter([Kwd(KwdReturn)], ALL);
for (ret in returns) {
if (isReturnBody(ret)) {
continue;
}
var lastChild:TokenTree = TokenTreeCheckUtils.getLastToken(ret);
if (lastChild == null) {
continue;
Expand All @@ -845,6 +848,21 @@ class MarkEmptyLines extends MarkerBase {
}
}

function isReturnBody(ret:TokenTree):Bool {
var parent:TokenTree = ret.parent;
while ((parent != null) && (parent.tok != null)) {
switch (parent.tok) {
case Kwd(KwdFunction):
return true;
case BrOpen:
return false;
default:
parent = parent.parent;
}
}
return true;
}

function markSharp() {
var sharps:Array<TokenTree> = parsedCode.root.filterCallback(function(token:TokenTree, index:Int):FilterResult {
return switch (token.tok) {
Expand Down
5 changes: 4 additions & 1 deletion src/formatter/marker/MarkWhitespace.hx
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,11 @@ class MarkWhitespace extends MarkerBase {
whitespace(token, NoneAfter);
case Kwd(KwdExtends), Kwd(KwdImplements):
whitespace(token, Around);
case Kwd(KwdIf), Kwd(KwdElse):
case Kwd(KwdIf):
whitespace(token, config.whitespace.ifPolicy);
case Kwd(KwdElse):
var policy:WhitespacePolicy = config.whitespace.ifPolicy;
whitespace(token, policy.add(After));
case Kwd(KwdDo):
whitespace(token, config.whitespace.doPolicy);
case Kwd(KwdWhile):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"emptyLines": {
"classEmptyLines": {
"beginType": 1,
"endType": 1
},
"afterLeftCurly": "keep",
"beforeRightCurly": "keep"
}
}

---

class Main {
function main2()
return "";
}

---

class Main {

function main2()
return "";

}
21 changes: 21 additions & 0 deletions test/testcases/whitespace/issue_543_ifPolicy_after_else.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"whitespace": {
"ifPolicy": "onlyBefore"
}
}

---

class Main {
static function main() {
return if(self == id) self else id;
}
}

---

class Main {
static function main() {
return if(self == id) self else id;
}
}

0 comments on commit 303f906

Please sign in to comment.