diff options
author | Sami Shalayel <sami.shalayel@qt.io> | 2023-10-20 13:43:18 +0200 |
---|---|---|
committer | Sami Shalayel <sami.shalayel@qt.io> | 2024-04-05 17:45:42 +0200 |
commit | 9cdd485c2b2d3505c80c4ac31dc9c935a1b80945 (patch) | |
tree | a21da518907a4ccf87518386f34925c0fd5fa4e0 /tests/auto/qml | |
parent | d2d71f77a6e26a2954fcd497dec00e430879932f (diff) |
qmllint: do not construct well known valuetypes from string (part 1)
Add a warning when well-known QML_STRUCTURED_VALUE's type are
constructed from strings in bindings, with a fix-it to
automatically generate the QML_STRUCTURED_VALUE way of constructing
value types.
Do not provide a fix-it in the case that a value type is constructed
from an invalid string, like "50x70" instead of
"50,70" for a QPointF, for example.
Previously, qmlcachegen was compiling some of the string assignments for
structured types like points, and some not, like vector2d. In the latter
case, it printed the generic "Cannot assign literal of type ..." from
the qmlIncompatible category.
This commit only handles point, size and rect. A later commit will take
care of the quick/gui types matrix4x4, vectorNd and quaternion.
Now that the methods are all accessible from one point and consistently
reused, implement qqmljsvaluetypefromstringcheck that checks if a given
type should be constructed from a stringliteral, and generates a
suggestion using the new way of constructing structured value types if
it can. This is implemented into a separate header because it might be
needed for multiple checks.
Currently, it is only needed for the stringliteral "binding" check,
but it will also be needed for stringliteral "assignment" checks,
where you warn about `myPoint ="1,2"`, for example (see QTBUG-118547).
To avoid code-duplication in the quicklintplugin, extract the code also
needed for the qquickliteralbindingcheck into a separate base class
LiteralBindingCheckBase, such that qquickliteralbindingcheck, in a later
commit, may just extend LiteralBindingCheckBase using its
isValueTypeFromStringConstruction override.
Task-number: QTBUG-118323
Fixes: QTBUG-118546
Change-Id: I8572798d94593b1044cdb49116c8468f998dbbd7
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r-- | tests/auto/qml/qmllint/data/validLiterals.qml | 6 | ||||
-rw-r--r-- | tests/auto/qml/qmllint/data/valueTypesFromString.qml | 7 | ||||
-rw-r--r-- | tests/auto/qml/qmllint/tst_qmllint.cpp | 20 | ||||
-rw-r--r-- | tests/auto/qml/qmltc_qprocess/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/auto/qml/qmltc_qprocess/data/constructFromString.qml | 7 | ||||
-rw-r--r-- | tests/auto/qml/qmltc_qprocess/tst_qmltc_qprocess.cpp | 13 |
6 files changed, 51 insertions, 3 deletions
diff --git a/tests/auto/qml/qmllint/data/validLiterals.qml b/tests/auto/qml/qmllint/data/validLiterals.qml index 4f8c575cd3..55792eaae2 100644 --- a/tests/auto/qml/qmllint/data/validLiterals.qml +++ b/tests/auto/qml/qmllint/data/validLiterals.qml @@ -29,9 +29,9 @@ QtObject { property date date1: "2021-08-13T14:16:21.435Z" - property point point1: "1,2" + property point point1: ({ x: 1, y: 2 }) - property size size1: "50x50" + property size size1: ({ width: 50, height: 50 }) - property rect rect1: "10,20,30x30" + property rect rect1: ({ x: 10, y: 20, width: 30, height: 30 }) } diff --git a/tests/auto/qml/qmllint/data/valueTypesFromString.qml b/tests/auto/qml/qmllint/data/valueTypesFromString.qml new file mode 100644 index 0000000000..fc013f858f --- /dev/null +++ b/tests/auto/qml/qmllint/data/valueTypesFromString.qml @@ -0,0 +1,7 @@ +import QtQuick + +Item { + property point p: "30,50" + property rect p3: "10, 20, 30x50" + property size p4: "30x50" +} diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp index a33e73ddb6..60cd313b48 100644 --- a/tests/auto/qml/qmllint/tst_qmllint.cpp +++ b/tests/auto/qml/qmllint/tst_qmllint.cpp @@ -97,6 +97,7 @@ private Q_SLOTS: void lintModule(); void testLineEndings(); + void valueTypesFromString(); #if QT_CONFIG(library) void testPlugin(); @@ -1950,6 +1951,25 @@ void TestQmllint::testLineEndings() } } +void TestQmllint::valueTypesFromString() +{ + runTest("valueTypesFromString.qml", + Result{ { + Message{ + u"Binding is not supported: Type QSizeF should be constructed using QML_STRUCTURED_VALUE's construction mechanism, instead of a string."_s }, + Message{ + u"Binding is not supported: Type QRectF should be constructed using QML_STRUCTURED_VALUE's construction mechanism, instead of a string."_s }, + Message{ + u"Binding is not supported: Type QPointF should be constructed using QML_STRUCTURED_VALUE's construction mechanism, instead of a string."_s }, + }, + { /*bad messages */ }, + { + Message{ u"({ width: 30, height: 50 })"_s }, + Message{ u"({ x: 10, y: 20, width: 30, height: 50 })"_s }, + Message{ u"({ x: 30, y: 50 })"_s }, + } }); +} + #if QT_CONFIG(library) void TestQmllint::testPlugin() { diff --git a/tests/auto/qml/qmltc_qprocess/CMakeLists.txt b/tests/auto/qml/qmltc_qprocess/CMakeLists.txt index 8d25214324..55266c80d9 100644 --- a/tests/auto/qml/qmltc_qprocess/CMakeLists.txt +++ b/tests/auto/qml/qmltc_qprocess/CMakeLists.txt @@ -42,6 +42,7 @@ qt6_add_qml_module(tst_qmltc_qprocess data/invalidSignalHandlers.qml data/QmlBaseFromAnotherModule.qml data/invalidTypeAnnotation.qml + data/constructFromString.qml ) set(common_libraries diff --git a/tests/auto/qml/qmltc_qprocess/data/constructFromString.qml b/tests/auto/qml/qmltc_qprocess/data/constructFromString.qml new file mode 100644 index 0000000000..76ec189f3d --- /dev/null +++ b/tests/auto/qml/qmltc_qprocess/data/constructFromString.qml @@ -0,0 +1,7 @@ +import QtQuick 2.15 + +Item { + property point p: "30,50" + property rect p3: "10, 20, 30x50" + property size p4: "30x50" +} diff --git a/tests/auto/qml/qmltc_qprocess/tst_qmltc_qprocess.cpp b/tests/auto/qml/qmltc_qprocess/tst_qmltc_qprocess.cpp index 404ae7dc63..fdb64f4a29 100644 --- a/tests/auto/qml/qmltc_qprocess/tst_qmltc_qprocess.cpp +++ b/tests/auto/qml/qmltc_qprocess/tst_qmltc_qprocess.cpp @@ -54,6 +54,7 @@ private slots: void exports(); void qmlBaseFromAnotherModule(); void invalidTypeAnnotation(); + void constructFromString(); }; #ifndef TST_QMLTC_QPROCESS_RESOURCES @@ -327,5 +328,17 @@ void tst_qmltc_qprocess::exports() QVERIFY(!implementation.contains(u"exportheader.h"_s)); } +void tst_qmltc_qprocess::constructFromString() +{ + const auto errors = runQmltc(u"constructFromString.qml"_s, false); + const QString warningMessage = + u"constructFromString.qml:%1:%2: Binding is not supported: Type %3 should be" + u" constructed using QML_STRUCTURED_VALUE's construction mechanism, instead of a" + u" string."_s; + QVERIFY(errors.contains(warningMessage.arg(4).arg(23).arg(u"QPointF"))); + QVERIFY(errors.contains(warningMessage.arg(5).arg(23).arg(u"QRectF"))); + QVERIFY(errors.contains(warningMessage.arg(6).arg(23).arg(u"QSizeF"))); +} + QTEST_MAIN(tst_qmltc_qprocess) #include "tst_qmltc_qprocess.moc" |