diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-21 14:11:10 +0200 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2020-10-22 09:57:57 +0200 |
commit | 826b7c3b18b2104cd0ff2c4b90892e72e149a88d (patch) | |
tree | ec337d2708e9944216b62c3c002c3765ed1dfa6f | |
parent | ec9d125f553c073ec7a431114a568eaeb6534b63 (diff) |
qmllint: Resolve aliases in nested objects
So far we've just ignored them.
Change-Id: I2ca522ef825a341a3f4bf1c2a42fb0376180cdda
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
-rw-r--r-- | src/qmlcompiler/qqmljsimportvisitor.cpp | 47 | ||||
-rw-r--r-- | src/qmlcompiler/qqmljsimportvisitor_p.h | 4 | ||||
-rw-r--r-- | tests/auto/qml/qmllint/data/badAliasObject.qml | 10 | ||||
-rw-r--r-- | tests/auto/qml/qmllint/data/goodAliasObject.qml | 10 | ||||
-rw-r--r-- | tests/auto/qml/qmllint/tst_qmllint.cpp | 5 |
5 files changed, 55 insertions, 21 deletions
diff --git a/src/qmlcompiler/qqmljsimportvisitor.cpp b/src/qmlcompiler/qqmljsimportvisitor.cpp index 5ff9accd97..6595b1d8d7 100644 --- a/src/qmlcompiler/qqmljsimportvisitor.cpp +++ b/src/qmlcompiler/qqmljsimportvisitor.cpp @@ -30,6 +30,7 @@ #include <QtCore/qfileinfo.h> #include <QtCore/qdir.h> +#include <QtCore/qqueue.h> QT_BEGIN_NAMESPACE @@ -60,32 +61,33 @@ void QQmlJSImportVisitor::leaveEnvironment() m_currentScope = m_currentScope->parentScope(); } -QQmlJSScope::Ptr QQmlJSImportVisitor::result() const +void QQmlJSImportVisitor::resolveAliases() { - QQmlJSScope::Ptr result = QQmlJSScope::create(); - result->setIsComposite(true); - result->setBaseTypeName(m_qmlRootScope->baseTypeName()); - const auto properties = m_qmlRootScope->properties(); - for (auto property : properties) { - if (property.isAlias()) { + QQueue<QQmlJSScope::Ptr> objects; + objects.enqueue(m_qmlRootScope); + + while (!objects.isEmpty()) { + const QQmlJSScope::Ptr object = objects.dequeue(); + const auto properties = object->properties(); + for (auto property : properties) { + if (!property.isAlias()) + continue; const auto it = m_scopesById.find(property.typeName()); - if (it != m_scopesById.end()) + if (it != m_scopesById.end()) { property.setType(QQmlJSScope::ConstPtr(*it)); - result->addProperty(property); - } else { - result->addProperty(property); + object->addProperty(property); + } } - } - - for (const auto &method : m_qmlRootScope->methods()) - result->addMethod(method); - for (const auto &enumerator : m_qmlRootScope->enums()) - result->addEnum(enumerator); + const auto childScopes = object->childScopes(); + for (const auto &childScope : childScopes) + objects.enqueue(childScope); + } +} - result->resolveTypes(m_rootScopeImports); - result->setSourceLocation(m_qmlRootScope->sourceLocation()); - return result; +QQmlJSScope::Ptr QQmlJSImportVisitor::result() const +{ + return m_qmlRootScope; } void QQmlJSImportVisitor::importExportedNames(QQmlJSScope::ConstPtr scope) @@ -126,6 +128,11 @@ bool QQmlJSImportVisitor::visit(QQmlJS::AST::UiProgram *) return true; } +void QQmlJSImportVisitor::endVisit(UiProgram *) +{ + resolveAliases(); +} + bool QQmlJSImportVisitor::visit(UiObjectDefinition *definition) { QString superType; diff --git a/src/qmlcompiler/qqmljsimportvisitor_p.h b/src/qmlcompiler/qqmljsimportvisitor_p.h index 0255450c68..eef5889263 100644 --- a/src/qmlcompiler/qqmljsimportvisitor_p.h +++ b/src/qmlcompiler/qqmljsimportvisitor_p.h @@ -59,6 +59,7 @@ public: protected: bool visit(QQmlJS::AST::UiProgram *) override; + void endVisit(QQmlJS::AST::UiProgram *) override; bool visit(QQmlJS::AST::UiObjectDefinition *) override; void endVisit(QQmlJS::AST::UiObjectDefinition *) override; bool visit(QQmlJS::AST::UiPublicMember *) override; @@ -97,7 +98,7 @@ protected: QString m_implicitImportDirectory; QStringList m_qmltypesFiles; QQmlJSScope::Ptr m_currentScope; - QQmlJSScope::ConstPtr m_qmlRootScope; + QQmlJSScope::Ptr m_qmlRootScope; QQmlJSScope::ConstPtr m_globalScope; QHash<QString, QQmlJSScope::ConstPtr> m_scopesById; QHash<QString, QQmlJSScope::ConstPtr> m_rootScopeImports; @@ -110,6 +111,7 @@ protected: void leaveEnvironment(); private: + void resolveAliases(); void visitFunctionExpressionHelper(QQmlJS::AST::FunctionExpression *fexpr); void importExportedNames(QQmlJSScope::ConstPtr scope); }; diff --git a/tests/auto/qml/qmllint/data/badAliasObject.qml b/tests/auto/qml/qmllint/data/badAliasObject.qml new file mode 100644 index 0000000000..1455b1daa9 --- /dev/null +++ b/tests/auto/qml/qmllint/data/badAliasObject.qml @@ -0,0 +1,10 @@ +import QtQuick + +Item { + Item { + QtObject { id: inner } + + property alias innerObj: inner + property string name: innerObj.wrongwrongwrong + } +} diff --git a/tests/auto/qml/qmllint/data/goodAliasObject.qml b/tests/auto/qml/qmllint/data/goodAliasObject.qml new file mode 100644 index 0000000000..0fe0d33857 --- /dev/null +++ b/tests/auto/qml/qmllint/data/goodAliasObject.qml @@ -0,0 +1,10 @@ +import QtQuick + +Item { + Item { + QtObject { id: inner } + + property alias innerObj: inner + property string name: innerObj.objectName + } +} diff --git a/tests/auto/qml/qmllint/tst_qmllint.cpp b/tests/auto/qml/qmllint/tst_qmllint.cpp index 0d2ab4c4cc..d9671a6631 100644 --- a/tests/auto/qml/qmllint/tst_qmllint.cpp +++ b/tests/auto/qml/qmllint/tst_qmllint.cpp @@ -228,6 +228,10 @@ void TestQmllint::dirtyQmlCode_data() << QStringLiteral("nanchors3.qml") << QString() << QString(); + QTest::newRow("badAliasObject") + << QStringLiteral("badAliasObject.qml") + << QString("Warning: Property \"wrongwrongwrong\" not found on type \"QtObject\"") + << QString(); } void TestQmllint::dirtyQmlCode() @@ -287,6 +291,7 @@ void TestQmllint::cleanQmlCode_data() QTest::newRow("anchors1") << QStringLiteral("anchors1.qml"); QTest::newRow("anchors2") << QStringLiteral("anchors2.qml"); QTest::newRow("optionalImport") << QStringLiteral("optionalImport.qml"); + QTest::newRow("goodAliasObject") << QStringLiteral("goodAliasObject.qml"); } void TestQmllint::cleanQmlCode() |