Skip to content

Commit

Permalink
fixed type parameter detection (#216)
Browse files Browse the repository at this point in the history
* fixed type parameter detection
  • Loading branch information
AlexHaxe authored Feb 15, 2023
1 parent dd2d263 commit 3209eda
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .haxerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "660947b",
"version": "a985681",
"resolveLibs": "scoped"
}
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## dev branch / next version (1.x.x)

## version 1.2.7 (2023-02-15)

- Fixed type parameter detection ([#216](https://github.com/HaxeCheckstyle/tokentree/issues/216))

## version 1.2.6 (2023-02-06)

- Fixed pos info of Root nodes, fixes [#215](https://github.com/HaxeCheckstyle/tokentree/issues/215)
Expand Down
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
"contributors": [
"AlexHaxe"
],
"releasenote": "fixed pos info of root nodes - see CHANGELOG for details",
"version": "1.2.6",
"releasenote": "fixed type parameter detection - see CHANGELOG for details",
"version": "1.2.7",
"url": "https://github.com/HaxeCheckstyle/tokentree",
"dependencies": {}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.2.6",
"version": "1.2.7",
"description": "TokenTree library used by haxe-checkstyle, haxe-formatter and haxe-languageserver",
"repository": {
"type": "git",
Expand Down
13 changes: 7 additions & 6 deletions src/tokentree/TokenStream.hx
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,7 @@ class TokenStream {
while (true) {
token = tokens[index++];
switch (token.tok) {
case Dot:
case DblDot:
case Comma:
case Arrow:
case Dot | DblDot | Comma | Arrow | Const(_) | Dollar(_):
case POpen:
pDepth++;
case PClose:
Expand All @@ -173,11 +170,15 @@ class TokenStream {
return false;
}
bkDepth--;
case Const(_):
case Dollar(_):
case Binop(OpLt):
if ((pDepth > 0) || (bkDepth > 0) || (brDepth > 0)) {
continue;
}
depth++;
case Binop(OpGt):
if ((pDepth > 0) || (bkDepth > 0) || (brDepth > 0)) {
continue;
}
depth--;
if (depth <= 0) {
return true;
Expand Down
2 changes: 2 additions & 0 deletions test/TestMain.hx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import tokentree.TokenStreamTest;
import tokentree.TokenTreeBuilderParsingTest;
import tokentree.TokenTreeBuilderTest;
import tokentree.utils.FieldUtilsTest;
Expand All @@ -16,6 +17,7 @@ class TestMain {
var tests:Array<ITest> = [
new FieldUtilsTest(),
new TokenTreeCheckUtilsTest(),
new TokenStreamTest(),
new TokenTreeBuilderTest(),
new TokenTreeBuilderParsingTest(),
new VerifyTokenTreeTest(),
Expand Down
28 changes: 28 additions & 0 deletions test/tokentree/TokenStreamTest.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tokentree;

import haxe.PosInfos;

class TokenStreamTest implements ITest {
public function new() {}

@Test
public function testIsTypedParam() {
function codeIsTyeParam(code:String):Bool {
var stream:TokenStream = TestTokenTreeBuilder.makeTokenStream(code);
return stream.isTypedParam();
}
Assert.isFalse(codeIsTyeParam(""));
Assert.isFalse(codeIsTyeParam("1"));
Assert.isFalse(codeIsTyeParam("< 1"));
Assert.isFalse(codeIsTyeParam("< (y >> 1);"));
Assert.isFalse(codeIsTyeParam("< arr[1];"));
Assert.isFalse(codeIsTyeParam("< arr[i << 1];"));
Assert.isFalse(codeIsTyeParam("< arr[i]];"));
Assert.isFalse(codeIsTyeParam("< arr[i]};"));

Assert.isTrue(codeIsTyeParam("<String>"));
Assert.isTrue(codeIsTyeParam("<haxe.Json>"));
Assert.isTrue(codeIsTyeParam("<Array<haxe.Json>>"));
Assert.isTrue(codeIsTyeParam("<Map<String, haxe.Json>>"));
}
}
21 changes: 17 additions & 4 deletions test/tokentree/TokenTreeBuilderParsingTest.hx
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class TokenTreeBuilderParsingTest implements ITest {
assertCodeParses(INLINE_MARKUP);
assertCodeParses(ENUM_TYPE_PARAM);
assertCodeParses(MACRO_COLON_TYPE);
assertCodeParses(TYPE_PARAMETER_WITH_GT);
}

@Test
Expand Down Expand Up @@ -1839,14 +1840,26 @@ import #if haxe4 js.lib.Promise #else js.Promise #end as JsPromise;
</soap:Envelope>;
';

var ENUM_TYPE_PARAM = 'enum Foo<Child> {
var ENUM_TYPE_PARAM = "enum Foo<Child> {
Bar<T>(options : Array<T>);
}';
}";

var MACRO_COLON_TYPE = 'function test() {
var MACRO_COLON_TYPE = "function test() {
switch type {
case Foo:
macro :Float;
}
}';
}";

var TYPE_PARAMETER_WITH_GT = "function test() {
var arr:Array<String>;
var arr:Array<haxe.Function>;
x < (y >> 1);
x < (y >>> 1);
x < (y << 1);
x < y << 1;
x < y >> 1;
x < y[i << 1];
x < y[i >> 1];
}";
}

0 comments on commit 3209eda

Please sign in to comment.