Force split in empty blocks in if statements with else clauses. (#683)
Fix #680.
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 668684d..3de6cec 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,7 +1,8 @@
-# 1.0.11-dev
+# 1.0.11
* Fix cast failure when running in Dart 2.
* Updated SDK version to 2.0.0-dev.17.0.
+* Force split in empty then block in if with an else (#680).
# 1.0.10
diff --git a/bin/format.dart b/bin/format.dart
index 22c266c..0e8ec50 100644
--- a/bin/format.dart
+++ b/bin/format.dart
@@ -14,7 +14,7 @@
import 'package:dart_style/src/source_code.dart';
// Note: The following line of code is modified by tool/grind.dart.
-const version = "1.0.10";
+const version = "1.0.11";
void main(List<String> args) {
var parser = new ArgParser(allowTrailingOptions: true);
diff --git a/lib/src/source_visitor.dart b/lib/src/source_visitor.dart
index 319a53e..6aa818e 100644
--- a/lib/src/source_visitor.dart
+++ b/lib/src/source_visitor.dart
@@ -261,10 +261,25 @@
}
visitBlock(Block node) {
- // Don't allow splitting in an empty block.
+ // Treat empty blocks specially. In most cases, they are not allowed to
+ // split. However, an empty block as the then statement of an if with an
+ // else is always split.
if (node.statements.isEmpty &&
node.rightBracket.precedingComments == null) {
token(node.leftBracket);
+
+ // Force a split when used as the then body of an if with an else:
+ //
+ // if (condition) {
+ // } else ...
+ if (node.parent is IfStatement) {
+ var ifStatement = node.parent as IfStatement;
+ if (ifStatement.elseStatement != null &&
+ ifStatement.thenStatement == node) {
+ newline();
+ }
+ }
+
token(node.rightBracket);
return;
}
diff --git a/pubspec.yaml b/pubspec.yaml
index 6c9add0..5f908cd 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,6 +1,6 @@
name: dart_style
# Note: See tool/grind.dart for how to bump the version.
-version: 1.0.11-dev
+version: 1.0.11
author: Dart Team <misc@dartlang.org>
description: Opinionated, automatic Dart source code formatter.
homepage: https://github.com/dart-lang/dart_style
diff --git a/test/regression/0600/0680.stmt b/test/regression/0600/0680.stmt
new file mode 100644
index 0000000..bf4263c
--- /dev/null
+++ b/test/regression/0600/0680.stmt
@@ -0,0 +1,22 @@
+>>> (indent 4)
+ if (receiver != null &&
+ receiver is! ThisExpression &&
+ interfaceMember is Procedure &&
+ interfaceMember
+ .isGenericContravariant) {} else if (identical(interfaceMember,
+ 'call')) {} else if (interfaceMember == null) {} else if (interfaceMember
+ is Procedure &&
+ interfaceMember.isGenericContravariant) {
+ return MethodContravarianceCheckKind.checkMethodReturn;
+ }
+<<<
+ if (receiver != null &&
+ receiver is! ThisExpression &&
+ interfaceMember is Procedure &&
+ interfaceMember.isGenericContravariant) {
+ } else if (identical(interfaceMember, 'call')) {
+ } else if (interfaceMember == null) {
+ } else if (interfaceMember is Procedure &&
+ interfaceMember.isGenericContravariant) {
+ return MethodContravarianceCheckKind.checkMethodReturn;
+ }
\ No newline at end of file
diff --git a/test/splitting/statements.stmt b/test/splitting/statements.stmt
index add34c2..8d8d31c 100644
--- a/test/splitting/statements.stmt
+++ b/test/splitting/statements.stmt
@@ -43,4 +43,18 @@
"a long string that must wrap") {
case 0:
return "ok";
-}
\ No newline at end of file
+}
+>>> don't split empty block in if without else
+if (condition) {
+
+
+}
+<<<
+if (condition) {}
+>>> split empty block in if if there is an else
+if (condition) {} else {
+
+}
+<<<
+if (condition) {
+} else {}
\ No newline at end of file