diff options
author | Sami Shalayel <[email protected]> | 2025-03-14 15:17:39 +0100 |
---|---|---|
committer | Sami Shalayel <[email protected]> | 2025-05-20 16:04:51 +0100 |
commit | e0c4a35a10ef50f44955f9c0ae2ae30eace5cb21 (patch) | |
tree | 5817751c46533baf4322fb3f281ce98ce9493b89 /tests/auto/qml | |
parent | 6864d9a1b8326bc031d3736a9a9d637698e0ebc5 (diff) |
QQmlSA: extend ScriptBindingValueType for function bindings
In QtC, ErrBlocksNotSupportedInQmlUi is supposed to warn about function
blocks when used in bindings. We can't detect function blocks with the
QQmlSA infrastructure because we don't have access to the AST, but we
still can actually detect bindings to function blocks.
Extend ScriptBindingValueType to be able to recognize bindings that
contain functions (JS blocks, arrow functions and lambdas). Extend the
QQmlSA interface to find out if a binding contains a function so that
qdslintplugin can use it in a later commit.
Task-number: QTBUG-129308
Change-Id: Ic46ad6faf7a04d805084db2d9353b009e881d4dd
Reviewed-by: Ulf Hermann <[email protected]>
Reviewed-by: Fabian Kosmale <[email protected]>
Diffstat (limited to 'tests/auto/qml')
-rw-r--r-- | tests/auto/qml/qqmljsscope/data/scriptBindingValueType.qml | 13 | ||||
-rw-r--r-- | tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp | 28 |
2 files changed, 41 insertions, 0 deletions
diff --git a/tests/auto/qml/qqmljsscope/data/scriptBindingValueType.qml b/tests/auto/qml/qqmljsscope/data/scriptBindingValueType.qml new file mode 100644 index 0000000000..9eed9e2a14 --- /dev/null +++ b/tests/auto/qml/qqmljsscope/data/scriptBindingValueType.qml @@ -0,0 +1,13 @@ +import QtQuick + +Item { + property int myInt: 123 + property int block: { return 123; } + property int alsoABlock: {42} + property var lambda: function() {} + property var namedFunction: function hello() {} + property var arrow: x => x + property var myUndefined: undefined + property var emptyBlock: {} + property var emptyObject: ({}) +} diff --git a/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp b/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp index 4a0b74e4a3..4fb8805b7a 100644 --- a/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp +++ b/tests/auto/qml/qqmljsscope/tst_qqmljsscope.cpp @@ -90,6 +90,8 @@ private Q_SLOTS: void initTestCase() override; void orderedBindings(); + void scriptBindingValueType_data(); + void scriptBindingValueType(); void signalCreationDifferences(); void allTypesAvailable(); void shadowing(); @@ -185,6 +187,32 @@ void tst_qqmljsscope::orderedBindings() QCOMPARE(std::next(itemsBindingsBegin)->objectType()->baseTypeName(), u"Text"_s); } +void tst_qqmljsscope::scriptBindingValueType_data() +{ + QTest::addColumn<ScriptBindingValueType>("expectedType"); + + QTest::addRow("myInt") << ScriptBindingValueType::ScriptValue_Unknown; + QTest::addRow("block") << ScriptBindingValueType::ScriptValue_Function; + QTest::addRow("alsoABlock") << ScriptBindingValueType::ScriptValue_Function; + QTest::addRow("lambda") << ScriptBindingValueType::ScriptValue_Function; + QTest::addRow("namedFunction") << ScriptBindingValueType::ScriptValue_Function; + QTest::addRow("arrow") << ScriptBindingValueType::ScriptValue_Function; + QTest::addRow("myUndefined") << ScriptBindingValueType::ScriptValue_Undefined; + QTest::addRow("emptyBlock") << ScriptBindingValueType::ScriptValue_Function; + QTest::addRow("emptyObject") << ScriptBindingValueType::ScriptValue_Unknown; +} +void tst_qqmljsscope::scriptBindingValueType() +{ + QFETCH(ScriptBindingValueType, expectedType); + + QQmlJSScope::ConstPtr root = run(u"scriptBindingValueType.qml"_s); + QVERIFY(root); + + auto [start, end] = root->ownPropertyBindings(QTest::currentDataTag()); + QCOMPARE(std::distance(start, end), 1); + QCOMPARE(start->scriptValueType(), expectedType); +} + void tst_qqmljsscope::signalCreationDifferences() { QQmlJSScope::ConstPtr root = run(u"signalCreationDifferences.qml"_s, true); |