| 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 | |
| 15 | QT_BEGIN_NAMESPACE |
| 16 | |
| 17 | struct TypeDescription |
| 18 | { |
| 19 | QString module; |
| 20 | QString name; |
| 21 | }; |
| 22 | |
| 23 | class 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 | |
| 29 | public: |
| 30 | void registerPasses(QQmlSA::PassManager *manager, const QQmlSA::Element &rootElement) override; |
| 31 | }; |
| 32 | |
| 33 | class ForbiddenChildrenPropertyValidatorPass : public QQmlSA::ElementPass |
| 34 | { |
| 35 | public: |
| 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 | |
| 43 | private: |
| 44 | struct Warning |
| 45 | { |
| 46 | QString propertyName; |
| 47 | QString message; |
| 48 | }; |
| 49 | |
| 50 | QHash<QQmlSA::Element, QVarLengthArray<Warning, 8>> m_types; |
| 51 | }; |
| 52 | |
| 53 | class AttachedPropertyTypeValidatorPass : public QQmlSA::PropertyPass |
| 54 | { |
| 55 | public: |
| 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 | |
| 70 | private: |
| 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 | |
| 83 | class ControlsNativeValidatorPass : public QQmlSA::ElementPass |
| 84 | { |
| 85 | public: |
| 86 | ControlsNativeValidatorPass(QQmlSA::PassManager *manager); |
| 87 | |
| 88 | bool shouldRun(const QQmlSA::Element &element) override; |
| 89 | void run(const QQmlSA::Element &element) override; |
| 90 | |
| 91 | private: |
| 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 | |
| 105 | class AnchorsValidatorPass : public QQmlSA::ElementPass |
| 106 | { |
| 107 | public: |
| 108 | AnchorsValidatorPass(QQmlSA::PassManager *manager); |
| 109 | |
| 110 | bool shouldRun(const QQmlSA::Element &element) override; |
| 111 | void run(const QQmlSA::Element &element) override; |
| 112 | |
| 113 | private: |
| 114 | QQmlSA::Element m_item; |
| 115 | }; |
| 116 | |
| 117 | class ControlsSwipeDelegateValidatorPass : public QQmlSA::ElementPass |
| 118 | { |
| 119 | public: |
| 120 | ControlsSwipeDelegateValidatorPass(QQmlSA::PassManager *manager); |
| 121 | |
| 122 | bool shouldRun(const QQmlSA::Element &element) override; |
| 123 | void run(const QQmlSA::Element &element) override; |
| 124 | |
| 125 | private: |
| 126 | QQmlSA::Element m_swipeDelegate; |
| 127 | }; |
| 128 | |
| 129 | class VarBindingTypeValidatorPass : public QQmlSA::PropertyPass |
| 130 | { |
| 131 | public: |
| 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 | |
| 139 | private: |
| 140 | QMultiHash<QString, QQmlSA::Element> m_expectedPropertyTypes; |
| 141 | }; |
| 142 | |
| 143 | class PropertyChangesValidatorPass : public QQmlSA::ElementPass |
| 144 | { |
| 145 | public: |
| 146 | PropertyChangesValidatorPass(QQmlSA::PassManager *manager); |
| 147 | |
| 148 | bool shouldRun(const QQmlSA::Element &element) override; |
| 149 | void run(const QQmlSA::Element &element) override; |
| 150 | |
| 151 | private: |
| 152 | QQmlSA::Element m_propertyChanges; |
| 153 | }; |
| 154 | |
| 155 | class AttachedPropertyReuse : public QQmlSA::PropertyPass |
| 156 | { |
| 157 | public: |
| 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 | |
| 173 | private: |
| 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 | |
| 183 | class StateNoItemChildrenValidator : public QQmlSA::ElementPass |
| 184 | { |
| 185 | public: |
| 186 | StateNoItemChildrenValidator(QQmlSA::PassManager *manager); |
| 187 | |
| 188 | bool shouldRun(const QQmlSA::Element &element) override; |
| 189 | void run(const QQmlSA::Element &element) override; |
| 190 | |
| 191 | private: |
| 192 | QQmlSA::Element m_state; |
| 193 | QQmlSA::Element m_anchorChanges; |
| 194 | QQmlSA::Element m_parentChanges; |
| 195 | QQmlSA::Element m_propertyChanges; |
| 196 | QQmlSA::Element m_stateChangeScript; |
| 197 | }; |
| 198 | |
| 199 | QT_END_NAMESPACE |
| 200 | |
| 201 | #endif // QUICKLINTPLUGIN_H |
| 202 | |