aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2020-10-01 09:41:16 +0200
committerMaximilian Goldstein <max.goldstein@qt.io>2020-10-07 08:48:11 +0200
commit209209e6fddce1b8f8a1837a2e9f40b81f540158 (patch)
tree08019accc871a01fb411f52d21a53d10b8cc61db
parentdcaa66e2fc93a2d98ea2b5475b8d90f899cf1888 (diff)
qmlformat: Fix formatting of for...of loops using array variables
Fixes: QTBUG-86980 Change-Id: Id27350821051709894c7645a362cfdf7ce0d279c Reviewed-by: Ulf Hermann <ulf.hermann@qt.io> (cherry picked from commit 9cfda79202c9c7955a1100861ed2135506302921) Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r--tests/auto/qml/qmlformat/data/forOf.formatted.qml9
-rw-r--r--tests/auto/qml/qmlformat/data/forOf.qml12
-rw-r--r--tests/auto/qml/qmlformat/tst_qmlformat.cpp7
-rw-r--r--tools/qmlformat/dumpastvisitor.cpp5
4 files changed, 32 insertions, 1 deletions
diff --git a/tests/auto/qml/qmlformat/data/forOf.formatted.qml b/tests/auto/qml/qmlformat/data/forOf.formatted.qml
new file mode 100644
index 0000000000..fa29f16b81
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/forOf.formatted.qml
@@ -0,0 +1,9 @@
+import QtQml 2.0
+
+QtObject {
+ Component.onCompleted: {
+ var list = [[1, 2], [3, 4], [5, 6]];
+ for (const [x, y] of list) console.log("X: " + x + "; Y: " + y)
+ for (let [x, y] of list) console.log("X: " + x + "; Y: " + y)
+ }
+}
diff --git a/tests/auto/qml/qmlformat/data/forOf.qml b/tests/auto/qml/qmlformat/data/forOf.qml
new file mode 100644
index 0000000000..0c3be0d2a2
--- /dev/null
+++ b/tests/auto/qml/qmlformat/data/forOf.qml
@@ -0,0 +1,12 @@
+import QtQml 2.0
+
+QtObject {
+ Component.onCompleted: {
+ var list = [[1,2],[3,4],[5,6]];
+
+ for (const [x,y] of list)
+ console.log("X: "+x+"; Y: "+y);
+ for (let [x,y] of list)
+ console.log("X: "+x+"; Y: "+y);
+ }
+}
diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
index 3b1f232139..03b320c5c8 100644
--- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp
+++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp
@@ -61,6 +61,7 @@ private Q_SLOTS:
void testNestedIf();
void testNestedFunctions();
+ void testForOf();
#if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled
void testExample();
@@ -286,6 +287,12 @@ void TestQmlformat::testNestedFunctions()
readTestFile("nestedFunctions.formatted.qml"));
}
+void TestQmlformat::testForOf()
+{
+ QCOMPARE(runQmlformat(testFile("forOf.qml"), false, true),
+ readTestFile("forOf.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 f45ddd5e46..15777984d2 100644
--- a/tools/qmlformat/dumpastvisitor.cpp
+++ b/tools/qmlformat/dumpastvisitor.cpp
@@ -236,7 +236,10 @@ QString DumpAstVisitor::parsePatternElement(PatternElement *element, bool scope)
}
}
- result += element->bindingIdentifier.toString();
+ if (element->bindingIdentifier.isEmpty())
+ result += parseExpression(element->bindingTarget);
+ else
+ result += element->bindingIdentifier.toString();
if (element->typeAnnotation != nullptr)
result += ": " + parseType(element->typeAnnotation->type);