diff options
author | Henning Gruendl <henning.gruendl@qt.io> | 2022-01-19 10:34:12 +0100 |
---|---|---|
committer | Henning Gründl <henning.gruendl@qt.io> | 2022-02-07 09:25:05 +0000 |
commit | 73ae5bb1f6f3dcb9526f333d9e6386fcce81bfc0 (patch) | |
tree | 437bab33d2132cf7007e1d5e7f6bb96b39a8ee1b /src/libs/qmljs/qmljscheck.cpp | |
parent | ba0886495454bdc2d777aa38f3c573463c4cd94f (diff) |
QmlDesigner: Show error/warning for root reference
Show an error when an alias property references the root item directly.
Show a warning when an alias property references the root item in a
hierarchy.
Task-number: QDS-4380
Change-Id: I312fc0296e3bb2321a4d9e1113bd0cd4c1c0b8ed
Reviewed-by: Leena Miettinen <riitta-leena.miettinen@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Diffstat (limited to 'src/libs/qmljs/qmljscheck.cpp')
-rw-r--r-- | src/libs/qmljs/qmljscheck.cpp | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/src/libs/qmljs/qmljscheck.cpp b/src/libs/qmljs/qmljscheck.cpp index 21c501791c..8cd87eda1e 100644 --- a/src/libs/qmljs/qmljscheck.cpp +++ b/src/libs/qmljs/qmljscheck.cpp @@ -1128,6 +1128,44 @@ bool Check::visit(UiPublicMember *ast) { if (ast->type == UiPublicMember::Property) { const QStringView typeName = ast->memberType->name; + + // Check alias properties don't reference root item + // Item { + // id: root + // property alias p1: root + // property alias p2: root.child + // + // Item { id: child } + // } + // - Show error for alias property p1 + // - Show warning for alias property p2 + + // Check if type and id stack only contain one item as we are only looking for alias + // properties in the root item. + if (typeName == QLatin1String("alias") && ast->type == AST::UiPublicMember::Property + && m_typeStack.count() == 1 && m_idStack.count() == 1 && m_idStack.top().count() == 1) { + + const QString rootId = m_idStack.top().values().first(); + if (!rootId.isEmpty()) { + if (ExpressionStatement *exp = cast<ExpressionStatement *>(ast->statement)) { + ExpressionNode *node = exp->expression; + + // Check for case property alias p1: root + if (IdentifierExpression *idExp = cast<IdentifierExpression *>(node)) { + if (!idExp->name.isEmpty() && idExp->name.toString() == rootId) + addMessage(ErrAliasReferRoot, idExp->identifierToken); + + // Check for case property alias p2: root.child + } else if (FieldMemberExpression *fmExp = cast<FieldMemberExpression *>(node)) { + if (IdentifierExpression *base = cast<IdentifierExpression *>(fmExp->base)) { + if (!base->name.isEmpty() && base->name.toString() == rootId) + addMessage(WarnAliasReferRootHierarchy, base->identifierToken); + } + } + } + } + } + // warn about dubious use of var/variant if (typeName == QLatin1String("variant") || typeName == QLatin1String("var")) { Evaluate evaluator(&_scopeChain); |