From 115916f217b0dc299b8df298f5c9c30369f561f8 Mon Sep 17 00:00:00 2001 From: Semih Yavuz Date: Mon, 31 Jul 2023 15:12:01 +0200 Subject: qmlformat: fix formatting of object destructuring The following issues are fixed: - [1]Incorrect detection of the property name as a string literal and thus writing out them with quotation marks - [2] Duplication of property name when a scoped variable is used as property key - [3] Writing out additional brackets during deconstruction - [4] Incorrect formatting when a default is assigned to a lhs variable like [a = 24, b] = array - [5] Automatic addition of "" characters into the object keys - [6] Automatic addition of assignment operator, instead only add it when there is a pending initializer Also, add the colon token location which was missing in the pattern property rules. Remove it from a couple of rules that was giving incorrect result. We require the location information of the colon token to be correct when formatting. A few of tst_qmlformat and tst_reformatter tests are adapted to the above mentioned changes [1], [2], [5]. [ChangeLog][qmlformat][Important Behavior Changes] qmlformat will no longer add "" characters automatically in the object keys unless the object key is actually a string literal. Also, using a scoped variable as the property key will no longer result in the duplication of the property name. Pick-to: 6.6 Fixes: QTBUG-108275 Fixes: QTBUG-114839 Change-Id: I272d41d13df34ff5877f3efebe43c80255dd7c2b Reviewed-by: Fabian Kosmale --- .../data/objectDestructuring.formatted.qml | 72 ++++++++++++++++++++++ .../qml/qmlformat/data/objectDestructuring.qml | 45 ++++++++++++++ .../qml/qmlformat/data/propertyNames.formatted.qml | 6 +- tests/auto/qml/qmlformat/tst_qmlformat.cpp | 3 + 4 files changed, 123 insertions(+), 3 deletions(-) create mode 100644 tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml create mode 100644 tests/auto/qml/qmlformat/data/objectDestructuring.qml (limited to 'tests/auto/qml/qmlformat') diff --git a/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml b/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml new file mode 100644 index 0000000000..b10751dcdf --- /dev/null +++ b/tests/auto/qml/qmlformat/data/objectDestructuring.formatted.qml @@ -0,0 +1,72 @@ +import QtQml + +QtObject { + + Component.onCompleted: { + set1(); + } + + function set1() { + const array = [1, 2, 3, 4]; + const [a, b] = [1, 2]; + const [aa, , bb] = array; + const [aaa = 23, bbb] = array; + const [a1, b1, ...rest1] = array; + const [a2, , b2, ...rest2] = array; + const [a3, b3, ...{ + pop, + push + }] = array; + const [a4, b4, ...[c, d]] = array; + const obj = { + _a: 1, + _b: 2 + }; + const { + a5, + b5 + } = obj; + const { + a6: a_, + b6: b1_ + } = obj; + const { + a7: a11 = 4, + b11 = 34, + c1: b111, + d1 + } = obj; + let key = a; + const { + [key]: a___ + } = obj; + } + + function set2() { + // declare first + let a, b, a1, b1, c, d, rest, pop, push; + const array = [1, 2, 3, 4]; + [a, b] = array; + [a, , b] = array; + [a = aDefault, b] = array; + [a, b, ...rest] = array; + [a, , b, ...rest] = array; + [a, b, ...{ + pop, + push + }] = array; + [a, b, ...[c, d]] = array; + const obj = { + _a: 1, + _b: 2 + }; + ({ + a, + b + } = obj); // brackets are required + ({ + a: a1, + b: b1 + } = obj); + } +} diff --git a/tests/auto/qml/qmlformat/data/objectDestructuring.qml b/tests/auto/qml/qmlformat/data/objectDestructuring.qml new file mode 100644 index 0000000000..2d377985aa --- /dev/null +++ b/tests/auto/qml/qmlformat/data/objectDestructuring.qml @@ -0,0 +1,45 @@ +import QtQml + +QtObject { + + + Component.onCompleted: { + set1(); + } + + function set1() { + const array = [1,2,3,4]; + const [a, b] = [1,2]; + const [aa, , bb] = array; + const [aaa = 23, bbb] = array; + const [a1, b1, ...rest1] = array; + const [a2, , b2, ...rest2] = array; + const [a3, b3, ...{ pop, push }] = array; + const [a4, b4, ...[c, d]] = array; + + const obj = {_a:1,_b:2}; + const { a5, b5 } = obj; + const { a6: a_, b6: b1_ } = obj; + const { a7: a11 = 4, b11 = 34, c1: b111, d1 } = obj; + let key = a; + const { [key]: a___ } = obj; + } + + function set2() { + // declare first + let a, b, a1, b1, c, d, rest, pop, push; + const array = [1,2,3,4]; + [a, b] = array; + [a, , b] = array; + [a = aDefault, b] = array; + [a, b, ...rest] = array; + [a, , b, ...rest] = array; + [a, b, ...{ pop, push }] = array; + [a, b, ...[c, d]] = array; + + const obj = {_a:1,_b:2}; + ({ a, b } = obj); // brackets are required + ({ a: a1, b: b1 } = obj); + + } +} diff --git a/tests/auto/qml/qmlformat/data/propertyNames.formatted.qml b/tests/auto/qml/qmlformat/data/propertyNames.formatted.qml index 94b5877957..9214014889 100644 --- a/tests/auto/qml/qmlformat/data/propertyNames.formatted.qml +++ b/tests/auto/qml/qmlformat/data/propertyNames.formatted.qml @@ -3,11 +3,11 @@ Item { var copiedItem = "copied value"; var computedItem = "computedName"; var obj = { - "identifierName": "identifier value", + identifierName: "identifier value", "string name": "string value", - "Infinity": "numeric value", + Infinity: "numeric value", [computedItem]: "computed value", - "copiedItem": copiedItem + copiedItem }; } } diff --git a/tests/auto/qml/qmlformat/tst_qmlformat.cpp b/tests/auto/qml/qmlformat/tst_qmlformat.cpp index e8eeb9835d..a45ae07d16 100644 --- a/tests/auto/qml/qmlformat/tst_qmlformat.cpp +++ b/tests/auto/qml/qmlformat/tst_qmlformat.cpp @@ -335,6 +335,9 @@ void TestQmlformat::testFormat_data() QTest::newRow("pragmaValueList") << "pragma.qml" << "pragma.formatted.qml" << QStringList{} << RunOption::OnCopy; + QTest::newRow("objectDestructuring") + << "objectDestructuring.qml" + << "objectDestructuring.formatted.qml" << QStringList{} << RunOption::OnCopy; } void TestQmlformat::testFormat() -- cgit v1.2.3