From 9f232746a5987cee5e2cc2895c876b7585a771b3 Mon Sep 17 00:00:00 2001 From: Rafal Stawarski Date: Wed, 19 Feb 2025 11:39:10 +0100 Subject: [PATCH] QmlJS::Check: Suspend warnings for ScriptAction defined in the .ui file Connections were the only type that was allowed to have function definitions, call expressions, etc. in the .ui file. It's now extended with ScriptAction. Task-number: QDS-10449 Change-Id: If55116a291c5c1d6441f6ab0c3a651861da69fba Reviewed-by: Thomas Hartmann --- src/libs/qmljs/qmljscheck.cpp | 28 ++++++++++--------- src/libs/qmljs/qmljscheck.h | 6 ++++ src/libs/qmljs/qmljsstaticanalysismessage.cpp | 3 +- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/libs/qmljs/qmljscheck.cpp b/src/libs/qmljs/qmljscheck.cpp index 968a96c1fa9..2aa27fe58e6 100644 --- a/src/libs/qmljs/qmljscheck.cpp +++ b/src/libs/qmljs/qmljscheck.cpp @@ -21,6 +21,7 @@ using namespace QmlJS; using namespace QmlJS::AST; using namespace QmlJS::StaticAnalysis; +using namespace Qt::Literals::StringLiterals; namespace { @@ -1373,10 +1374,10 @@ bool Check::visit(FunctionExpression *ast) } } - const bool isDirectInConnectionsScope = - (!m_typeStack.isEmpty() && m_typeStack.last() == "Connections"); + const bool isDirectInConnectionsOrScriptActionScope + = isDirectInTypeScope("Connections"_L1, "ScriptAction"_L1); - if (!isDirectInConnectionsScope) + if (!isDirectInConnectionsOrScriptActionScope) addMessage(ErrFunctionsNotSupportedInQmlUi, locationFromRange(locfunc, loclparen)); DeclarationsCheck bodyCheck; @@ -1433,10 +1434,10 @@ bool Check::visit(BinaryExpression *ast) SourceLocation expressionSourceLocation = locationFromRange(ast->firstSourceLocation(), ast->lastSourceLocation()); - const bool isDirectInConnectionsScope = (!m_typeStack.isEmpty() - && m_typeStack.last() == "Connections"); + const bool isDirectInConnectionsOrScriptActionScope + = isDirectInTypeScope("Connections"_L1, "ScriptAction"_L1); - if (expressionAffectsVisualAspects(ast) && !isDirectInConnectionsScope) + if (expressionAffectsVisualAspects(ast) && !isDirectInConnectionsOrScriptActionScope) addMessage(WarnImperativeCodeNotEditableInVisualDesigner, expressionSourceLocation); // check ==, != @@ -1481,11 +1482,10 @@ bool Check::visit(BinaryExpression *ast) bool Check::visit(Block *ast) { + const bool isDirectInConnectionsOrScriptActionScope + = isDirectInTypeScope("Connections"_L1, "ScriptAction"_L1); - bool isDirectInConnectionsScope = - (!m_typeStack.isEmpty() && m_typeStack.last() == "Connections"); - - if (!isDirectInConnectionsScope) + if (!isDirectInConnectionsOrScriptActionScope) addMessage(ErrBlocksNotSupportedInQmlUi, locationFromRange(ast->firstSourceLocation(), ast->lastSourceLocation())); if (Node *p = parent()) { @@ -1910,9 +1910,11 @@ bool Check::visit(CallExpression *ast) const bool isMathFunction = namespaceName == "Math"; const bool isDateFunction = namespaceName == "Date"; // allow adding connections with the help of the qt quick designer ui - bool isDirectInConnectionsScope = - (!m_typeStack.isEmpty() && m_typeStack.last() == QLatin1String("Connections")); - if (!whiteListedFunction && !isMathFunction && !isDateFunction && !isDirectInConnectionsScope) + const bool isDirectInConnectionsOrScriptActionScope + = isDirectInTypeScope("Connections"_L1, "ScriptAction"_L1); + + if (!whiteListedFunction && !isMathFunction && !isDateFunction + && !isDirectInConnectionsOrScriptActionScope) addMessage(ErrFunctionsNotSupportedInQmlUi, location); if (translationFunctions.contains(name)) { diff --git a/src/libs/qmljs/qmljscheck.h b/src/libs/qmljs/qmljscheck.h index d7fc6cc4256..65dcfd84eef 100644 --- a/src/libs/qmljs/qmljscheck.h +++ b/src/libs/qmljs/qmljscheck.h @@ -109,6 +109,12 @@ private: bool isCaseOrDefault(AST::Node *n); bool hasVarStatement(AST::Block *b) const; + template + bool isDirectInTypeScope(Args... args) + { + return !m_typeStack.isEmpty() && ((m_typeStack.last() == args) || ...); + } + AST::Node *parent(int distance = 0); Document::Ptr _doc; diff --git a/src/libs/qmljs/qmljsstaticanalysismessage.cpp b/src/libs/qmljs/qmljsstaticanalysismessage.cpp index ffd66d0baac..83e99e58842 100644 --- a/src/libs/qmljs/qmljsstaticanalysismessage.cpp +++ b/src/libs/qmljs/qmljsstaticanalysismessage.cpp @@ -212,7 +212,8 @@ StaticAnalysisMessages::StaticAnalysisMessages() newMsg( ErrFunctionsNotSupportedInQmlUi, Error, - Tr::tr("Arbitrary functions and function calls outside of a Connections object are not supported in a UI file (.ui.qml).")); + Tr::tr("Arbitrary functions and function calls outside of a Connections or ScriptAction " + "objects are not supported in a UI file (.ui.qml).")); newMsg(ErrBlocksNotSupportedInQmlUi, Error, Tr::tr("JavaScript blocks are not supported in a UI file (.ui.qml).")); newMsg(ErrBehavioursNotSupportedInQmlUi, Error,