From b0ad53347fdb26c52c6c0456adf19b049a78ea3d Mon Sep 17 00:00:00 2001 From: Maximilian Goldstein Date: Wed, 7 Oct 2020 10:24:29 +0200 Subject: qmlformat: Fix computed property names Fixes: QTBUG-87222 Change-Id: If1da02d503041009b82651e1087fb4a1bdd79d59 Reviewed-by: Ulf Hermann (cherry picked from commit 49391fcc41d871836868452b8300938d0b94f00e) Reviewed-by: Fabian Kosmale --- tests/auto/qml/qmlformat/data/propertyNames.formatted.qml | 14 ++++++++++++++ tests/auto/qml/qmlformat/data/propertyNames.qml | 13 +++++++++++++ tests/auto/qml/qmlformat/tst_qmlformat.cpp | 7 +++++++ tools/qmlformat/dumpastvisitor.cpp | 6 +++++- 4 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 tests/auto/qml/qmlformat/data/propertyNames.formatted.qml create mode 100644 tests/auto/qml/qmlformat/data/propertyNames.qml diff --git a/tests/auto/qml/qmlformat/data/propertyNames.formatted.qml b/tests/auto/qml/qmlformat/data/propertyNames.formatted.qml new file mode 100644 index 0000000000..c7d50abb32 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/propertyNames.formatted.qml @@ -0,0 +1,14 @@ +Item { + function x() { + var copiedItem = "copied value"; + var computedItem = "computedName"; + var obj = { + "identifierName": "identifier value", + "string name": "string value", + "Infinity": "numeric value", + [computedItem]: "computed value", + "copiedItem": copiedItem + }; + } + +} diff --git a/tests/auto/qml/qmlformat/data/propertyNames.qml b/tests/auto/qml/qmlformat/data/propertyNames.qml new file mode 100644 index 0000000000..9214014889 --- /dev/null +++ b/tests/auto/qml/qmlformat/data/propertyNames.qml @@ -0,0 +1,13 @@ +Item { + function x() { + var copiedItem = "copied value"; + var computedItem = "computedName"; + var obj = { + identifierName: "identifier value", + "string name": "string value", + Infinity: "numeric value", + [computedItem]: "computed value", + copiedItem + }; + } +} diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index 03b320c5c8..413dc5f8a5 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -62,6 +62,7 @@ private Q_SLOTS: void testNestedFunctions(); void testForOf(); + void testPropertyNames(); #if !defined(QTEST_CROSS_COMPILED) // sources not available when cross compiled void testExample(); @@ -293,6 +294,12 @@ void TestQmlformat::testForOf() readTestFile("forOf.formatted.qml")); } +void TestQmlformat::testPropertyNames() +{ + QCOMPARE(runQmlformat(testFile("propertyNames.qml"), false, true), + readTestFile("propertyNames.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 15777984d2..9b4449df31 100644 --- a/tools/qmlformat/dumpastvisitor.cpp +++ b/tools/qmlformat/dumpastvisitor.cpp @@ -298,7 +298,11 @@ QString DumpAstVisitor::parsePatternProperty(PatternProperty *property) case PatternElement::Setter: return "set "+parseFunctionExpression(cast(property->initializer), true); default: - return escapeString(property->name->asString())+": "+parsePatternElement(property, false); + if (property->name->kind == Node::Kind_ComputedPropertyName) { + return "["+parseExpression(cast(property->name)->expression)+"]: "+parsePatternElement(property, false); + } else { + return escapeString(property->name->asString())+": "+parsePatternElement(property, false); + } } } -- cgit v1.2.3