Skip to content

Commit

Permalink
fixed null pointer issues (#666)
Browse files Browse the repository at this point in the history
* fixed null pointer issues
  • Loading branch information
AlexHaxe authored Feb 15, 2023
1 parent 8328457 commit 3a907da
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 86 deletions.
2 changes: 1 addition & 1 deletion .haxerc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"version": "5645ecc",
"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.14.5 (2023-02-15)

- Fixed null pointer issues ([#666](https://github.com/HaxeCheckstyle/haxe-formatter/issues/666))

## version 1.14.4 (2022-12-14)

- Refactored PosInfosMacro to limit number of invocations of inner loop
Expand Down
6 changes: 3 additions & 3 deletions haxe_libraries/tokentree.hxml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# @install: lix --silent download "haxelib:/tokentree#1.2.5" into tokentree/1.2.5/haxelib
-cp ${HAXE_LIBCACHE}/tokentree/1.2.5/haxelib/src
-D tokentree=1.2.5
# @install: lix --silent download "haxelib:/tokentree#1.2.7" into tokentree/1.2.7/haxelib
-cp ${HAXE_LIBCACHE}/tokentree/1.2.7/haxelib/src
-D tokentree=1.2.7
4 changes: 2 additions & 2 deletions haxelib.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"style"
],
"description": "A code formatter for Haxe",
"version": "1.14.4",
"releasenote": "fixed some whitespace formatting issues - see CHANGELOG for details.",
"version": "1.14.5",
"releasenote": "fixed null pointer issue - see CHANGELOG for details.",
"contributors": [
"AlexHaxe",
"Gama11"
Expand Down
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": "@haxecheckstyle/haxe-formatter",
"version": "1.14.4",
"version": "1.14.5",
"description": "A code formatter for Haxe",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions resources/hxformat-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -576,11 +576,11 @@
"formatter.config.WrapConditionType": {
"anyOf": [
{
"description": "condition matches if item count is larger than or equal n characters",
"description": "condition matches if item count is larger than or equal n items",
"const": "itemCount >= n"
},
{
"description": "condition matches if item count is less than or equal n characters",
"description": "condition matches if item count is less than or equal n items",
"const": "itemCount <= n"
},
{
Expand Down
4 changes: 2 additions & 2 deletions src/formatter/config/WrapConfig.hx
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,12 @@ typedef WrapCondition = {

enum abstract WrapConditionType(String) {
/**
condition matches if item count is larger than or equal n characters
condition matches if item count is larger than or equal n items
**/
var ItemCountLargerThan = "itemCount >= n";

/**
condition matches if item count is less than or equal n characters
condition matches if item count is less than or equal n items
**/
var ItemCountLessThan = "itemCount <= n";

Expand Down
16 changes: 12 additions & 4 deletions src/formatter/marker/MarkEmptyLines.hx
Original file line number Diff line number Diff line change
Expand Up @@ -1063,7 +1063,9 @@ class MarkEmptyLines extends MarkerBase {
parsedCode.root.filterCallback(function(token:TokenTree, index:Int):FilterResult {
switch (token.tok) {
case Kwd(KwdIf):
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
if ((token.children != null) && (token.children.length > 0)) {
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
}
var block:Null<TokenTree> = token.access().firstOf(Kwd(KwdElse)).previousSibling().token;
if (block != null) {
removeEmptyLinesAroundBlock(block, Keep, config.emptyLines.afterBlocks);
Expand All @@ -1075,21 +1077,27 @@ class MarkEmptyLines extends MarkerBase {
removeEmptyLinesAroundBlock(block, config.emptyLines.beforeBlocks, Keep);
case Kwd(KwdFunction):
case Kwd(KwdFor):
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
if ((token.children != null) && (token.children.length > 0)) {
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
}
case Kwd(KwdDo):
removeEmptyLinesAroundBlock(token.getFirstChild(), config.emptyLines.beforeBlocks, Keep);
var block:Null<TokenTree> = token.access().lastChild().previousSibling().token;
removeEmptyLinesAroundBlock(block, Keep, config.emptyLines.afterBlocks);
case Kwd(KwdWhile):
if ((token.parent == null) || (!token.parent.tok.match(Kwd(KwdDo)))) {
if ((token.children != null)
&& (token.children.length > 0)
&& (token.parent == null || !token.parent.tok.match(Kwd(KwdDo)))) {
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
}
case Kwd(KwdTry):
removeEmptyLinesAroundBlock(token.getFirstChild(), config.emptyLines.beforeBlocks, Keep);
var block:Null<TokenTree> = token.access().lastChild().previousSibling().token;
removeEmptyLinesAroundBlock(block, Keep, config.emptyLines.afterBlocks);
case Kwd(KwdCatch):
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
if ((token.children != null) && (token.children.length > 0)) {
removeEmptyLinesAroundBlock(token.children[1], config.emptyLines.beforeBlocks, Keep);
}
default:
}
return GoDeeper;
Expand Down
73 changes: 41 additions & 32 deletions src/formatter/marker/wrapping/MarkWrapping.hx
Original file line number Diff line number Diff line change
Expand Up @@ -577,21 +577,23 @@ class MarkWrapping extends MarkWrappingBase {
}
}
var first:Bool = true;
for (child in itemStart.children) {
switch (child.tok) {
case Binop(OpBoolAnd), Binop(OpBoolOr):
if (first) {
itemStart = firstItemStart;
first = false;
}
items.push(makeWrappableItem(itemStart, child));
var next:Null<TokenInfo> = getNextToken(child);
if (next == null) {
return;
}
itemStart = next.token;
default:
continue;
if (itemStart.children != null) {
for (child in itemStart.children) {
switch (child.tok) {
case Binop(OpBoolAnd), Binop(OpBoolOr):
if (first) {
itemStart = firstItemStart;
first = false;
}
items.push(makeWrappableItem(itemStart, child));
var next:Null<TokenInfo> = getNextToken(child);
if (next == null) {
return;
}
itemStart = next.token;
default:
continue;
}
}
}
items.push(makeWrappableItem(itemStart, TokenTreeCheckUtils.getLastToken(itemStart)));
Expand Down Expand Up @@ -630,13 +632,15 @@ class MarkWrapping extends MarkWrappingBase {
return;
}
var itemStart:TokenTree = next.token;
for (child in itemContainer.children) {
switch (child.tok) {
case DblDot:
break;
default:
var lastToken:TokenTree = TokenTreeCheckUtils.getLastToken(child);
items.push(makeWrappableItem(child, lastToken));
if (itemContainer.children != null) {
for (child in itemContainer.children) {
switch (child.tok) {
case DblDot:
break;
default:
var lastToken:TokenTree = TokenTreeCheckUtils.getLastToken(child);
items.push(makeWrappableItem(child, lastToken));
}
}
}
queueWrapping({
Expand Down Expand Up @@ -709,17 +713,19 @@ class MarkWrapping extends MarkWrappingBase {
return;
}
var itemStart:TokenTree = next.token;
for (child in itemContainer.children) {
switch (child.tok) {
case Binop(OpAdd), Binop(OpSub):
items.push(makeWrappableItem(itemStart, child));
var next:Null<TokenInfo> = getNextToken(child);
if (next == null) {
if (itemContainer.children != null) {
for (child in itemContainer.children) {
switch (child.tok) {
case Binop(OpAdd), Binop(OpSub):
items.push(makeWrappableItem(itemStart, child));
var next:Null<TokenInfo> = getNextToken(child);
if (next == null) {
continue;
}
itemStart = next.token;
default:
continue;
}
itemStart = next.token;
default:
continue;
}
}
}
items.push(makeWrappableItem(itemStart, TokenTreeCheckUtils.getLastToken(itemStart)));
Expand Down Expand Up @@ -850,6 +856,9 @@ class MarkWrapping extends MarkWrappingBase {
});
for (v in allVars) {
var items:Array<WrappableItem> = [];
if (v.children == null) {
continue;
}
for (child in v.children) {
var endToken:TokenTree = TokenTreeCheckUtils.getLastToken(child);
items.push(makeWrappableItem(child, endToken));
Expand Down
88 changes: 51 additions & 37 deletions src/formatter/marker/wrapping/MarkWrappingBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,27 @@ class MarkWrappingBase extends MarkerBase {
}
}
noWrappingBetween(open, close);
for (child in open.children) {
switch (child.tok) {
case PClose, BrClose, BkClose:
break;
case Binop(OpGt):
continue;
case Semicolon, Comma:
continue;
default:
}
var lastChild:Null<TokenTree> = TokenTreeCheckUtils.getLastToken(child);
if (lastChild == null) {
continue;
} else {
switch (lastChild.tok) {
case Comma, Semicolon:
noLineEndAfter(lastChild);
if (open.children != null) {
for (child in open.children) {
switch (child.tok) {
case PClose, BrClose, BkClose:
break;
case Binop(OpGt):
continue;
case Semicolon, Comma:
continue;
default:
}
var lastChild:Null<TokenTree> = TokenTreeCheckUtils.getLastToken(child);
if (lastChild == null) {
continue;
} else {
switch (lastChild.tok) {
case Comma, Semicolon:
noLineEndAfter(lastChild);
default:
}
}
}
}
noLineEndBefore(close);
Expand Down Expand Up @@ -87,26 +89,29 @@ class MarkWrappingBase extends MarkerBase {

public function keep(open:TokenTree, close:TokenTree, addIndent:Int) {
noWrappingBetween(open, close);
for (child in open.children) {
var last:Bool = false;
switch (child.tok) {
case PClose, BrClose, BkClose:
last = true;
case Binop(OpGt):
continue;
case Semicolon, Comma:
continue;
default:
}
if (parsedCode.isOriginalNewlineBefore(child)) {
lineEndBefore(child);
additionalIndent(child, addIndent);
} else {
noLineEndBefore(child);
wrapBefore(child, false);
}
if (last) {
break;

if (open.children != null) {
for (child in open.children) {
var last:Bool = false;
switch (child.tok) {
case PClose, BrClose, BkClose:
last = true;
case Binop(OpGt):
continue;
case Semicolon, Comma:
continue;
default:
}
if (parsedCode.isOriginalNewlineBefore(child)) {
lineEndBefore(child);
additionalIndent(child, addIndent);
} else {
noLineEndBefore(child);
wrapBefore(child, false);
}
if (last) {
break;
}
}
}
if (!parsedCode.isOriginalNewlineBefore(open)) {
Expand Down Expand Up @@ -182,6 +187,9 @@ class MarkWrappingBase extends MarkerBase {
if (!keepFirst) {
lineEndAfter(open);
}
if (open.children == null) {
return;
}
for (child in open.children) {
switch (child.tok) {
case PClose, BrClose, BkClose:
Expand Down Expand Up @@ -452,6 +460,9 @@ class MarkWrappingBase extends MarkerBase {
var indent:Int = indenter.calcIndent(lineStart);
var lineLength:Int = calcLineLengthBefore(open) + indenter.calcAbsoluteIndent(indent + addIndent);
var first:Bool = true;
if (open.children == null) {
return;
}
for (child in open.children) {
switch (child.tok) {
case PClose, BrClose, BkClose:
Expand Down Expand Up @@ -552,6 +563,9 @@ class MarkWrappingBase extends MarkerBase {
function makeWrappableItems(token:TokenTree):Array<WrappableItem> {
var items:Array<WrappableItem> = [];
var lastIndex:Int = -1;
if (token.children == null) {
return items;
}
for (child in token.children) {
switch (child.tok) {
case PClose, BkClose, BrClose:
Expand Down
Loading

0 comments on commit 3a907da

Please sign in to comment.