Skip to content

Commit

Permalink
fixed expression try…catch, fixes #509
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHaxe committed Oct 6, 2024
1 parent f9b5405 commit 8af515b
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/formatter/marker/MarkSameLine.hx
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ class MarkSameLine extends MarkerBase {
function markCatch(token:TokenTree) {
if (shouldCatchBeSameLine(token) && config.sameLine.expressionTry == Same) {
markBodyAfterPOpen(token, Same, false);
applySameLinePolicy(token, config.sameLine.tryCatch);
applySameLinePolicy(token, config.sameLine.expressionTry);
return;
}
markBodyAfterPOpen(token, config.sameLine.catchBody, false);
Expand Down
2 changes: 1 addition & 1 deletion src/formatter/marker/MarkWhitespace.hx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ class MarkWhitespace extends MarkerBase {
policy = policy.add(Before);
case Call | Expression:
}
case Question:
case Question | DblDot:
policy = policy.add(Before);
default:
}
Expand Down
8 changes: 8 additions & 0 deletions test/testcases/sameline/issue_123_return_try_catch.hxtest
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

class Main {
public static function main() {
return try call(); catch (e:Any) null;
return try {call();} catch (e:Any) {null;}
return try {
call();
} catch (e:Any) {
Expand All @@ -17,6 +19,12 @@ class Main {

class Main {
public static function main() {
return try call(); catch (e:Any) null;
return try {
call();
} catch (e:Any) {
null;
}
return try {
call();
} catch (e:Any) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"sameLine": {
"expressionTry": "next"
}
}

---

class Main {
public static function main() {
return try call(); catch (e:Any) null;
return try {call();} catch (e:Any) {null;}
return try {
call();
} catch (e:Any) {
null;
}
}
}

---

class Main {
public static function main() {
return try
call();
catch (e:Any)
null;
return try {
call();
} catch (e:Any) {
null;
}
return try {
call();
} catch (e:Any) {
null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"sameLine": {
"expressionTry": "same"
}
}

---

class Main {
public static function main() {
return try call(); catch (e:Any) null;
return try {call();} catch (e:Any) {null;}
return try {
call();
} catch (e:Any) {
null;
}
}
}

---

class Main {
public static function main() {
return try call(); catch (e:Any) null;
return try {
call();
} catch (e:Any) {
null;
}
return try {
call();
} catch (e:Any) {
null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

class Main {
public static function main() {
return try call(); catch (e:Any) null;
return try {call();} catch (e:Any) {null;}
return try {
call();
} catch (e:Any) {
Expand All @@ -20,10 +22,15 @@ class Main {

class Main {
public static function main() {
return try call(); catch (e:Any) null;
return try {
call();
} catch (e:Any) {
null;
}
catch (e:Any) {
return try {
call();
} catch (e:Any) {
null;
}
}
Expand Down
21 changes: 21 additions & 0 deletions test/testcases/sameline/issue_509_try_catch_expression.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"sameLine": {
"tryCatch": "next"
}
}

---

class Main {
static function main() {
var applicationName = try ProjectUtils.getApplicationFile(FileSystem.fullPath(project.projectXmlPath)) catch (_:Dynamic) project.name;
}
}

---

class Main {
static function main() {
var applicationName = try ProjectUtils.getApplicationFile(FileSystem.fullPath(project.projectXmlPath)) catch (_:Dynamic) project.name;
}
}
25 changes: 25 additions & 0 deletions test/testcases/sameline/issue_509_try_catch_expression_next.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"sameLine": {
"tryCatch": "next",
"expressionTry": "next"
}
}

---

class Main {
static function main() {
var applicationName = try ProjectUtils.getApplicationFile(FileSystem.fullPath(project.projectXmlPath)) catch (_:Dynamic) project.name;
}
}

---

class Main {
static function main() {
var applicationName = try
ProjectUtils.getApplicationFile(FileSystem.fullPath(project.projectXmlPath))
catch (_:Dynamic)
project.name;
}
}
22 changes: 22 additions & 0 deletions test/testcases/sameline/issue_509_try_catch_expression_same.hxtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"sameLine": {
"tryCatch": "next",
"expressionTry": "same"
}
}

---

class Main {
static function main() {
var applicationName = try ProjectUtils.getApplicationFile(FileSystem.fullPath(project.projectXmlPath)) catch (_:Dynamic) project.name;
}
}

---

class Main {
static function main() {
var applicationName = try ProjectUtils.getApplicationFile(FileSystem.fullPath(project.projectXmlPath)) catch (_:Dynamic) project.name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ function test(arr) {
trace(arr[1]);
makeArray()[100]();
var a = [for (i in 0...100) i * i];
_metadata = _data == null ? [] : [Constants.DEFAULT_VARIATION => _data];
_metadata = _data == null ? [] : [ Constants.DEFAULT_VARIATION => _data ];
}

0 comments on commit 8af515b

Please sign in to comment.