aboutsummaryrefslogtreecommitdiffstats
path: root/tests/auto/qml/qmllint/lintplugin.cpp
diff options
context:
space:
mode:
authorMaximilian Goldstein <max.goldstein@qt.io>2022-03-16 13:19:54 +0100
committerMaximilian Goldstein <max.goldstein@qt.io>2022-04-05 14:06:56 +0200
commit7c9276d38b311042235a476e7421b64549b43413 (patch)
tree7d11a591f183fc00f229e84778b19790a0c95ef3 /tests/auto/qml/qmllint/lintplugin.cpp
parent93193747e8065c5c6c3c33c67c4383b7148b818c (diff)
qmllint: Integrate plugin infrastructure
Integrates the plugin and pass infrastructure into qmlcompiler and qmllint proper. Plugins are searched for in the "qmllint" subfolder of the Qt installation's plugin directory. In addition we also support loading statically linked plugins. [ChangeLog][qmllint][New Feature] qmllint can now be extended via plugins. Use --plugins to get a list of available plugins. Original-patch-by: Fabian Kosmale <fabian.kosmale@qt.io> Change-Id: I75a09dd978fc7724aca4931f055cc71c7ced971b Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qmllint/lintplugin.cpp')
-rw-r--r--tests/auto/qml/qmllint/lintplugin.cpp126
1 files changed, 126 insertions, 0 deletions
diff --git a/tests/auto/qml/qmllint/lintplugin.cpp b/tests/auto/qml/qmllint/lintplugin.cpp
new file mode 100644
index 0000000000..a413a4df2c
--- /dev/null
+++ b/tests/auto/qml/qmllint/lintplugin.cpp
@@ -0,0 +1,126 @@
+/****************************************************************************
+**
+** Copyright (C) 2022 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the test suite of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:GPL-EXCEPT$
+** Commercial License Usage
+** Licensees holding valid commercial Qt licenses may use this file in
+** accordance with the commercial license agreement provided with the
+** Software or, alternatively, in accordance with the terms contained in
+** a written agreement between you and The Qt Company. For licensing terms
+** and conditions see https://www.qt.io/terms-conditions. For further
+** information use the contact form at https://www.qt.io/contact-us.
+**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** included in the packaging of this file. Please review the following
+** information to ensure the GNU General Public License requirements will
+** be met: https://www.gnu.org/licenses/gpl-3.0.html.
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "lintplugin.h"
+
+class ElementTest : public QQmlSA::ElementPass
+{
+public:
+ ElementTest(QQmlSA::PassManager *manager) : QQmlSA::ElementPass(manager)
+ {
+ m_rectangle = resolveType(u"QtQuick", u"Rectangle");
+ }
+
+ bool shouldRun(const QQmlSA::Element &element) override
+ {
+ return element->baseType() == m_rectangle;
+ }
+
+ void run(const QQmlSA::Element &element) override
+ {
+ auto property = element->property(u"radius"_qs);
+ if (!property.isValid() || element->property(u"radius"_qs).typeName() != u"double") {
+ emitWarning(u"Failed to verify radius property", element->sourceLocation());
+ return;
+ }
+
+ auto bindings = element->propertyBindings(u"radius"_qs);
+ if (bindings.isEmpty() || bindings.constFirst().numberValue() != 5) {
+ emitWarning(u"Failed to verify radius property binding", element->sourceLocation());
+ return;
+ }
+
+ emitWarning(u"ElementTest OK", element->sourceLocation());
+ }
+
+private:
+ QQmlSA::Element m_rectangle;
+};
+
+class PropertyTest : public QQmlSA::PropertyPass
+{
+public:
+ PropertyTest(QQmlSA::PassManager *manager) : QQmlSA::PropertyPass(manager)
+ {
+ m_image = resolveType(u"QtQuick", u"Image");
+ }
+
+ bool shouldRun(const QQmlSA::Element &element, const QQmlJSMetaProperty &property,
+ const QList<QQmlJSMetaPropertyBinding> &bindings) override
+ {
+ return !bindings.isEmpty() && element->baseType() == m_image
+ && property.propertyName() == u"x";
+ }
+
+ void run(const QQmlJSMetaProperty &property,
+ const QList<QQmlJSMetaPropertyBinding> &bindings) override
+ {
+ if (property.typeName() != u"double") {
+ emitWarning(u"Failed to verify x property");
+ return;
+ }
+
+ bool foundInterceptor = false, foundValue = false;
+ for (const QQmlJSMetaPropertyBinding &binding : bindings) {
+ switch (binding.bindingType()) {
+ case QQmlJSMetaPropertyBinding::Interceptor:
+ foundInterceptor = true;
+ break;
+ case QQmlJSMetaPropertyBinding::NumberLiteral:
+ foundValue = true;
+ if (binding.numberValue() != 5) {
+ emitWarning(u"Binding has wrong value");
+ return;
+ }
+ break;
+ default:
+ emitWarning(u"Found unexpected binding on x property");
+ return;
+ }
+ }
+
+ if (!foundInterceptor || !foundValue) {
+ emitWarning(u"Didn't see all expected bindings");
+ return;
+ }
+
+ emitWarning(u"PropertyTest OK");
+ }
+
+private:
+ QQmlSA::Element m_image;
+};
+
+void LintPlugin::registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Element &rootElement)
+{
+ if (!rootElement->filePath().endsWith(u"_pluginTest.qml"))
+ return;
+
+ manager->registerElementPass(std::make_unique<ElementTest>(manager));
+ manager->registerPropertyPass(std::make_unique<PropertyTest>(manager));
+}