Skip to content

Commit c7f1f49

Browse files
authored
fix parser errors when handling blocks and object decls, fixes #396 (#397)
* fix parser errors when handling blocks and object decls, fixes #396 * updated CHANGES.md
1 parent e9ba15c commit c7f1f49

File tree

8 files changed

+57
-60
lines changed

8 files changed

+57
-60
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Fixed handling of default setters/getters in indentation check [#391](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/391)
44
- Fixed token tree structure for Sharp(If) inside Kwd(KwdCase) [#394](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/394)
5+
- Fixed comments in function parameters [#395](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/394)
6+
- Fixed parser errors when handling block and object declarations, fixes [#366](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/396) ([#397](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/394))
57
- Added unittests for ParserQueue and CheckerPool [#393](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/393)
68
- Improved wrapped code detection [#392](https://github.com/HaxeCheckstyle/haxe-checkstyle/issues/392)
79

src/checkstyle/token/walk/WalkArrayAccess.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class WalkArrayAccess {
2323
case BrOpen:
2424
for (stored in tempStore) bkOpen.addChild(stored);
2525
tempStore = [];
26-
WalkObjectDecl.walkObjectDecl(stream, bkOpen);
26+
WalkBlock.walkBlock(stream, bkOpen);
2727
case BkOpen:
2828
for (stored in tempStore) bkOpen.addChild(stored);
2929
tempStore = [];

src/checkstyle/token/walk/WalkBlock.hx

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ class WalkBlock {
1313
var rewindPos:Int = stream.currentPos();
1414
while (stream.is(At)) tempStore.push(WalkAt.walkAt(stream));
1515
if (stream.is(BrOpen)) {
16-
if (isBrOpenObjectDecl(parent)) {
17-
WalkObjectDecl.walkObjectDecl(stream, parent);
18-
return;
19-
}
2016
var openTok:TokenTree = stream.consumeTokenDef(BrOpen);
2117
parent.addChild(openTok);
2218
for (tok in tempStore) openTok.addChild(tok);
2319

2420
var progress:TokenStreamProgress = new TokenStreamProgress(stream);
2521
while (progress.streamHasChanged()) {
26-
if (stream.is(BrClose)) break;
27-
WalkStatement.walkStatement(stream, openTok);
22+
switch (stream.token()) {
23+
case BrClose: break;
24+
case Comma, BkClose, PClose:
25+
var child:TokenTree = stream.consumeToken();
26+
openTok.addChild(child);
27+
default: WalkStatement.walkStatement(stream, openTok);
28+
29+
}
2830
}
2931
openTok.addChild(stream.consumeTokenDef(BrClose));
3032
}
@@ -33,16 +35,4 @@ class WalkBlock {
3335
WalkStatement.walkStatement(stream, parent);
3436
}
3537
}
36-
37-
static function isBrOpenObjectDecl(token:TokenTree):Bool {
38-
if ((token == null) || (token.tok == null)) return false;
39-
return switch (token.tok) {
40-
case BkOpen: true;
41-
case Kwd(KwdReturn): true;
42-
case Kwd(KwdFor): isBrOpenObjectDecl(token.parent);
43-
case Kwd(_): false;
44-
case Binop(OpAssign): true;
45-
default: isBrOpenObjectDecl(token.parent);
46-
}
47-
}
4838
}

src/checkstyle/token/walk/WalkObjectDecl.hx

Lines changed: 0 additions & 25 deletions
This file was deleted.

src/checkstyle/token/walk/WalkPOpen.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WalkPOpen {
1616
case POpen:
1717
WalkPOpen.walkPOpen(stream, parent);
1818
case BrOpen:
19-
WalkObjectDecl.walkObjectDecl(stream, parent);
19+
WalkBlock.walkBlock(stream, parent);
2020
case BkOpen:
2121
WalkArrayAccess.walkArrayAccess(stream, parent);
2222
case PClose:

src/checkstyle/token/walk/WalkStatement.hx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ class WalkStatement {
8383
WalkStatement.walkStatement(stream, question);
8484
return;
8585
}
86-
if (isDblDotObjectDecl(parent)) {
87-
return;
88-
}
8986
var dblDotTok:TokenTree = stream.consumeToken();
9087
parent.addChild(dblDotTok);
9188
WalkTypeNameDef.walkTypeNameDef(stream, dblDotTok);
@@ -166,13 +163,4 @@ class WalkStatement {
166163
}
167164
return null;
168165
}
169-
170-
static function isDblDotObjectDecl(token:TokenTree):Bool {
171-
if ((token == null) || (token.tok == null)) return false;
172-
return switch (token.tok) {
173-
case BrOpen: true;
174-
case POpen: false;
175-
default: isDblDotObjectDecl(token.parent);
176-
}
177-
}
178166
}

src/checkstyle/token/walk/WalkSwitch.hx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class WalkSwitch {
123123
case POpen:
124124
WalkPOpen.walkPOpen(stream, parent);
125125
case BrOpen:
126-
WalkObjectDecl.walkObjectDecl(stream, parent);
126+
WalkBlock.walkBlock(stream, parent);
127127
case BkOpen:
128128
WalkArrayAccess.walkArrayAccess(stream, parent);
129129
case Kwd(KwdFunction):

test/token/TokenTreeBuilderParsingTest.hx

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ class TokenTreeBuilderParsingTest {
3131
assertCodeParses(MACRO_REIFICATION);
3232
assertCodeParses(BLOCK_METADATA);
3333
assertCodeParses(COMMENTS_IN_FUNCTION_PARAMS);
34+
assertCodeParses(BLOCK_OBJECT_DECL_SAMPLES_ISSUE_396_1);
35+
assertCodeParses(BLOCK_OBJECT_DECL_SAMPLES_ISSUE_396_2);
36+
assertCodeParses(BLOCK_OBJECT_DECL_SAMPLES_ISSUE_396_3);
3437
}
3538

3639
public function assertCodeParses(code:String, ?pos:PosInfos) {
@@ -398,7 +401,46 @@ abstract TokenTreeBuilderParsingTests(String) to String {
398401
class Test {
399402
function test( /* comment */ a:String /* comment */) { }
400403
function test2( /* comment */ /* comment */) { }
401-
}
402-
";
404+
}";
405+
406+
var BLOCK_OBJECT_DECL_SAMPLES_ISSUE_396_1 = "
407+
class Test {
408+
function test() {
409+
//fails with: bad token Comma != BrClose
410+
var test = switch a
411+
{
412+
case 3: {a: 1, b: 2};
413+
default: {a: 0, b: 2};
414+
}
415+
}
416+
}";
403417

418+
var BLOCK_OBJECT_DECL_SAMPLES_ISSUE_396_2 = "
419+
class Test {
420+
function test() {
421+
//fails with: bad token Kwd(KwdFunction) != DblDot
422+
return {
423+
#if js
424+
something:
425+
#else
426+
somethingelse:
427+
#end
428+
function (e)
429+
{
430+
e.preventDefault();
431+
callback();
432+
}
433+
};
434+
}
435+
}";
436+
437+
var BLOCK_OBJECT_DECL_SAMPLES_ISSUE_396_3 = "
438+
class Test {
439+
function test() {
440+
return {
441+
doSomething();
442+
1;
443+
}
444+
}
445+
}";
404446
}

0 commit comments

Comments
 (0)