1// Copyright (C) 2021 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 QMLJSLINTER_P_H
5#define QMLJSLINTER_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16
17#include <private/qtqmlcompilerexports_p.h>
18
19#include <QtQmlCompiler/private/qqmljslogger_p.h>
20#include <QtQmlCompiler/private/qqmljsimporter_p.h>
21
22#include <QtQml/private/qqmljssourcelocation_p.h>
23
24#include <QtCore/qjsonarray.h>
25#include <QtCore/qstring.h>
26#include <QtCore/qmap.h>
27#include <QtCore/qscopedpointer.h>
28
29#include <vector>
30#include <optional>
31
32QT_BEGIN_NAMESPACE
33
34class QPluginLoader;
35struct QStaticPlugin;
36
37namespace QQmlSA {
38class LintPlugin;
39}
40
41class Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSLinter
42{
43public:
44 QQmlJSLinter(const QStringList &importPaths,
45 const QStringList &pluginPaths = { QQmlJSLinter::defaultPluginPath() },
46 bool useAbsolutePath = false);
47
48 enum LintResult { FailedToOpen, FailedToParse, HasWarnings, LintSuccess };
49 enum FixResult { NothingToFix, FixError, FixSuccess };
50
51 class Q_QMLCOMPILER_PRIVATE_EXPORT Plugin
52 {
53 Q_DISABLE_COPY(Plugin)
54 public:
55 Plugin() = default;
56 Plugin(Plugin &&plugin) noexcept;
57
58#if QT_CONFIG(library)
59 Plugin(QString path);
60#endif
61 Plugin(const QStaticPlugin &plugin);
62 ~Plugin();
63
64 const QString &name() const { return m_name; }
65 const QString &description() const { return m_description; }
66 const QString &version() const { return m_version; }
67 const QString &author() const { return m_author; }
68 const QList<QQmlJS::LoggerCategory> categories() const
69 {
70 return m_categories;
71 }
72 bool isBuiltin() const { return m_isBuiltin; }
73 bool isValid() const { return m_isValid; }
74 bool isInternal() const
75 {
76 return m_isInternal;
77 }
78
79 bool isEnabled() const
80 {
81 return m_isEnabled;
82 }
83 void setEnabled(bool isEnabled)
84 {
85 m_isEnabled = isEnabled;
86 }
87
88 private:
89 friend class QQmlJSLinter;
90
91 bool parseMetaData(const QJsonObject &metaData, QString pluginName);
92
93 QString m_name;
94 QString m_description;
95 QString m_version;
96 QString m_author;
97
98 QList<QQmlJS::LoggerCategory> m_categories;
99 QQmlSA::LintPlugin *m_instance;
100 std::unique_ptr<QPluginLoader> m_loader;
101 bool m_isBuiltin = false;
102 bool m_isInternal =
103 false; // Internal plugins are those developed and maintained inside the Qt project
104 bool m_isValid = false;
105 bool m_isEnabled = true;
106 };
107
108 static std::vector<Plugin> loadPlugins(QStringList paths);
109 static QString defaultPluginPath();
110
111 LintResult lintFile(const QString &filename, const QString *fileContents, const bool silent,
112 QJsonArray *json, const QStringList &qmlImportPaths,
113 const QStringList &qmldirFiles, const QStringList &resourceFiles,
114 const QList<QQmlJS::LoggerCategory> &categories);
115
116 LintResult lintModule(const QString &uri, const bool silent, QJsonArray *json,
117 const QStringList &qmlImportPaths, const QStringList &resourceFiles);
118
119 FixResult applyFixes(QString *fixedCode, bool silent);
120
121 const QQmlJSLogger *logger() const { return m_logger.get(); }
122
123 std::vector<Plugin> &plugins()
124 {
125 return m_plugins;
126 }
127 void setPlugins(std::vector<Plugin> plugins) { m_plugins = std::move(plugins); }
128
129 void setPluginsEnabled(bool enablePlugins) { m_enablePlugins = enablePlugins; }
130 bool pluginsEnabled() const { return m_enablePlugins; }
131
132 void clearCache() { m_importer.clearCache(); }
133
134private:
135 void parseComments(QQmlJSLogger *logger, const QList<QQmlJS::SourceLocation> &comments);
136 void processMessages(QJsonArray &warnings);
137
138 bool m_useAbsolutePath;
139 bool m_enablePlugins;
140 QQmlJSImporter m_importer;
141 QScopedPointer<QQmlJSLogger> m_logger;
142 QString m_fileContents;
143 std::vector<Plugin> m_plugins;
144};
145
146QT_END_NAMESPACE
147
148#endif // QMLJSLINTER_P_H
149

source code of qtdeclarative/src/qmlcompiler/qqmljslinter_p.h