aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2020-06-12 15:24:25 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2020-07-02 13:30:42 +0200
commit6b72b1a6861ddc10356c5505a00e2204fdfe83e5 (patch)
treea8ca699ff0aff888516099f03a040c61651a1d2a
parent9ac953aefdec4481d53cea61b494d6657a61d03e (diff)
qmlformat: Fix trailing newline in if blocks
Fixes: QTBUG-85003 Change-Id: Ie68c838c9bc1d52181a741871279bee5a8020855 Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io> (cherry picked from commit 15777b94ee96035be79b363f3b1e54f02b5d2cd5)
-rw-r--r--tests/auto/qml/qmlformat/data/Example1.formatted.nosort.qml1
-rw-r--r--tests/auto/qml/qmlformat/data/Example1.formatted.qml1
-rw-r--r--tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml1
-rw-r--r--tests/auto/qml/qmlformat/data/QtBug85003.formatted.qml8
-rw-r--r--tests/auto/qml/qmlformat/data/QtBug85003.qml6
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp8
-rw-r--r--tools/qmlformat/dumpastvisitor.cpp4
7 files changed, 26 insertions, 3 deletions
diff --git a/tests/auto/qml/qmlformat/data/Example1.formatted.nosort.qml b/tests/auto/qml/qmlformat/data/Example1.formatted.nosort.qml
index 34d58cf571..fdc0b1f68c 100644
--- a/tests/auto/qml/qmlformat/data/Example1.formatted.nosort.qml
+++ b/tests/auto/qml/qmlformat/data/Example1.formatted.nosort.qml
@@ -97,7 +97,6 @@ Item {
console.log("other thing");
else
console.log("false");
-
if (x == 50) {
console.log("true");
} else if (x == 50) {
diff --git a/tests/auto/qml/qmlformat/data/Example1.formatted.qml b/tests/auto/qml/qmlformat/data/Example1.formatted.qml
index b06734eb0b..5f12517781 100644
--- a/tests/auto/qml/qmlformat/data/Example1.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/Example1.formatted.qml
@@ -97,7 +97,6 @@ Item {
console.log("other thing");
else
console.log("false");
-
if (x == 50) {
console.log("true");
} else if (x == 50) {
diff --git a/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
index b8e77ec23a..b26361d5bf 100644
--- a/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
+++ b/tests/auto/qml/qmlformat/data/IfBlocks.formatted.qml
@@ -24,7 +24,6 @@ Item {
console.log("bar");
else
console.log("baz");
-
//// The following if blocks should HAVE braces
// Single branch, braces
if (true) {
diff --git a/tests/auto/qml/qmlformat/data/QtBug85003.formatted.qml b/tests/auto/qml/qmlformat/data/QtBug85003.formatted.qml
new file mode 100644
index 0000000000..ee42b39571
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/QtBug85003.formatted.qml
@@ -0,0 +1,8 @@
+Item {
+ Component.onCompleted: {
+ if (3 < 2)
+ console.log("Foo");
+ else
+ console.log("Bar");
+ }
+}
diff --git a/tests/auto/qml/qmlformat/data/QtBug85003.qml b/tests/auto/qml/qmlformat/data/QtBug85003.qml
new file mode 100644
index 0000000000..5d039cb9ca
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/QtBug85003.qml
@@ -0,0 +1,6 @@
+Item {
+ Component.onCompleted:
+ {
+ if (3 < 2) console.log("Foo"); else { console.log("Bar"); }
+ }
+}
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index 7690f78e9d..ecb53b295f 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -55,6 +55,8 @@ private Q_SLOTS:
void testLargeBindings();
void testInlineComponents();
+ void testQtbug85003();
+
#if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
void testExample();
void testExample_data();
@@ -255,6 +257,12 @@ void TestQmlformat::testLineEndings()
QVERIFY(!unixContents.contains("\r"));
}
+void TestQmlformat::testQtbug85003()
+{
+ QCOMPARE(runQmlformat(testFile("QtBug85003.qml"), false, true),
+ readTestFile("QtBug85003.formatted.qml"));
+}
+
#if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
void TestQmlformat::testExample_data()
{
diff --git a/tools/qmlformat/dumpastvisitor.cpp b/tools/qmlformat/dumpastvisitor.cpp
index 3ad66bc6a1..9f49fe0090 100644
--- a/tools/qmlformat/dumpastvisitor.cpp
+++ b/tools/qmlformat/dumpastvisitor.cpp
@@ -692,6 +692,10 @@ QString DumpAstVisitor::parseStatement(Statement *statement, bool blockHasNext,
result += formatLine("else", false);
if (ifFalseBlock) {
+ // Blocks generate an extra newline that we don't want here.
+ if (!m_blockNeededBraces && ifFalse.endsWith(QLatin1String("\n")))
+ ifFalse.chop(1);
+
result += " " + ifFalse;
} else {
result += "\n";