1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "helloplugin.h"
#include <QDebug>
using namespace Qt::StringLiterals;
static constexpr QQmlSA::LoggerWarningId helloWorld { "Plugin.HelloWorld.hello-world" };
class HelloWorldElementPass : public QQmlSA::ElementPass
{
public:
HelloWorldElementPass(QQmlSA::PassManager *manager);
bool shouldRun(const QQmlSA::Element &element) override;
void run(const QQmlSA::Element &element) override;
private:
QQmlSA::Element m_textType;
};
HelloWorldElementPass::HelloWorldElementPass(QQmlSA::PassManager *manager)
: QQmlSA::ElementPass(manager)
{
m_textType = resolveType("QtQuick", "Text");
}
bool HelloWorldElementPass::shouldRun(const QQmlSA::Element &element)
{
if (!element.inherits(m_textType))
return false;
if (!element.hasOwnPropertyBindings(u"text"_s))
return false;
return true;
}
void HelloWorldElementPass::run(const QQmlSA::Element &element)
{
auto textBindings = element.ownPropertyBindings(u"text"_s);
for (const auto &textBinding: textBindings) {
if (textBinding.bindingType() != QQmlSA::BindingType::StringLiteral)
continue;
QString currentBinding = textBinding.stringValue();
if (currentBinding == u"Hello world!"_s)
continue;
if (currentBinding == u"Goodbye world!"_s) {
QQmlSA::FixSuggestion suggestion(u"Replace 'Goodbye' with 'Hello'"_s,
textBinding.sourceLocation(), u"\"Hello world!\""_s);
suggestion.setAutoApplicable(true);
emitWarning("Incorrect greeting", helloWorld, textBinding.sourceLocation(), suggestion);
}
}
}
void HelloWorldPlugin::registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Element &rootElement)
{
const bool pluginIsEnabled = manager->isCategoryEnabled(helloWorld);
qDebug() << "Hello World plugin is" << (pluginIsEnabled ? "enabled" : "disabled");
if (!pluginIsEnabled)
return; // skip registration if the plugin is disabled anyway
manager->registerElementPass(std::make_unique<HelloWorldElementPass>(manager));
}
#include "moc_helloplugin.cpp"
|