diff options
author | Maximilian Goldstein <max.goldstein@qt.io> | 2022-04-13 11:32:38 +0200 |
---|---|---|
committer | Maximilian Goldstein <max.goldstein@qt.io> | 2022-04-21 21:04:01 +0200 |
commit | fc3f4e5834ee41385c4f78fbdb34f49d0de5eccc (patch) | |
tree | 5b6c0ab84b145763d73071fda1219cc3d51a6871 /tests/auto/qml/qmllint/lintplugin.cpp | |
parent | 0b12dd572d00247cd2252039fa7eb14fa2d951d6 (diff) |
qqmlsa/PassManager: Add method for checking whether module is imported
Adds a method to the PassManager to check whether a QML document has
imported a particular QML module.
This is important as a lot of linting passes only need to run when a
certain module is present and can be skipped otherwise.
Checking for the module is accomplished by adding an invalid
$module$.<URI> scope to the imported types generated by QQmlJSImporter.
This means that not only direct imports but also indirect imports caused
by a qmldir import line will be properly detected.
Change-Id: I341f916a43c60d373d205b5571104d5c5a533d61
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Diffstat (limited to 'tests/auto/qml/qmllint/lintplugin.cpp')
-rw-r--r-- | tests/auto/qml/qmllint/lintplugin.cpp | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/auto/qml/qmllint/lintplugin.cpp b/tests/auto/qml/qmllint/lintplugin.cpp index a413a4df2c..9fe0789f41 100644 --- a/tests/auto/qml/qmllint/lintplugin.cpp +++ b/tests/auto/qml/qmllint/lintplugin.cpp @@ -116,6 +116,30 @@ private: QQmlSA::Element m_image; }; +class HasImportedModuleTest : public QQmlSA::ElementPass +{ +public: + HasImportedModuleTest(QQmlSA::PassManager *manager, QString message) + : QQmlSA::ElementPass(manager), m_message(message) + { + } + + bool shouldRun(const QQmlSA::Element &element) override + { + Q_UNUSED(element) + return true; + } + + void run(const QQmlSA::Element &element) override + { + Q_UNUSED(element) + emitWarning(m_message); + } + +private: + QString m_message; +}; + void LintPlugin::registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Element &rootElement) { if (!rootElement->filePath().endsWith(u"_pluginTest.qml")) @@ -123,4 +147,15 @@ void LintPlugin::registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Elem manager->registerElementPass(std::make_unique<ElementTest>(manager)); manager->registerPropertyPass(std::make_unique<PropertyTest>(manager)); + if (manager->hasImportedModule("QtQuick.Controls")) { + if (manager->hasImportedModule("QtQuick")) { + if (manager->hasImportedModule("QtQuick.Window")) { + manager->registerElementPass(std::make_unique<HasImportedModuleTest>( + manager, "QtQuick.Controls, QtQuick and QtQuick.Window present")); + } + } else { + manager->registerElementPass(std::make_unique<HasImportedModuleTest>( + manager, "QtQuick.Controls and NO QtQuick present")); + } + } } |