Split empty catch blocks with catches after them.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index a5c022f..4903665 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,7 @@
* Don't indent cascades farther than their receiver method chains.
* Optimize line splitting cascades (#811).
* Split empty catch blocks with finally clauses (#1029).
+* Split empty catch blocks with catches after them.
# 2.0.1
diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart
index c97f0b5..1b918d1 100644
--- a/lib/src/source_visitor.dart
+++ b/lib/src/source_visitor.dart
@@ -3419,11 +3419,16 @@
ifStatement.thenStatement == node;
}
- // Force a split in an empty catch if there is a finally:
+ // Force a split in an empty catch if there is a finally or other catch
+ // after it:
if (node.parent is CatchClause && node.parent!.parent is TryStatement) {
var tryStatement = node.parent!.parent as TryStatement;
- return tryStatement.finallyBlock != null &&
- tryStatement.catchClauses.any((clause) => clause.body == node);
+ if (tryStatement.finallyBlock != null) {
+ return tryStatement.catchClauses.any((clause) => clause.body == node);
+ } else {
+ return tryStatement.catchClauses.any((clause) => clause.body == node) &&
+ node != tryStatement.catchClauses.last.body;
+ }
}
return false;
diff --git a/test/regression/1000/1029.unit b/test/regression/1000/1029.unit
index 8dda931..ffff020 100644
--- a/test/regression/1000/1029.unit
+++ b/test/regression/1000/1029.unit
@@ -14,4 +14,21 @@
} finally {
cleanupSomething();
}
+}
+>>>
+void main() {
+ try {
+ doSomething();
+ } on FooException {} on BarException {
+ doSomething();
+ }
+}
+<<<
+void main() {
+ try {
+ doSomething();
+ } on FooException {
+ } on BarException {
+ doSomething();
+ }
}
\ No newline at end of file
diff --git a/test/splitting/statements.stmt b/test/splitting/statements.stmt
index dde279c..c5f19dc 100644
--- a/test/splitting/statements.stmt
+++ b/test/splitting/statements.stmt
@@ -99,4 +99,12 @@
} catch (err3) {
} finally {
;
-}
\ No newline at end of file
+}
+>>> split leading empty catches if there are multiple
+try {;} catch (err1) {} catch (err2) {} catch (err3) {}
+<<<
+try {
+ ;
+} catch (err1) {
+} catch (err2) {
+} catch (err3) {}
\ No newline at end of file