1// Copyright (C) 2022 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
4#ifndef QUICKLINTPLUGIN_H
5#define QUICKLINTPLUGIN_H
6
7#include <QtCore/qplugin.h>
8#include <QtCore/qlist.h>
9#include <QtCore/qvarlengtharray.h>
10#include <QtCore/qhash.h>
11
12#include <QtQmlCompiler/qqmlsa.h>
13#include "qqmlsaconstants.h"
14
15QT_BEGIN_NAMESPACE
16
17struct TypeDescription
18{
19 QString module;
20 QString name;
21};
22
23class QmlLintQuickPlugin : public QObject, public QQmlSA::LintPlugin
24{
25 Q_OBJECT
26 Q_PLUGIN_METADATA(IID QmlLintPluginInterface_iid FILE "plugin.json")
27 Q_INTERFACES(QQmlSA::LintPlugin)
28
29public:
30 void registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Element &rootElement) override;
31};
32
33class ForbiddenChildrenPropertyValidatorPass : public QQmlSA::ElementPass
34{
35public:
36 ForbiddenChildrenPropertyValidatorPass(QQmlSA::PassManager *manager);
37
38 void addWarning(QAnyStringView moduleName, QAnyStringView typeName, QAnyStringView propertyName,
39 QAnyStringView warning);
40 bool shouldRun(const QQmlSA::Element &element) override;
41 void run(const QQmlSA::Element &element) override;
42
43private:
44 struct Warning
45 {
46 QString propertyName;
47 QString message;
48 };
49
50 QHash<QQmlSA::Element, QVarLengthArray<Warning, 8>> m_types;
51};
52
53class AttachedPropertyTypeValidatorPass : public QQmlSA::PropertyPass
54{
55public:
56 AttachedPropertyTypeValidatorPass(QQmlSA::PassManager *manager);
57
58 QString addWarning(TypeDescription attachType, QList<TypeDescription> allowedTypes,
59 bool allowInDelegate, QAnyStringView warning);
60
61 void onBinding(const QQmlSA::Element &element, const QString &propertyName,
62 const QQmlSA::Binding &binding, const QQmlSA::Element &bindingScope,
63 const QQmlSA::Element &value) override;
64 void onRead(const QQmlSA::Element &element, const QString &propertyName,
65 const QQmlSA::Element &readScope, QQmlSA::SourceLocation location) override;
66 void onWrite(const QQmlSA::Element &element, const QString &propertyName,
67 const QQmlSA::Element &value, const QQmlSA::Element &writeScope,
68 QQmlSA::SourceLocation location) override;
69
70private:
71 void checkWarnings(const QQmlSA::Element &element, const QQmlSA::Element &scopeUsedIn,
72 const QQmlSA::SourceLocation &location);
73
74 struct Warning
75 {
76 QVarLengthArray<QQmlSA::Element, 4> allowedTypes;
77 bool allowInDelegate = false;
78 QString message;
79 };
80 QHash<QString, Warning> m_attachedTypes;
81};
82
83class ControlsNativeValidatorPass : public QQmlSA::ElementPass
84{
85public:
86 ControlsNativeValidatorPass(QQmlSA::PassManager *manager);
87
88 bool shouldRun(const QQmlSA::Element &element) override;
89 void run(const QQmlSA::Element &element) override;
90
91private:
92 struct ControlElement
93 {
94 QString name;
95 QStringList restrictedProperties;
96 bool isInModuleControls = true;
97 bool isControl = false;
98 bool inheritsControl = false;
99 QQmlSA::Element element = {};
100 };
101
102 QList<ControlElement> m_elements;
103};
104
105class AnchorsValidatorPass : public QQmlSA::ElementPass
106{
107public:
108 AnchorsValidatorPass(QQmlSA::PassManager *manager);
109
110 bool shouldRun(const QQmlSA::Element &element) override;
111 void run(const QQmlSA::Element &element) override;
112
113private:
114 QQmlSA::Element m_item;
115};
116
117class ControlsSwipeDelegateValidatorPass : public QQmlSA::ElementPass
118{
119public:
120 ControlsSwipeDelegateValidatorPass(QQmlSA::PassManager *manager);
121
122 bool shouldRun(const QQmlSA::Element &element) override;
123 void run(const QQmlSA::Element &element) override;
124
125private:
126 QQmlSA::Element m_swipeDelegate;
127};
128
129class VarBindingTypeValidatorPass : public QQmlSA::PropertyPass
130{
131public:
132 VarBindingTypeValidatorPass(QQmlSA::PassManager *manager,
133 const QMultiHash<QString, TypeDescription> &expectedPropertyTypes);
134
135 void onBinding(const QQmlSA::Element &element, const QString &propertyName,
136 const QQmlSA::Binding &binding, const QQmlSA::Element &bindingScope,
137 const QQmlSA::Element &value) override;
138
139private:
140 QMultiHash<QString, QQmlSA::Element> m_expectedPropertyTypes;
141};
142
143class PropertyChangesValidatorPass : public QQmlSA::ElementPass
144{
145public:
146 PropertyChangesValidatorPass(QQmlSA::PassManager *manager);
147
148 bool shouldRun(const QQmlSA::Element &element) override;
149 void run(const QQmlSA::Element &element) override;
150
151private:
152 QQmlSA::Element m_propertyChanges;
153};
154
155class AttachedPropertyReuse : public QQmlSA::PropertyPass
156{
157public:
158 enum Mode {
159 CheckAll,
160 RestrictToControls
161 };
162
163 AttachedPropertyReuse(QQmlSA::PassManager *manager, QQmlSA::LoggerWarningId category)
164 : QQmlSA::PropertyPass(manager), category(category)
165 {}
166
167 void onRead(const QQmlSA::Element &element, const QString &propertyName,
168 const QQmlSA::Element &readScope, QQmlSA::SourceLocation location) override;
169 void onWrite(const QQmlSA::Element &element, const QString &propertyName,
170 const QQmlSA::Element &value, const QQmlSA::Element &writeScope,
171 QQmlSA::SourceLocation location) override;
172
173private:
174 struct ElementAndLocation {
175 QQmlSA::Element element;
176 QQmlSA::SourceLocation location;
177 };
178
179 QMultiHash<QQmlSA::Element, ElementAndLocation> usedAttachedTypes;
180 QQmlSA::LoggerWarningId category;
181};
182
183QT_END_NAMESPACE
184
185#endif // QUICKLINTPLUGIN_H
186

source code of qtdeclarative/src/plugins/qmllint/quick/quicklintplugin.h