Skip to content

Commit

Permalink
Fix if else with comments (#72)
Browse files Browse the repository at this point in the history
* also allow OpLt for arrow detection
* allow comments before else
* prepare release of 1.0.7
  • Loading branch information
AlexHaxe authored Jul 26, 2018
1 parent be6dce1 commit 21357f4
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## dev branch / next version (1.x.x)

## version 1.0.7 (2018-07-26)

- Added `TokenTreeCheckUtils.isTypeStructure()` [#36](https://github.com/HaxeCheckstyle/tokentree/issues/36) + [#50](https://github.com/HaxeCheckstyle/tokentree/issues/50)
- Added `TokenTreeCheckUtils.isTypeEnum()` [#40](https://github.com/HaxeCheckstyle/tokentree/issues/40)
- Added `TokenTreeCheckUtils.isMacroClass()` [#42](https://github.com/HaxeCheckstyle/tokentree/issues/42)
Expand Down Expand Up @@ -27,7 +29,7 @@
- Fixed handling of const type parameters [#57](https://github.com/HaxeCheckstyle/tokentree/issues/57)
- Fixed `case var` handling [#58](https://github.com/HaxeCheckstyle/tokentree/issues/58)
- Fixed `case Pattern(var foo, var bar)` handling [#59](https://github.com/HaxeCheckstyle/tokentree/issues/59)
- Fixed comments in if…else [#60](https://github.com/HaxeCheckstyle/tokentree/issues/60)
- Fixed comments in if…else [#60](https://github.com/HaxeCheckstyle/tokentree/issues/60) +[#72](https://github.com/HaxeCheckstyle/tokentree/issues/72)
- Fixed comments in typedefs [#62](https://github.com/HaxeCheckstyle/tokentree/issues/62)
- Fixed handling of array items [#62](https://github.com/HaxeCheckstyle/tokentree/issues/62)
- Fixed handling of `null`, `true` and `false` as body of if [#63](https://github.com/HaxeCheckstyle/tokentree/issues/63)
Expand Down
2 changes: 1 addition & 1 deletion haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"AlexHaxe"
],
"releasenote": "Added helper functions TokenTreeCheckUtils and bugfixes - see CHANGELOG",
"version": "1.0.6",
"version": "1.0.7",
"url": "https://github.com/HaxeCheckstyle/tokentree",
"dependencies": {}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tokentree",
"version": "1.0.6",
"version": "1.0.7",
"description": "TokenTree library used by haxe-checkstyle",
"repository": {
"type": "git",
Expand Down
21 changes: 21 additions & 0 deletions src/tokentree/walk/WalkComment.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,25 @@ class WalkComment {
}
}
}

public static function tryWalkComment(stream:TokenStream, parent:TokenTree, expect:TokenDef) {
var currentPos:Int = stream.getCurrentPos();
var progress:TokenStreamProgress = new TokenStreamProgress(stream);
var comments:Array<TokenTree> = [];
while (stream.hasMore() && progress.streamHasChanged()) {
switch (stream.token()) {
case Comment(_), CommentLine(_):
comments.push(stream.consumeToken());
default:
if (stream.is(expect)) {
for (comment in comments) {
parent.addChild(comment);
}
return;
}
stream.rewindTo(currentPos);
return;
}
}
}
}
1 change: 1 addition & 0 deletions src/tokentree/walk/WalkIf.hx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class WalkIf {
if (stream.is(DblDot)) return;
// if-expr
WalkBlock.walkBlock(stream, ifTok);
WalkComment.tryWalkComment(stream, ifTok, Kwd(KwdElse));
if (stream.is(Kwd(KwdElse))) {
var elseTok:TokenTree = stream.consumeTokenDef(Kwd(KwdElse));
ifTok.addChild(elseTok);
Expand Down
27 changes: 27 additions & 0 deletions test/tokentree/TokenTreeBuilderParsingTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TokenTreeBuilderParsingTest {
assertCodeParses(COMMENTS_IN_IF_ELSE);
assertCodeParses(COMMENTS_IN_TYPEDEF);
assertCodeParses(ADVANCED_ARRAY_ITEMS);
assertCodeParses(IF_ELSE_COMMENTS);
}

public function assertCodeParses(code:String, ?pos:PosInfos) {
Expand Down Expand Up @@ -721,4 +722,30 @@ abstract TokenTreeBuilderParsingTests(String) to String {
]);
}
";

var IF_ELSE_COMMENTS = "
class Main {
function recycle() {
// rotated recycling
if (maxSize > 0) {
// create new instance
if (length < maxSize) {
return recycleCreateObject(ObjectClass, ObjectFactory);
}
// get the next member if at capacity
else {
if (Revive)
basic.revive();
}
}
// grow-style recycling - grab a basic with exists == false or create a new one
else {
if (basic != null) {
if (Revive)
basic.revive();
}
}
}
}
";
}

0 comments on commit 21357f4

Please sign in to comment.