aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2020-06-17 14:06:11 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2020-07-02 15:11:29 +0200
commiteb2e53ae81239b561022940db27e65c23506450c (patch)
tree00e6e357c038a42d9f214aa62101423b857463f1
parentd9a051a7056212d521c1f8aa3f8e5d66b1a01e96 (diff)
qmlformat: Fix nested ifs
Fixes: QTBUG-85077 Change-Id: Ia2fec64a389fd7355f3fcf9438408b021c5abef4 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit df1f4c9a9f3e3c60fdd43513fdc4926c0cbaa553)
-rw-r--r--tests/auto/qml/qmlformat/data/nestedIf.formatted.qml29
-rw-r--r--tests/auto/qml/qmlformat/data/nestedIf.qml30
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp8
-rw-r--r--tools/qmlformat/dumpastvisitor.cpp40
4 files changed, 88 insertions, 19 deletions
diff --git a/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml b/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml
new file mode 100644
index 0000000000..6c17f18a9c
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/nestedIf.formatted.qml
@@ -0,0 +1,29 @@
+Item {
+ Component.onCompleted: {
+ // Make sure that nested if statements get properly braced
+ if (a) {
+ if (b)
+ foo();
+ else
+ bar();
+ } else if (x == 3) {
+ stuff();
+ } else {
+ foo_bar();
+ }
+ // Same for "for"
+ if (a) {
+ for (x in y) {
+ bar();
+ y();
+ }
+ }
+ // ...and while
+ if (b) {
+ while (y in x) {
+ foo();
+ x();
+ }
+ }
+ }
+}
diff --git a/tests/auto/qml/qmlformat/data/nestedIf.qml b/tests/auto/qml/qmlformat/data/nestedIf.qml
new file mode 100644
index 0000000000..9388dd42f6
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/nestedIf.qml
@@ -0,0 +1,30 @@
+Item {
+Component.onCompleted: {
+ // Make sure that nested if statements get properly braced
+ if (a) {
+ if (b)
+ foo();
+ else
+ bar();
+ } else if (x == 3)
+ stuff();
+ else
+ foo_bar();
+
+ // Same for "for"
+ if (a) {
+ for (x in y) {
+ bar();
+ y();
+ }
+ }
+
+ // ...and while
+ if (b) {
+ while (y in x) {
+ foo();
+ x();
+ }
+ }
+}
+} \ No newline at end of file
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index ecb53b295f..a1bcada5b4 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -57,6 +57,8 @@ private Q_SLOTS:
void testQtbug85003();
+ void testNestedIf();
+
#if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
void testExample();
void testExample_data();
@@ -240,6 +242,12 @@ void TestQmlformat::testLargeBindings()
readTestFile("largeBindings.formatted.qml"));
}
+void TestQmlformat::testNestedIf()
+{
+ QCOMPARE(runQmlformat(testFile("nestedIf.qml"), false, true),
+ readTestFile("nestedIf.formatted.qml"));
+}
+
void TestQmlformat::testLineEndings()
{
// macos
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp
index 9f49fe0090..06a6f6335f 100644
--- a/tools/qmlformat/dumpastvisitor.cpp
+++ b/tools/qmlformat/dumpastvisitor.cpp
@@ -578,6 +578,23 @@ QString DumpAstVisitor::parseExportsList(ExportsList *list)
return result;
}
+bool needsSemicolon(int kind)
+{
+ switch (kind) {
+ case Node::Kind_ForStatement:
+ case Node::Kind_ForEachStatement:
+ case Node::Kind_IfStatement:
+ case Node::Kind_SwitchStatement:
+ case Node::Kind_WhileStatement:
+ case Node::Kind_DoWhileStatement:
+ case Node::Kind_TryStatement:
+ case Node::Kind_WithStatement:
+ return false;
+ default:
+ return true;
+ }
+}
+
QString DumpAstVisitor::parseBlock(Block *block, bool hasNext, bool allowBraceless)
{
bool hasOneLine = (block->statements == nullptr || block->statements->next == nullptr) && allowBraceless;
@@ -593,7 +610,10 @@ QString DumpAstVisitor::parseBlock(Block *block, bool hasNext, bool allowBracele
if (!hasNext && !hasOneLine)
result += formatLine("}", false);
- m_blockNeededBraces |= (block->statements && block->statements->next != nullptr);
+ if (block->statements) {
+ m_blockNeededBraces |= !needsSemicolon(block->statements->statement->kind)
+ || (block->statements->next != nullptr);
+ }
return result;
}
@@ -834,24 +854,6 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext,
}
}
-bool needsSemicolon(int kind)
-{
- switch (kind)
- {
- case Node::Kind_ForStatement:
- case Node::Kind_ForEachStatement:
- case Node::Kind_IfStatement:
- case Node::Kind_SwitchStatement:
- case Node::Kind_WhileStatement:
- case Node::Kind_DoWhileStatement:
- case Node::Kind_TryStatement:
- case Node::Kind_WithStatement:
- return false;
- default:
- return true;
- }
-}
-
QString DumpAstVisitor::parseStatementList(StatementList *list)
{
QString result = "";