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,