Skip to content

Commit

Permalink
fixed multiline coments after typedefs without semicolon (#138)
Browse files Browse the repository at this point in the history
fixed multiline coments after typedefs without semicolon
  • Loading branch information
AlexHaxe authored Feb 6, 2019
1 parent 36d28cb commit e553ef3
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 46 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fixed `TokenTreeCheckUtils.getColonType` detection of type check in array comprehension [#136](https://github.com/HaxeCheckstyle/tokentree/issues/136)
- Fixed handling of multiple `implements` [#137](https://github.com/HaxeCheckstyle/tokentree/issues/137)
- Fixed comments after typedefs without semicolon [#138](https://github.com/HaxeCheckstyle/tokentree/issues/138)

## version 1.0.14 (2018-12-05)

Expand Down
18 changes: 8 additions & 10 deletions src/tokentree/TokenStream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class TokenStream {
return new TokenTree(token.tok, space, token.pos, current - 1);
}

public function consumeConstIdent():TokenTree {
public function consumeConstIdent():Null<TokenTree> {
switch (token()) {
case Dollar(_):
return consumeToken();
Expand All @@ -68,9 +68,7 @@ class TokenStream {
default:
switch (MODE) {
case RELAXED: return createDummyToken(Const(CString("autoInsert")));
case STRICT:
error('bad token ${token()} != Const(_)');
return null;
case STRICT: error('bad token ${token()} != Const(_)');
}
}
}
Expand All @@ -82,7 +80,6 @@ class TokenStream {
return createDummyToken(tokenDef);
case STRICT:
error('bad token ${token()} != $tokenDef');
return null;
}
}

Expand Down Expand Up @@ -172,7 +169,7 @@ class TokenStream {
return tokens[current].tok;
}

public function peekNonCommentToken():TokenDef {
public function peekNonCommentToken():Null<TokenDef> {
if ((current < 0) || (current >= tokens.length)) {
switch (MODE) {
case RELAXED:
Expand All @@ -196,7 +193,7 @@ class TokenStream {
return null;
}

public function getTokenPos():Position {
public function getTokenPos():Null<Position> {
if ((current < 0) || (current >= tokens.length)) {
return null;
}
Expand Down Expand Up @@ -307,15 +304,16 @@ class TokenStream {
}

public function popSharpIf():TokenTree {
if (sharpIfStack.length <= 0) {
var token:Null<TokenTree> = sharpIfStack.pop();
if (token == null) {
switch (MODE) {
case RELAXED:
return createDummyToken(CommentLine("dummy token"));
case STRICT:
throw NO_MORE_TOKENS;
}
}
return sharpIfStack.pop();
return token;
}

public function peekSharpIf():TokenTree {
Expand All @@ -331,7 +329,7 @@ class TokenStream {
}

function createDummyToken(tokDef:TokenDef):TokenTree {
var pos:Position = null;
var pos:Null<Position> = null;
if ((current < 0) || (current >= tokens.length)) {
pos = tokens[tokens.length - 1].pos;
pos.min = pos.max;
Expand Down
7 changes: 4 additions & 3 deletions src/tokentree/TokenTree.hx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ class TokenTree extends Token {
}
}

public function addChild(child:TokenTree) {
public function addChild(child:Null<TokenTree>) {
if (child == null) return;
if (children == null) children = [];
if (children.length > 0) {
child.previousSibling = children[children.length - 1];
Expand All @@ -65,12 +66,12 @@ class TokenTree extends Token {
return children.length > 0;
}

public function getFirstChild():TokenTree {
public function getFirstChild():Null<TokenTree> {
if (!hasChildren()) return null;
return children[0];
}

public function getLastChild():TokenTree {
public function getLastChild():Null<TokenTree> {
if (!hasChildren()) return null;
return children[children.length - 1];
}
Expand Down
24 changes: 12 additions & 12 deletions src/tokentree/TokenTreeAccessHelper.hx
Original file line number Diff line number Diff line change
Expand Up @@ -11,56 +11,56 @@ abstract TokenTreeAccessHelper(TokenTree) from TokenTree {
return tok;
}

public function parent():TokenTreeAccessHelper {
public function parent():Null<TokenTreeAccessHelper> {
return if (exists()) this.parent else null;
}

public function previousSibling():TokenTreeAccessHelper {
public function previousSibling():Null<TokenTreeAccessHelper> {
return if (exists()) this.previousSibling else null;
}

public function nextSibling():TokenTreeAccessHelper {
public function nextSibling():Null<TokenTreeAccessHelper> {
return if (exists()) this.nextSibling else null;
}

public function firstChild():TokenTreeAccessHelper {
public function firstChild():Null<TokenTreeAccessHelper> {
return if (exists()) this.getFirstChild() else null;
}

public function lastChild():TokenTreeAccessHelper {
public function lastChild():Null<TokenTreeAccessHelper> {
return if (exists()) this.getLastChild() else null;
}

public function firstOf(tokenDef:TokenDef):TokenTreeAccessHelper {
public function firstOf(tokenDef:TokenDef):Null<TokenTreeAccessHelper> {
if (!exists() || this.children == null) return null;
for (tok in this.children) {
if (tok.is(tokenDef)) return tok;
}
return null;
}

public function lastOf(tokenDef:TokenDef):TokenTreeAccessHelper {
public function lastOf(tokenDef:TokenDef):Null<TokenTreeAccessHelper> {
if (!exists() || this.children == null) return null;
var found:TokenTree = null;
var found:Null<TokenTree> = null;
for (tok in this.children) {
if (tok.is(tokenDef)) found = tok;
}
return found;
}

public function child(index:Int):TokenTreeAccessHelper {
public function child(index:Int):Null<TokenTreeAccessHelper> {
return if (exists() && this.children != null) this.children[index] else null;
}

public function is(tokenDef:TokenDef):TokenTreeAccessHelper {
public function is(tokenDef:TokenDef):Null<TokenTreeAccessHelper> {
return if (exists() && this.is(tokenDef)) this else null;
}

public function isComment():TokenTreeAccessHelper {
public function isComment():Null<TokenTreeAccessHelper> {
return if (exists() && this.isComment()) this else null;
}

public function isCIdent():TokenTreeAccessHelper {
public function isCIdent():Null<TokenTreeAccessHelper> {
return if (exists() && this.isCIdent()) this else null;
}

Expand Down
2 changes: 1 addition & 1 deletion src/tokentree/utils/FieldUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ using tokentree.utils.TokenTreeCheckUtils;
using Lambda;

class FieldUtils {
public static function getFieldType(field:TokenTree, defaultVisibility:TokenFieldVisibility):TokenFieldType {
public static function getFieldType(field:Null<TokenTree>, defaultVisibility:TokenFieldVisibility):TokenFieldType {
if (field == null) {
return UNKNOWN;
}
Expand Down
9 changes: 5 additions & 4 deletions src/tokentree/utils/TokenTreeCheckUtils.hx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ class TokenTreeCheckUtils {

public static function isOpGtTypedefExtension(token:TokenTree):Bool {
return switch (token.tok) {
case Binop(OpGt): (token.access().parent().is(BrOpen).parent().is(Binop(OpAssign)).parent().isCIdent().parent().is(Kwd(KwdTypedef)).token != null);
case Binop(OpGt): (token.access().parent().is(BrOpen).parent().is(Binop(OpAssign)).parent().isCIdent().parent().is(Kwd(KwdTypedef))
.token != null);
default: false;
}
}
Expand Down Expand Up @@ -595,7 +596,7 @@ class TokenTreeCheckUtils {
return FUNCTION_TYPE_HAXE4;
}

static function checkArrowPOpen(token:TokenTree):ArrowType {
static function checkArrowPOpen(token:TokenTree):Null<ArrowType> {
if ((token.children == null) || (token.children.length <= 1)) {
return null;
}
Expand All @@ -613,7 +614,7 @@ class TokenTreeCheckUtils {
return FUNCTION_TYPE_HAXE3;
}

static function checkArrowParent(parent:TokenTree):ArrowType {
static function checkArrowParent(parent:TokenTree):Null<ArrowType> {
if (parent == null) {
return ARROW_FUNCTION;
}
Expand Down Expand Up @@ -712,7 +713,7 @@ class TokenTreeCheckUtils {
return UNKNOWN;
}

public static function getLastToken(token:TokenTree):TokenTree {
public static function getLastToken(token:TokenTree):Null<TokenTree> {
if (token == null) {
return null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/tokentree/walk/WalkArrayAccess.hx
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class WalkArrayAccess {
WalkFunction.walkFunction(stream, bkOpen);
case Comma:
var comma:TokenTree = stream.consumeToken();
var child:TokenTree = bkOpen.getLastChild();
var child:Null<TokenTree> = bkOpen.getLastChild();
if (child == null) child = bkOpen;
child.addChild(comma);
case Binop(OpArrow):
var child:TokenTree = bkOpen.getLastChild();
var child:Null<TokenTree> = bkOpen.getLastChild();
if (child == null) child = bkOpen;
WalkStatement.walkStatement(stream, child);
default:
Expand Down
6 changes: 3 additions & 3 deletions src/tokentree/walk/WalkFor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class WalkFor {
var pOpen:TokenTree = stream.consumeTokenDef(POpen);
parent.addChild(pOpen);
WalkComment.walkComment(stream, pOpen);
var identifier:TokenTree = null;
var identifier:Null<TokenTree> = null;
switch (stream.token()) {
case Dollar(_):
WalkStatement.walkStatement(stream, pOpen);
Expand All @@ -67,10 +67,10 @@ class WalkFor {
if (stream.is(Binop(OpArrow))) {
var arrowTok:TokenTree = stream.consumeToken();
identifier.addChild(arrowTok);
var valueIdent:TokenTree = stream.consumeConstIdent();
var valueIdent:Null<TokenTree> = stream.consumeConstIdent();
arrowTok.addChild(valueIdent);
}
var inTok:TokenTree = null;
var inTok:Null<TokenTree> = null;
switch (stream.token()) {
case #if (haxe_ver < 4.0) Kwd(KwdIn) #else Binop(OpIn) #end:
inTok = stream.consumeToken();
Expand Down
4 changes: 2 additions & 2 deletions src/tokentree/walk/WalkFunction.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package tokentree.walk;

class WalkFunction {
public static function walkFunction(stream:TokenStream, parent:TokenTree) {
var funcTok:TokenTree = stream.consumeTokenDef(Kwd(KwdFunction));
var funcTok:Null<TokenTree> = stream.consumeTokenDef(Kwd(KwdFunction));
parent.addChild(funcTok);
WalkComment.walkComment(stream, funcTok);

Expand All @@ -22,7 +22,7 @@ class WalkFunction {
WalkFunction.walkFunctionParameters(stream, name);
WalkComment.walkComment(stream, name);
if (stream.is(DblDot)) {
var dblDot:TokenTree = stream.consumeTokenDef(DblDot);
var dblDot:Null<TokenTree> = stream.consumeTokenDef(DblDot);
name.addChild(dblDot);
WalkTypeNameDef.walkTypeNameDef(stream, dblDot);
}
Expand Down
3 changes: 1 addition & 2 deletions src/tokentree/walk/WalkPackageImport.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ class WalkPackageImport {
*
*/
public static function walkPackageImport(stream:TokenStream, parent:TokenTree) {
var newChild:TokenTree = null;
newChild = stream.consumeToken();
var newChild:TokenTree = stream.consumeToken();
parent.addChild(newChild);
if (Type.enumEq(Semicolon, newChild.tok)) return;
if (!stream.hasMore()) return;
Expand Down
4 changes: 2 additions & 2 deletions src/tokentree/walk/WalkStatement.hx
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ class WalkStatement {
}
}

static function findQuestionParent(token:TokenTree):TokenTree {
var parent:TokenTree = token;
static function findQuestionParent(token:TokenTree):Null<TokenTree> {
var parent:Null<TokenTree> = token;
while (parent != null && parent.tok != null) {
switch (parent.tok) {
case Question:
Expand Down
26 changes: 23 additions & 3 deletions src/tokentree/walk/WalkTypeNameDef.hx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class WalkTypeNameDef {
parent = questTok;
WalkComment.walkComment(stream, parent);
}
var name:TokenTree;
var name:Null<TokenTree>;
var bAdd:Bool = true;
switch (stream.token()) {
case BrOpen:
Expand Down Expand Up @@ -48,7 +48,7 @@ class WalkTypeNameDef {
}

static function walkTypeNameDefContinue(stream:TokenStream, parent:TokenTree) {
WalkComment.walkComment(stream, parent);
walkTypeNameDefComment(stream, parent);
if (stream.is(Dot)) {
var dot:TokenTree = stream.consumeTokenDef(Dot);
parent.addChild(dot);
Expand All @@ -63,6 +63,26 @@ class WalkTypeNameDef {
return;
}
if (stream.is(BkOpen)) WalkArrayAccess.walkArrayAccess(stream, parent);
WalkComment.walkComment(stream, parent);
walkTypeNameDefComment(stream, parent);
}

static function walkTypeNameDefComment(stream:TokenStream, parent:TokenTree) {
var currentPos:Int = stream.getStreamIndex();
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());
case DblDot, Comma, Semicolon, Dot, BrOpen, BkOpen, POpen, Binop(_):
for (comment in comments) {
parent.addChild(comment);
}
return;
default:
stream.rewindTo(currentPos);
return;
}
}
}
}
4 changes: 2 additions & 2 deletions src/tokentree/walk/WalkVar.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package tokentree.walk;

class WalkVar {
public static function walkVar(stream:TokenStream, parent:TokenTree) {
var name:TokenTree = null;
var varTok:TokenTree = stream.consumeTokenDef(Kwd(KwdVar));
var name:Null<TokenTree> = null;
var varTok:Null<TokenTree> = stream.consumeTokenDef(Kwd(KwdVar));
parent.addChild(varTok);
WalkComment.walkComment(stream, parent);
var progress:TokenStreamProgress = new TokenStreamProgress(stream);
Expand Down

0 comments on commit e553ef3

Please sign in to comment.