QmlJSCheck: Do not allow object in when condition

This is syntactically correct and can be hard to spot, but
is not valid QtQuick.

Task-number: QDS-13452
Change-Id: I010e44e33147c97edbd992c3452ba1c086fb42cb
Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
This commit is contained in:
Thomas Hartmann
2024-09-05 17:04:11 +02:00
parent 7d7e3807c2
commit 269694eb10
3 changed files with 10 additions and 1 deletions

View File

@@ -953,6 +953,13 @@ bool Check::visit(UiObjectBinding *ast)
//addMessage(ErrBehavioursNotSupportedInQmlUi, locationFromRange(ast->firstSourceLocation(), ast->lastSourceLocation()));
}
if (!m_typeStack.isEmpty() && m_typeStack.last() == "State"
&& toString(ast->qualifiedId) == "when") {
addMessage(
ErrWhenConditionCannotBeObject,
locationFromRange(ast->firstSourceLocation(), ast->lastSourceLocation()));
}
visitQmlObject(ast, ast->qualifiedTypeNameId, ast->initializer);
return false;
}

View File

@@ -238,6 +238,7 @@ StaticAnalysisMessages::StaticAnalysisMessages()
Tr::tr("Do not reference the root item as alias."));
newMsg(WarnAliasReferRootHierarchy, Warning,
Tr::tr("Avoid referencing the root item in a hierarchy."));
newMsg(ErrWhenConditionCannotBeObject, Error, Tr::tr("When condtion cannot contain an object."));
}
} // anonymous namespace

View File

@@ -113,7 +113,8 @@ enum Type {
WarnComponentRequiresChildren = 327,
WarnDuplicateImport = 400,
ErrAliasReferRoot = 401,
WarnAliasReferRootHierarchy = 402
WarnAliasReferRootHierarchy = 402,
ErrWhenConditionCannotBeObject = 403,
};
class QMLJS_EXPORT PrototypeMessageData {